Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

T-SQL Query question

Posted on 2009-12-20
5
Medium Priority
?
223 Views
Last Modified: 2012-05-08
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

0
Comment
Question by:daforga
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:vipin_nagarro
ID: 26091796
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
 
LVL 3

Expert Comment

by:vipin_nagarro
ID: 26091804
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
 

Author Comment

by:daforga
ID: 26097675
Thanks, but I forgot to say that my company is using SQL 2000.  Is there any way this can be done in 2000?
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 26098776
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
 

Author Comment

by:daforga
ID: 26098977
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

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question