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.
Author |
Topic |
new_bees
Starting Member
27 Posts |
Posted - 2008-09-15 : 12:13:06
|
Hi Guys,It's been a little while since my last post. I'm working on my personal project and I got stucked on something. I want to do a crosstab/matrix style query. Below is the data. Note: there is no primary key in the tableDATA:QUARTER | YEAR | PERCENTAGE4 2007 673 2008 731 2008 522 2008 343 2008 31THIS IS THE RESULT THAT I WANTED TO LOOK LIKE.QUARTER | YEAR | PERCENTAGE1 | PERCENTAGE24 2007 67 NULL1 2008 NULL 522 2008 73 343 | 2008 | NULL | 31This is my SQL query.SELECT QUARTER, YEAR, PERCENTAGE FROM QuarterI tried to follow the tutoria posted in the article section, but no luck.I hope someone here can help me. Thanks in advanced. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-15 : 12:30:17
|
your sample output does not make much sense. can you explain how you think the output will be obtained in words? |
 |
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-09-17 : 06:12:23
|
Hi,Try with thisCreate Table #Table ( Id Int, Quarter Int, YEAR Int, PERCENTAGE Int)Insert into #Table (Quarter , YEAR , PERCENTAGE)Select 4, 2007, 67 Union AllSelect 3, 2008, 73 Union AllSelect 1, 2008, 52 Union AllSelect 2, 2008, 34 Union AllSelect 3, 2008, 31UPDATE T1SET Id = (SELECT COUNT(*) FROM #Table T WHERE T.Quarter = T1.Quarter AND T.YEAR = T1.YEAR AND T.PERCENTAGE <= T1.PERCENTAGE )FROM #Table T1Declare @i int, @str varchar(8000)Select @i = min(id) From #table Group By year Select @str = ''While ( @I is not null)Begin Select @str = @str + ', Percentage' + cast (@i as varchar(10) ) + ' = min(case when id = ' + cast (@i as varchar(10) ) + ' then PERCENTAGE end)' Select @i = min(id) From #table where id > @i EndSelect @str = 'select Quarter, Year ' + @str + 'from #table group by Quarter, year'Print @strExec (@str)drop table #table |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-17 : 06:31:14
|
quote: Originally posted by ranganath Hi,Try with thisCreate Table #Table ( Id Int, Quarter Int, YEAR Int, PERCENTAGE Int)Insert into #Table (Quarter , YEAR , PERCENTAGE)Select 4, 2007, 67 Union AllSelect 3, 2008, 73 Union AllSelect 1, 2008, 52 Union AllSelect 2, 2008, 34 Union AllSelect 3, 2008, 31UPDATE T1SET Id = (SELECT COUNT(*) FROM #Table T WHERE T.Quarter = T1.Quarter AND T.YEAR = T1.YEAR AND T.PERCENTAGE <= T1.PERCENTAGE )FROM #Table T1Declare @i int, @str varchar(8000)Select @i = min(id) From #table Group By year Select @str = ''While ( @I is not null)Begin Select @str = @str + ', Percentage' + cast (@i as varchar(10) ) + ' = min(case when id = ' + cast (@i as varchar(10) ) + ' then PERCENTAGE end)' Select @i = min(id) From #table where id > @i EndSelect @str = 'select Quarter, Year ' + @str + 'from #table group by Quarter, year'Print @strExec (@str)drop table #table
have you checked the sample output posted by OP. your query above gives below outputQuarter Year Percentage1 Percentage2----------- ----------- ----------- -----------4 2007 67 NULL1 2008 52 NULL2 2008 34 NULL3 2008 31 73 this is different from what OP asked for |
 |
|
|
|
|
|
|