Solved

Mix of group and transpose using SQL

Posted on 2013-11-18
1
719 Views
Last Modified: 2013-11-22
Hi SQL gurus,

I'm looking to create a query that will do the following: I have a table below

ID	ItemID	Column Name	ColumnValue	DataSource	DateModified
1	A1B2	Name		Test		SP		11/18/2013 20:00
1	A1B2	Number		1		SP		11/18/2013 20:00
1	A1B2	Type		local		SP		11/18/2013 20:00
1	A1B2	Date		11/1/2013 20:00	SP		11/18/2013 20:00
1	A1B2	YesNo		Yes		SP		11/18/2013 20:00
2	C3D4	Name		Test2		SP		11/18/2013 20:00
2	C3D4	Number		2		SP		11/18/2013 20:00
2	C3D4	Type		global		SP		11/18/2013 20:00
2	C3D4	Date		10/1/2013 20:00	SP		11/18/2013 20:00
2	C3D4	YesNo		No		SP		11/18/2013 20:00
3	E5F6	Name		Test3		SP		11/18/2013 20:00
3	E5F6	Number		3		SP		11/18/2013 20:00
3	E5F6	Type		local		SP		11/18/2013 20:00
3	E5F6	Date		9/1/2013 20:00	SP		11/18/2013 20:00
3	E5F6	YesNo		Yes		SP		11/18/2013 20:00

Open in new window


And I'm looking to use a SQL query to display the following:

ID	ItemID	Name	Number	Type	Date		YesNo	DataSource	DateModified
1	A1B2	Test	1	local	11/1/2013 20:00	Yes	SP		11/18/2013 20:00
2	C3D4	Test2	2	global	10/1/2013 20:00	No	SP		11/18/2013 20:00
3	E5F6	Test3	3	local	9/1/2013 20:00	Yes	SP		11/18/2013 20:00

Open in new window


Where i'm grouping by ID and ItemID but transposing Name, Number, Type, Date and YesNo. DataSource and DateModified will also be 1 to 1 with the ID. Name, Number, Type, Date and YesNo will exist for each record.

Can anyone help out?

Thanks
0
Comment
Question by:MAVSS
1 Comment
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39658142
You need to use PIVOT for this:
SELECT [ID], ItemID,
[Name], [Number], [Type], [Date], [YesNo],
[DataSource], [DateModified]
FROM
(SELECT [ID], ItemID, [ColumnName], [ColumnValue],
    [DataSource], [DateModified]
    FROM Table1) AS SourceTable
PIVOT
(
MAX([ColumnValue])
FOR [ColumnName] IN ([Name], [Number], [Type], [Date], [YesNo])
) AS PivotTable

Open in new window

Result:
| ID | ITEMID |  NAME | NUMBER |   TYPE |            DATE | YESNO | DATASOURCE |                    DATEMODIFIED |
|----|--------|-------|--------|--------|-----------------|-------|------------|---------------------------------|
|  1 |   A1B2 |  Test |      1 |  local | 11/1/2013 20:00 |   Yes |         SP | November, 19 2013 07:00:00+0000 |
|  2 |   C3D4 | Test2 |      2 | global | 10/1/2013 20:00 |    No |         SP | November, 19 2013 07:00:00+0000 |
|  3 |   E5F6 | Test3 |      3 |  local |  9/1/2013 20:00 |   Yes |         SP | November, 19 2013 07:00:00+0000 |

Open in new window

SQL Fiddle
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Hi, I have heard from my friends that it’s not possible to create Label Printing report using SSRS. I am amazed after hearing this words not possible in SSRS. I googled lot and found that it is possible to some of people know about the Report Bui…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

707 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

15 Experts available now in Live!

Get 1:1 Help Now