Solved

MS SQL Question transpose

Posted on 2013-05-16
5
179 Views
Last Modified: 2013-05-28
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
Comment
Question by:HelpdeskJBC
  • 2
  • 2
5 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 39171069
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
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39171164
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
 

Author Comment

by:HelpdeskJBC
ID: 39171195
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
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39171214
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
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39171217
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now