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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Query by months

Author  Topic 

Dragomir
Starting Member

4 Posts

Posted - 2006-11-21 : 10:26:25
Hello everybody,

I have a problem.

I want to make query that return quatities of diferent car makes month by month in the year.

Example:
Audi 1 2 3 4 5 6 7 8 9 10 11 12
BMW 1 2 3 4 5 6 7 8 9 10 11 12
...
Can anyone help me

Thx

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-21 : 10:33:41
select cartypecolumn,
sum(case when monthvalue = 1 then amountsoldcolumn else 0 end) 'January',
sum(case when monthvalue = 2 then amountsoldcolumn else 0 end) 'February',
sum(case when monthvalue = 3 then amountsoldcolumn else 0 end) 'March',
sum(case when monthvalue = 4 then amountsoldcolumn else 0 end) 'April',
sum(case when monthvalue = 5 then amountsoldcolumn else 0 end) 'May',
sum(case when monthvalue = 6 then amountsoldcolumn else 0 end) 'June',
sum(case when monthvalue = 7 then amountsoldcolumn else 0 end) 'July',
sum(case when monthvalue = 8 then amountsoldcolumn else 0 end) 'August',
sum(case when monthvalue = 9 then amountsoldcolumn else 0 end) 'September',
sum(case when monthvalue = 10 then amountsoldcolumn else 0 end) 'October',
sum(case when monthvalue = 11 then amountsoldcolumn else 0 end) 'November',
sum(case when monthvalue = 12 then amountsoldcolumn else 0 end) 'December'
from yourtable
group by cartypecolumn
order by cartypecolumn


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Dragomir
Starting Member

4 Posts

Posted - 2006-11-21 : 10:58:14
That was exactly what I needed.

Thank you very much
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-11-21 : 11:29:13
Also read about Cross-tab reports in sql server help file for more informations

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2006-11-21 : 14:52:48
I'm liking PIVOT on 2K5 for these types of things ...



Jay White
Go to Top of Page

graz
Chief SQLTeam Crack Dealer

4149 Posts

Posted - 2006-11-21 : 16:35:44
(Moved to the Transact-SQL forum from the Articles forum)

===============================================
Creating tomorrow's legacy systems today.
One crisis at a time.
Go to Top of Page
   

- Advertisement -