SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Counting
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mpayne2010
Starting Member

14 Posts

Posted - 02/28/2013 :  17:46:34  Show Profile  Reply with Quote
When I run this sql:

Select count(Prodname) ProdCount, ProdName, year(orderdate) year
from products
inner join orders on prodnum = orderprodnum
group by prodname, orderdate
Order By Prodname, orderdate

Im getting this result:
ProdCount ProdName year
1 Gargantuan Alabaster Delightful Carpet sweeper 2000
1 Gargantuan Alabaster Delightful Carpet sweeper 2000
1 Gargantuan Alabaster Delightful Carpet sweeper 2000
1 Gargantuan Alabaster Delightful Carpet sweeper 2000
1 Gargantuan Alabaster Delightful Carpet sweeper 2001
1 Gargantuan Alabaster Delightful Carpet sweeper 2001
1 Gargantuan Alabaster Delightful Carpet sweeper 2001

The result I want is for each year 2000 to be totaled. So the result would look like:

4 Gargantuan Alabaster Delightful Carpet sweeper 2000
3 Gargantuan Alabaster Delightful Carpet sweeper 2001

Thanks in advance!!!
Michele

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 02/28/2013 :  18:07:22  Show Profile  Reply with Quote
Don't group by orderdate, rather by YEAR(OrderDate)

Edited by - Lamprey on 02/28/2013 18:08:02
Go to Top of Page

mpayne2010
Starting Member

14 Posts

Posted - 02/28/2013 :  18:13:48  Show Profile  Reply with Quote
Nope, still giving me the same results
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3724 Posts

Posted - 02/28/2013 :  18:21:18  Show Profile  Reply with Quote
I know I am repeating what Lamprey said, but don't group by orderdate, instead group by year(orderdate). That would give you one row per product, per year. The only way that you will still be getting multiple rows is if the prodname is different in each row (perhaps because of hidden characters)
Select count(Prodname) ProdCount, ProdName, year(orderdate) year
from products
inner join orders on prodnum = orderprodnum 
group by prodname, year(orderdate)
Order By Prodname, year(orderdate)

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.33 seconds. Powered By: Snitz Forums 2000