Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with group by

Author  Topic 

hallyvaux
Starting Member

12 Posts

Posted - 2011-05-02 : 17:14:11
Can you help me understand group by?
If you are using the select statement and selecting aggregate functions with and alias, then you use the group by clause and group by a field that you used in more then one aggregate function, what is it grouping by? EX.

SELECT NAME "Proudct Name", SUM(ORDERQTY)"Qty Ordered" ,SUM(SELLPRICE) "Price Sold" ,SUM(SELLPRICE) * SUM(ORDERQTY) "Total Sale"
FROM GS_SALES,GS_PRODUCTS
WHERE GS_SALES.PRODID = GS_PRODUCTS.PRODID
GROUP BY SELLPRICE, NAME,ORDERDATE,ORDERQTY
ORDER BY SELLPRICE DESC, NAME DESC;

SELLPRICE is part of "price sold" AND "Total Sales" so when I use group by, what is going on? Also if you see where this is just a ridiculous way to get this info, please tell me because I am partially lost.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-05-02 : 17:41:24
It's grouping on the data and doesn't matter how many times you've referenced it in the query.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -