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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Group By Problem

Status
Not open for further replies.

craigfw

Technical User
Aug 11, 2004
32
0
0
US
Greetings, I'm pretty slow at SQL for as long as I've been associated with SQL Server. But non the less...

I have a query that runs correctly until I attempt to add a group by. I know it must be something not too difficult, but, I'm at a loss.

select [Entry No_], [Sale No_], [Posting Date]
from "LARS-403-Test$Timber Ticket Ledger Entry"
where [Posting Date] between '2006-02-02' and '2006-12-02'

order by [Sale No_]
/*group by [Posting Date]*/

Entry No_ Sale No_ Posting Date
--------- --------- -----------------------
263015 SS500056 2006-02-03 00:00:00 000
263015 SS500056 2006-02-03 00:00:00 000
etc..

But when I introduce the group by clause I get:

select [Entry No_], [Sale No_], [Posting Date]
from "LARS-403-Test$Timber Ticket Ledger Entry"
where [Posting Date] between '2006-02-02' and '2006-12-02'

order by [Sale No_]
group by [Posting Date]

Server: Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'group'.

OR

if the group by is before the order by then I get this:

Server: Msg 8120, Level 16, State 1, Line 2
Column 'LARS-403-Test$Timber Ticket Ledger Entry.Entry No_' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.



 


You have to GROUP BY all columns not "AGGREGATED":
Code:
select  [Entry No_], [Sale No_], [Posting Date]
from "LARS-403-Test$Timber Ticket Ledger Entry"
where [Posting Date] between '2006-02-02' and '2006-12-02'

order by [Sale No_]
group by [Entry No_], [Sale No_],[Posting Date]
[3eyes]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 

PS: The above is equivalent to:
Code:
SELECT DISTINCT ... FROM...
[thumbsup2]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thanks for the feedback. You're correct about the not agregating (Is that a word?)the columns which are in the Select into the Where clause.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top