?
Solved

MS SQL Question transpose

Posted on 2013-05-16
5
Medium Priority
?
198 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 2000 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 93

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 93

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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…
Suggested Courses

752 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