2021-05-31

sqldf only returning one row, same query used in SQL

For some reason I'm only returning one row when it comes to R while at SQL Server, I'm returning the correct number of rows. SQLDF:

CustomerCodingChangesT <- sqldf("
        SELECT c.CustID as ID, c.ReverseSupplier as Supplier, c.ReverseCustomerCode as Code, c.Name, c.Address, c.[From PostCode], c.[From Outlet], c.[From OutletName], 
        o.FullAddress AS [From Address], 
        c.[To PostCode], c.[To Outlet], c.[To OutletName], 
        o1.FullAddress AS [To Address], 
        Max(Cast( c.TotalUnits as varchar)) as [Total Units], '$'+Max(cast(c.TotalValue as varchar)) as [Total Value], '' AS Checked, c.CustRecActive as Active
        FROM CustomerCorrectionSummaryT AS c
        LEFT JOIN OutletMasterT AS o ON c.[From PostCode] = o.Postcode AND c.[From Outlet] = o.Outlet 
        LEFT JOIN OutletMasterT AS o1 ON c.[To PostCode] = o1.Postcode AND c.[To Outlet] = o1.Outlet
        ORDER BY c.totalvalue DESC;")

SQL:

if object_id ('tempdb..#CustomerCodingChanges') is not null drop table #CustomerCodingChanges
SELECT c.CustID as ID, c.ReverseSupplier as Supplier, c.ReverseCustomerCode as Code, c.Name, c.Address, c.[From Postcode], c.[From Outlet], c.[From OutletName], 
o.FullAddress AS [From Address], c.[To Postcode], c.[To Outlet], c.[To OutletName], 
o1.FullAddress AS [To Address], cast(c.TotalUnits as varchar(max)) as [Total Units], '$'+cast(c.TotalValue as varchar(max)) as [Total Value], '' AS Checked, c.CustRecActive as Active
        INTO #CustomerCodingChanges
    FROM #CustomerCorrectionSummary AS c
        LEFT JOIN ndf_061.IRGMaster.dbo.OutletMaster AS o ON c.[From Postcode] = o.postcode AND c.[From Outlet] = o.outlet
        LEFT JOIN ndf_061.IRGMaster.dbo.OutletMaster AS o1 ON c.[To Postcode] = o1.postcode AND c.[To Outlet] = o1.outlet
    ORDER BY c.totalvalue DESC;

Both dataframes for CustomerCorrectionSummaryT and OutletMasterT have the same number of results in R and in SQL so i dont know why it won't also show the same number of result in R versus in SQL. In SQL in returning 22 rows while in R i'm only getting one row which both are correct. R's sqldf just doesn't show all of it. Im thinking it has something to do with my left join function but i don't really know. Lemme know if you need more information!



from Recent Questions - Stack Overflow https://ift.tt/34zK13k
https://ift.tt/eA8V8J

No comments:

Post a Comment