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
 I have No Idea how to do this

Author  Topic 

noyellatmonkeys
Starting Member

15 Posts

Posted - 2009-12-03 : 16:07:55
Any Help would be extremely usefull!!!

I have a table that looks like this

id page traffic month year
1 home 1000 11 09
2 books 800 11 09
3 home 1200 10 09
4 books 750 10 09

what i want is to be able to see the info as follows

______10 09 | 11 09
home 1200 1000
books 750 800

Any suggestions?

X002548
Not Just a Number

15586 Posts

Posted - 2009-12-03 : 16:09:53
quote:
Originally posted by noyellatmonkeys


id page traffic month year
1 home 1000 11 09
2 books 800 11 09
3 home 1200 10 09
4 books 750 10 09



what i want is to be able to see the info as follows

10 09 | 11 09
home 1200 1000
books 750 800


Any suggestions?



What Version of SQL Server are you on?

We need to look up PIVOT probably....

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

noyellatmonkeys
Starting Member

15 Posts

Posted - 2009-12-03 : 16:59:19
sql server express 2008
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-12-04 : 00:36:43
Hi try this,

DECLARE @temp TABLE (id INT, page VARCHAR(32),traffic INT,months VARCHAR(32), years VARCHAR(32) )
INSERT INTO @temp SELECT 1, 'home', 1000, 11, 09
INSERT INTO @temp SELECT 2, 'books', 800, 11, 09
INSERT INTO @temp SELECT 3, 'home', 1200, 10, 09
INSERT INTO @temp SELECT 4, 'books', 750, 10, 09

SELECT page, MAX([10_9]) AS [10_9],MAX([11_9]) AS [11_9]
FROM ( SELECT id,page,traffic,months+'_'+years AS monthsss FROM @temp ) t
PIVOT ( MAX(traffic) FOR monthsss IN ( [10_9],[11_9] ))p
GROUP BY page
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-04 : 01:50:03
If the values are not fixed,
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

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

- Advertisement -