Link to home
Start Free TrialLog in
Avatar of HelpdeskJBC
HelpdeskJBCFlag for Austria

asked on

MS SQL Question transpose

Hello I've just got that table:
select * from dataview

ID     Price   Date              Name
1       12        1.1.2012      NameA
1       11        1.1.2012      NameB
1       19        1.1.2012      NameC
1       75        2.1.2012      NameA

And I need to get the SQL Select result to show up like:

Date             NameA    NameB   NameC    NameD
1.1.2012      12             11           19             ...
2.1.2012      75             ...            ...              ...
ASKER CERTIFIED SOLUTION
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
HelpdeskJBC,

1) Is the list of NameA, NameB, NameC, etc. fixed, known ahead of time, and reasonably short?  Or does the query have to automatically "discover" those values?

2) How does the ID column come into play?

Patrick
Avatar of HelpdeskJBC

ASKER

thanks looks good
one more question if i would like to select only top 100 and order by date how to i change the result to order it one more time by the date?
If i choose top 100 and order by date desc then ive got the newest entry date on top, but i yould prefer to see the last of this top 100 dates on top of the table?
I've already done this on a small sql select with another select is it the same way here ore are there other options?
so do I need to make one more select from your whole code? --> nested select?
It depends on what you need....

If you want the top 100 rows of the table and then pivot them then you should:

select * 
from 
(select top 100 [Date],[Name],[Price]
from dataview) p
pivot 
(
SUM (Price)
FOR [Name] IN
( [NameA], [NameB], [NameC] )
) AS pvt
order by [Date]

Open in new window


if you want to pivot everything and get the top 100 dates then:

select  top 100 * 
from 
(select [Date],[Name],[Price]
from dataview) p
pivot 
(
SUM (Price)
FOR [Name] IN
( [NameA], [NameB], [NameC] )
) AS pvt
order by [Date]

Open in new window


Giannis
The following will automatically "discover" the various names:


CREATE TABLE dataview (ID int, Price money, [Date] datetime, [Name] varchar(50))

INSERT INTO dataview (ID, Price, [Date], [Name]) VALUES
(1,       12,        '2012-01-01',      'NameA'),
(1,       11,        '2012-01-01',      'NameB'),
(1,       19,        '2012-01-01',      'NameC'),
(1,       75,        '2012-02-01',      'NameA'),
(2,       22,        '2012-01-01',      'NameA'),
(2,       21,        '2012-01-01',      'NameB'),
(2,       29,        '2012-01-01',      'NameD')

DECLARE @sql varchar(MAX)

SET @sql = 'SELECT t1.ID, t1.[Date]'

SELECT @sql = @sql + ', (SELECT MAX(t2.Price) 
    FROM dataview t2 
    WHERE t2.ID = t1.ID AND t2.[Date] = t1.[Date] AND t2.[Name] = ''' + [Name] + ''') AS [' + [Name] + ']'
FROM dataview
GROUP BY [Name]
ORDER BY [Name]

SET @sql = @sql + ' FROM dataview t1 GROUP BY t1.ID, t1.[Date] ORDER BY t1.ID, t1.[Date]'

EXEC(@sql)

DROP TABLE dataview

Open in new window