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
 Group by

Author  Topic 

mukhan85
Starting Member

46 Posts

Posted - 2008-05-30 : 18:52:46
Hi,
suppose I have the following query:
select colName1 as column1,
colName2 as column2
sum(colName3) as Total
from table
group by colName1, colName2 // here confusion

How the "group by" will work? Will it group by colName1 or colName2 , it seems that it will group by both columns, but I cant imangine how it will look like... Could you please give a simple example?
Thank you.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-30 : 23:49:30
It groups by whatever you put in the group by. A data example would be let's say you want to add up all of the employees in each of the departments in each of the companies stored in your database. So let's say you've got two companies. The first company has 2 departments and the second company has 5 departments. Each of those departments have people in them. To add them up and show them grouped by their department and their company, you would use a query similar to the one that you have posted.

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

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page
   

- Advertisement -