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 with Adventureworks

Author  Topic 

madhan
Yak Posting Veteran

59 Posts

Posted - 2014-11-26 : 13:27:36
I am trying to understand group by concept using below query problem.

write a query that displays the count of orders placed by year for each customer using the Sales.SalesOrderHeader table

select count(*) as CountOfOrders,year(OrderDate) as OrderYear,CustomerID from Sales.SalesOrderHeader
group by CustomerID,year(OrderDate)

Is my query correct for the question,please

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-11-26 : 13:30:33
Yes looks correct. I normally list the GROUP BY columns first in the SELECT and then the aggregate functions last. But that doesn't impact the results, just is my programming style.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -