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
 Not Row to Column But rows+Colyumn to Column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Deon Smit
Starting Member

47 Posts

Posted - 11/24/2008 :  02:49:13  Show Profile  Reply with Quote
Hi all. This is my normal query.


select * from table
order by item

I get. For Example the following.

Item Year Month1 Month2 Month3
---------------------------------------------
abc 2005 1 22 4
abc 2006 45 64 4
abc 2007 99 46 99
bbc 2005 54 64 4
bbc 2006 2 4 77
bbc 2007 46 7 98


I want my records to be returned as follow as possible.

Item | 2005 Month1 | 2005 Month2 | 2005 Month3 | 2006 Month1 | 2006 Month2 |
abc | 1 | 22 | 4 | 45 | 64
bbc | 54 | 64 | 4 | 2 | 4


Please Guide me to get this working.

visakh16
Very Important crosS Applying yaK Herder

India
48106 Posts

Posted - 11/24/2008 :  02:54:49  Show Profile  Reply with Quote
SELECT Item,
SUM(CASE WHEN Year=2005 THEN Month1 ELSE 0 END) AS [2005 Month1],
SUM(CASE WHEN Year=2005 THEN Month2 ELSE 0 END) AS [2005 Month2],
SUM(CASE WHEN Year=2005 THEN Month3 ELSE 0 END) AS [2005 Month3],
SUM(CASE WHEN Year=2006 THEN Month1 ELSE 0 END) AS [2006 Month1],
SUM(CASE WHEN Year=2006 THEN Month2 ELSE 0 END) AS [2006 Month2],
SUM(CASE WHEN Year=2006 THEN Month3 ELSE 0 END) AS [2006 Month3],
... other columns here
FROM Table
GROUP BY Item
Go to Top of Page

Deon Smit
Starting Member

47 Posts

Posted - 11/24/2008 :  03:23:13  Show Profile  Reply with Quote
I will try that.

Year 2008 month 12 is comming and I want the system to add it in the results. I don't want to add the line SUM(CASE WHEN Year=2008 THEN Month12 ELSE 0 END) AS [2008 Month12]. I want a script that detect a new month/year.

Thank you for your help visakh16
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48106 Posts

Posted - 11/24/2008 :  03:35:04  Show Profile  Reply with Quote
are you using sql 2005?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48106 Posts

Posted - 11/24/2008 :  03:35:05  Show Profile  Reply with Quote
are you using sql 2005?
Go to Top of Page

Deon Smit
Starting Member

47 Posts

Posted - 11/24/2008 :  03:39:45  Show Profile  Reply with Quote
No 2000
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48106 Posts

Posted - 11/24/2008 :  03:47:28  Show Profile  Reply with Quote
see this

http://www.sqlteam.com/article/dynamic-cross-tabs-pivot-tables
Go to Top of Page

Deon Smit
Starting Member

47 Posts

Posted - 11/24/2008 :  04:49:32  Show Profile  Reply with Quote
How do you write it when using SQL 2005
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48106 Posts

Posted - 11/24/2008 :  04:56:39  Show Profile  Reply with Quote
sql 2005 has PIVOT operator which makes this easier.
Go to Top of Page

Deon Smit
Starting Member

47 Posts

Posted - 11/24/2008 :  05:11:27  Show Profile  Reply with Quote
can you give me an example please. I have got a different databse on SQL 2005. I can't test it on there.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48106 Posts

Posted - 11/24/2008 :  05:18:21  Show Profile  Reply with Quote
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx
Go to Top of Page

Nageswar9
Aged Yak Warrior

India
600 Posts

Posted - 11/27/2008 :  02:04:57  Show Profile  Reply with Quote
Select item,
sum( case year when 2005 then month1 else 0 end),
sum( case year when 2005 then month2 else 0 end),
sum( case year when 2005 then month3 else 0 end),
sum( case year when 2006 then month1 else 0 end) ,
sum( case year when 2006 then month2 else 0 end)
from Items
Group By item

I Struggle For Excellence
Go to Top of Page

bklr
Flowing Fount of Yak Knowledge

India
1693 Posts

Posted - 11/27/2008 :  02:05:50  Show Profile  Reply with Quote
SELECT pvt.item ,pvt.[2005] AS '2005month1', pvt1.[2005] AS '2005month2',pvt2.[2005] AS '2005month3',pvt3.[2006] AS '2006month1',pvt4.[2006] AS '2006month2',pvt5.[2006] AS '2006month3'
FROM
(SELECT item,year,Month1,Month2,Month3 FROM items)M
PIVOT (MAX(month1) FOR year IN
([2005] ))AS PVT
INNER JOIN
(SELECT item,year,Month1,Month2,Month3 FROM items)M
PIVOT (MAX(month2) FOR year IN
([2005] ))AS PVT1 ON pvt.item = pvt1.item
INNER JOIN
(SELECT item,year,Month1,Month2,Month3 FROM items)M
PIVOT (MAX(month3) FOR year IN
([2005] ))AS PVT2 ON pvt2.item = pvt1.item
INNER JOIN
(SELECT item,year,Month1,Month2,Month3 FROM items)M
PIVOT (MAX(month1) FOR year IN
([2006] ))AS PVT3 ON pvt3.item = pvt1.item
INNER JOIN
(SELECT item,year,Month1,Month2,Month3 FROM items)M
PIVOT (MAX(month2) FOR year IN
([2006] ))AS PVT4 ON pvt4.item = pvt1.item
INNER JOIN
(SELECT item,year,Month1,Month2,Month3 FROM items)M
PIVOT (MAX(month3) FOR year IN
([2006] ))AS PVT5 ON pvt5.item = pvt1.item
WHERE pvt.[2005] IS NOT NULL AND pvt1.[2005] IS NOT NULL AND PVT2.[2005] IS NOT NULL
AND pvt3.[2006] IS NOT NULL AND pvt4.[2006] IS NOT NULL AND PVT5.[2006] IS NOT NULL

By Using Pivot
Try it in SQL 2005
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

India
333 Posts

Posted - 11/27/2008 :  02:07:16  Show Profile  Reply with Quote
SELECT item,
SUM(CASE year WHEN 2005 THEN month1 ELSE 0 END) AS '2005 month1',
SUM(CASE year WHEN 2005 THEN month2 ELSE 0 END) AS '2005 month2',
SUM(CASE year WHEN 2005 THEN month3 ELSE 0 END) AS '2005 month3',
SUM(CASE year WHEN 2006 THEN month1 ELSE 0 END) AS '2006 month1',
SUM(CASE year WHEN 2006 THEN month2 ELSE 0 END) AS '2006 month2',
SUM(CASE year WHEN 2006 THEN month3 ELSE 0 END) AS '2006 month3',
SUM(CASE year WHEN 2007 THEN month1 ELSE 0 END) AS '2007 month1',
SUM(CASE year WHEN 2007 THEN month2 ELSE 0 END) AS '2007 month2',
SUM(CASE year WHEN 2007 THEN month3 ELSE 0 END) AS '2007 month3'
FROM items
GROUP BY item

Edited by - Jai Krishna on 11/27/2008 02:24:06
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22469 Posts

Posted - 11/27/2008 :  02:30:20  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by Nageswar9

Select item,
sum( case year when 2005 then month1 else 0 end),
sum( case year when 2005 then month2 else 0 end),
sum( case year when 2005 then month3 else 0 end),
sum( case year when 2006 then month1 else 0 end) ,
sum( case year when 2006 then month2 else 0 end)
from Items
Group By item

I Struggle For Excellence


Visakh suggested this. You just removed column names

Madhivanan

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

madhivanan
Premature Yak Congratulator

India
22469 Posts

Posted - 11/27/2008 :  02:32:50  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by Jai Krishna

SELECT item,
SUM(CASE year WHEN 2005 THEN month1 ELSE 0 END) AS '2005 month1',
SUM(CASE year WHEN 2005 THEN month2 ELSE 0 END) AS '2005 month2',
SUM(CASE year WHEN 2005 THEN month3 ELSE 0 END) AS '2005 month3',
SUM(CASE year WHEN 2006 THEN month1 ELSE 0 END) AS '2006 month1',
SUM(CASE year WHEN 2006 THEN month2 ELSE 0 END) AS '2006 month2',
SUM(CASE year WHEN 2006 THEN month3 ELSE 0 END) AS '2006 month3',
SUM(CASE year WHEN 2007 THEN month1 ELSE 0 END) AS '2007 month1',
SUM(CASE year WHEN 2007 THEN month2 ELSE 0 END) AS '2007 month2',
SUM(CASE year WHEN 2007 THEN month3 ELSE 0 END) AS '2007 month3'
FROM items
GROUP BY item


Dont use single quotes around column names. Use [ and ] instead

Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

India
48106 Posts

Posted - 11/27/2008 :  02:33:26  Show Profile  Reply with Quote
quote:
Originally posted by madhivanan

quote:
Originally posted by Nageswar9

Select item,
sum( case year when 2005 then month1 else 0 end),
sum( case year when 2005 then month2 else 0 end),
sum( case year when 2005 then month3 else 0 end),
sum( case year when 2006 then month1 else 0 end) ,
sum( case year when 2006 then month2 else 0 end)
from Items
Group By item

I Struggle For Excellence


Visakh suggested this. You just removed column names

Madhivanan

Failing to plan is Planning to fail


May be he believed its a new suggestion altogether
Go to Top of Page

Deon Smit
Starting Member

47 Posts

Posted - 11/28/2008 :  08:59:48  Show Profile  Reply with Quote
It works very cool. But I must ask the following. I have got Year 2005 to 2008 and month 1 to 12. Next year the database will add 2009. Is there a way to make this dynamic? I mean that I don't have to enter the year 2009 lines there?

Regarsd
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48106 Posts

Posted - 11/28/2008 :  09:42:11  Show Profile  Reply with Quote
quote:
Originally posted by Deon Smit

It works very cool. But I must ask the following. I have got Year 2005 to 2008 and month 1 to 12. Next year the database will add 2009. Is there a way to make this dynamic? I mean that I don't have to enter the year 2009 lines there?

Regarsd


http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx
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.12 seconds. Powered By: Snitz Forums 2000