Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Mix of group and transpose using SQL

Posted on 2013-11-18
1
Medium Priority
?
823 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

597 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