>>>I cannot figure out why my query is not grouping the way I expect (VFP8):
>>>
>>>
>>>SELECT 0000000000+SUM(NVL(WeekShips.Shipped,0)) AS nShipped, ;
>>> 0000000000+SUM(NVL(WeekSales.Sales,0)) AS WeekSales, ;
>>> 0000000000+SUM(NVL(YTDSales.Sales,0)) AS YTDSales, ;
>>> ItemSetup.Retail, 0000000000+NVL(iciwhs.nOnHand,0) AS nOnHand ;
>>> FROM ItemSetup ;
>>> INNER JOIN ItemSales ON ItemSetup.cItemNo = ItemSales.cItemNo ;
>>> AND ItemSales.Category = "TRANSFER" ;
>>> LEFT JOIN WeekShips ON WeekShips.cItemNo = ItemSetup.cItemNo ;
>>> LEFT JOIN WeekSales ON WeekSales.UnitRetail = ItemSetup.Retail ;
>>> LEFT JOIN IciWhs ON iciWhs.cItemNo = ItemSetup.cItemNo ;
>>> LEFT JOIN YTDSales ON YTDSales.UnitRetail = ItemSetup.Retail ;
>>> ORDER BY ItemSetup.Retail, nOnHand ;
>>> GROUP BY ItemSetup.Retail, nOnHand ;
>>> HAVING YTDSales > 0 ;
>>> INTO CURSOR SpreadsheetResults
>>>
>>>
>>>My resulting cursor has many records for each "retail" value! Any idea why?
>>
>>I presume you have multiple nOnHand values for each "retail" value, since you are also grouping on nOnHand.
>
>No - Proper SQL syntax now requires that all non-aggregate fields appear in the 'group by' clause to define sort order for grouping (as of VFP8).
>
>>
>>I think that your ORDER BY clause is superfluous. If I remember right, it will order on the stuff in the GROUP BY clause.
>
>I think you are right about the ORDER BY clause being redundant... I added it while grasping at straws.
If my guess is wrong, then I'm not sure. Perhaps someone else has a better idea. A few example lines from the result set might help. Meanwhile, here are a few more straws to grasp:
- Take 'AND ItemSales.Category = "TRANSFER"' out of the INNER JOIN and make it a WHERE condition. It may be fine where it is, but I find it unusual.
- Replace "GROUP BY ItemSetup.Retail, nOnHand" with "GROUP BY 4, 5". It shouldn't be necessary, though.
My Visual FoxPro SQL knowledge is indeed out of date.