T-SQL Query question

I have a table, MatrixRates.  The following select statement returns 50 rows containig 5 columns.  
Selectdaysoutstanding,purchases,netrate from matrixrates
where ratecode = '0.9 0 100' order by daysoutstanding

10      0      6.90
10      10      3.90
10      25      2.90
10      50      1.90
10      100      1.80
20      0      7.80
20      10      4.80
20      25      3.80
20      50      2.80
20      100      1.80
30      0      8.70
30      10      5.70
30      25      4.70
30      50      3.70
30      100      2.70
40      0      9.00
40      10      6.60
40      25      5.60
40      50      4.60
40      100      3.60
50      0      9.0
50      10      7.50
50      25      6.50
50      50      5.50
50      100      4.50
60      0      9.0
60      10      8.40
60      25      7.40
60      50      6.40
60      100      5.40
70      0      9.0
70      10      9.0
70      25      8.30
70      50      7.30
70      100      6.30
80      0      9.0
80      10      9.0
80      25      9.00
80      50      8.20
80      100      7.20
90      0      9.0
90      10      9.0
90      25      9.0
90      50      9.00
90      100      8.10
100      0      9.0
100      10      9.0
100      50      9.0
100      100      9.00
100      25      9.0


Is there any way to present this data in a table of 10 rows, reflecting the 10 distinct daysoutstanding by 5

columns containing netrate data ,reflecting the 5 distinct purchases the netrate.

example:

          0     10    25    50   100  
10   6.90 3.90 2.90 1.90 1.80
20   7.80 4.80 3.80 2.80 1.80
30   8.70 5.70 4.70 3.70 2.70
40   9.00 6.60 5.60 4.60 3.60
50    9.00 7.50 6.50 5.50 4.50
60    9.00 8.40 7.40 6.40 5.40
70    9.00 9.00 8.30 7.30 6.30
80    9.00 9.00 9.00 8.20 7.20
90    9.00 9.00 9.00 9.00 8.10
100  9.00 9.00 9.00 9.00 9.00

daforgaAsked:
Who is Participating?
 
Kevin CrossChief Technology OfficerCommented:
You can use conditional aggregates to accomplish this with a group by on the daysoutstanding column.


select daysoutstanding
     , sum(case purchases when 0 then netrate end) as [0]
     , sum(case purchases when 10 then netrate end) as [10]
     , sum(case purchases when 25 then netrate end) as [25]
     , sum(case purchases when 50 then netrate end) as [50]
     , sum(case purchases when 100 then netrate end) as [100]
from matrixrates where ratecode = '0.9 0 100' group by daysoutstandingorder by daysoutstanding;

HTH

Kevin
0
 
vipin_nagarroCommented:
Hi
 
Its very simple. Tsql provide Pivoting for this.
Make sure to use it in SQL server 2005. This is not available in 2000.
 
Thanks and Regards
Vipin Goel

SELECT *
FROM table
PIVOT
(
  MAX(col3) 
  FOR col2 IN ([0],[10],[25],[50],[100])
)
AS p

Open in new window

0
 
vipin_nagarroCommented:
Again, to achieve it dynamically use following.
DECLARE @columns VARCHAR(8000)

SELECT @columns = COALESCE(@columns + ',[' + cast(col2 as varchar) + ']',
'[' + cast(col2 as varchar)+ ']')
FROM table
GROUP BY col2

DECLARE @query VARCHAR(8000)

SET @query = '
SELECT *
FROM table
PIVOT
(
MAX(col3)
FOR [col2]
IN (' + @columns + ')
)
AS p'

EXECUTE(@query)

Open in new window

0
 
daforgaAuthor Commented:
Thanks, but I forgot to say that my company is using SQL 2000.  Is there any way this can be done in 2000?
0
 
daforgaAuthor Commented:
Thanks to all for the informative answers.  I am sorry about my omission of the version being used.  Kevin's solution worked for me.
Dave
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.