Solved

Mix of group and transpose using SQL

Posted on 2013-11-18
1
768 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Introduction Earlier I wrote an article about the new lookup functions (http://www.experts-exchange.com/A_3433.html) that ship with SQL Server 2008 R2.  In this article I’m going to show you another new feature of SSRS 2008 R2, this time in the vis…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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