Solved

Mix of group and transpose using SQL

Posted on 2013-11-18
1
794 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
[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
1 Comment
 
LVL 25

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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