Solved

MS SQL Question transpose

Posted on 2013-05-16
5
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 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