Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL GROUP BY query

Status
Not open for further replies.

StewartUK

Programmer
Feb 19, 2001
860
GB
Using VFP9 SP2

I have the following SQL:
Code:
SELECT TOP 15 Claimed, SUM(Amount)*TaxRates.taxrate/TaxRates.GrossUp AS Total, {} AS FirstDate, {} AS LastDate ; 
  FROM Donation ;
    JOIN TaxRates ON BETWEEN(DonDate, YearStart, YearEnd) ;
  WHERE NOT EMPTY(claimed) ;
INTO CURSOR curTemp GROUP BY 1,3,4 ORDER BY 1 DESCENDING

This gives me the GROUP BY CLAUSE is invalid error. Can someone suggest why that is?

I can run the above as
Code:
SELECT TOP 15 Claimed, SUM(Amount)*[COLOR=red]22/78[/color] AS Total, {} AS FirstDate, {} AS LastDate ; 
  FROM Donation ;
    JOIN TaxRates ON BETWEEN(DonDate, YearStart, YearEnd) ;
  WHERE NOT EMPTY(claimed) ;
INTO CURSOR curTemp GROUP BY 1,3,4 ORDER BY 1 DESCENDING

Thanks,

Stewart
 
Possibly interesting - I found that after SET ENGINEBEHAVIOR 70, the first SQL worked.

Stewart
 
SUM(Amount)*TaxRates.taxrate/TaxRates.GrossUp

This puzzles me. That closing bracket looks to be in the wrong place. You're asking it to SUM the Amount so that's got to be done after the grouping; by which time SQL doesn't know which taxrate and grossup to use in the calculation.

Geoff Franklin
 
Well, you put me on the right track Geoff. Moving the closing bracket means that the select runs correctly.

Thanks very much,

Stewart
 
Stewart,

By the way, you don't need to put columns 3 and 4 in the GROUP BY. In other words, just do GROUP BY 1. The fact that those two columns contain the same value in every row means that the SELECT will satisfy the rules.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Hi Mike,

Yes, I was suprised to notice that it worked just with GROUP BY 1.

I hadn't really thought about it, but I suppose I thought it checked the group by before starting, but obviously not. However it does suggest that it scans (in the everyday sense not the VFP sense) the results before grouping.

Interesting.

Stewart
 
Stewart,

It might be that it simply looks at the expressions, and sees that they don't depend on any of the data in the tables, and so allows the grouping. That would make sense, as it would avoid the need to scan the results before deciding if the grouping was valid. But who knows.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top