[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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             ...            ...              ...
0
HelpdeskJBC
Asked:
HelpdeskJBC
  • 2
  • 2
1 Solution
 
Ioannis ParaskevopoulosCommented:
Hi,

Check this:
select * 
from 
(select [Date],[Name],[Price]
from dataview) p
pivot 
(
SUM (Price)
FOR [Name] IN
( [NameA], [NameB], [NameC] )
) AS pvt

Open in new window


SQLFiddle

Giannis
0
 
Patrick MatthewsCommented:
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
0
 
HelpdeskJBCAuthor Commented:
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?
0
 
Ioannis ParaskevopoulosCommented:
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
0
 
Patrick MatthewsCommented:
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

0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now