Solved

Mix of group and transpose using SQL

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Introduction In the following article I’ll be discussing and demonstrating several different ways of how images can be put on a report. I’m using SQL Server Reporting Services 2008 R2 CTP, more precisely version 10.50.1352.12, but the methods ex…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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