Solved

Query syntax - PIVOT?

Posted on 2013-11-19
8
211 Views
Last Modified: 2013-11-19
Hello,

I have a tabled defined as:

CREATE TABLE [dbo].[DocumentData](
	[doc_id] [int] NOT NULL,
	[ddt_FieldName] [nvarchar](20) NULL,
	[ddt_FieldValue] [nvarchar](100) NULL,
	[ddt_Changed] [bit] NULL,
	[ddt_ChangedUserId] [int] NULL,
	[ddt_ChangedDateTime] [datetime] NULL,
	[id] [int] IDENTITY(1,1) NOT NULL
) ON [PRIMARY]

Open in new window


It contains data collected from a document - identified by doc_id.  

For each document there are many rows where each row is a field name and a field value.

If i know that my document has ddt_FieldNames of A, B, C and D how would i write an sql statement to produce all the data in columns for document with doc_id 6?

I am looking for output like:

doc_id      A      B      C      D
6               10    66   12     13

where A, B, C, and D have respective ddt_FieldValues  of 10, 66, 12 , 13
0
Comment
Question by:soozh
  • 4
  • 3
8 Comments
 
LVL 11

Expert Comment

by:Louis01
ID: 39659016
Q1: I suppose the columns (A-D) comes from ddt_FieldNames?
Q2: Would it always be A, B, C & D? Can there be more names?
Q3: Do you need to sum ddt_FieldValues?
0
 

Author Comment

by:soozh
ID: 39659032
Q1: I suppose the columns (A-D) comes from ddt_FieldNames?
Yes


Q2: Would it always be A, B, C & D? Can there be more names?
No

Q3: Do you need to sum ddt_FieldValues?
No


I just need a row of data for each document.

Thanks
0
 
LVL 11

Expert Comment

by:Louis01
ID: 39659034
Your response of No to Q2:
No, there can be more names or No, it would always be ABC&D?

Q4: What to do when no row is found for ddt_FieldNames?
Q5: What to do when there us more than one of the same value in ddt_FieldNames?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:soozh
ID: 39659041
It should Always be A B C and D

Q4: return null as the value

Q5:  There should only be one row in the table for each document so i expect that i would have to add the document number to the final Query.
0
 
LVL 11

Expert Comment

by:Louis01
ID: 39659050
The simplest way to achieve this would be:
select [doc_id]
     , CASE [ddt_FieldName] WHEN 'A' THEN [ddt_FieldValue] ELSE 0 END as 'A'
     , CASE [ddt_FieldName] WHEN 'B' THEN [ddt_FieldValue] ELSE 0 END as 'B'
     , CASE [ddt_FieldName] WHEN 'C' THEN [ddt_FieldValue] ELSE 0 END as 'C'
     , CASE [ddt_FieldName] WHEN 'D' THEN [ddt_FieldValue] ELSE 0 END as 'D'
  from DocumentData

Open in new window

0
 
LVL 11

Accepted Solution

by:
Louis01 earned 500 total points
ID: 39659057
You could use the PIVOT function as follows:
select [doc_id], [A], [B], [C], [D]
  from (select doc_id, ddt_FieldName, ddt_FieldValue
          from DocumentData) as pt_src
PIVOT (MAX(ddt_FieldValue) FOR ddt_FieldName IN ([A], [B], [C], [D])) as pt

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39659080
No points please.

Louis01, you forgot the max( ... ) and group by
select [doc_id]
     , MAX ( CASE [ddt_FieldName] WHEN 'A' THEN [ddt_FieldValue] ELSE 0 END ) as 'A'
     , MAX ( CASE [ddt_FieldName] WHEN 'B' THEN [ddt_FieldValue] ELSE 0 END ) as 'B'
     , MAX ( CASE [ddt_FieldName] WHEN 'C' THEN [ddt_FieldValue] ELSE 0 END ) as 'C'
     , MAX ( CASE [ddt_FieldName] WHEN 'D' THEN [ddt_FieldValue] ELSE 0 END ) as 'D'
  from DocumentData
group by [doc_id]

Open in new window

I did exactly the same thing myself recently :)
0
 

Author Closing Comment

by:soozh
ID: 39659082
The pivot worked.  The other solution created many rows.  Thanks.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

10 Experts available now in Live!

Get 1:1 Help Now