Solved

Query syntax - PIVOT?

Posted on 2013-11-19
8
209 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ms sql stored procedure 22 77
how to fix this error 14 46
replication - alerts? 4 23
Script to randomly create characters in MS SQL 6 18
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

21 Experts available now in Live!

Get 1:1 Help Now