SQL Server - Dynamic Pivot Query [duplicate] - sql

This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
Closed 7 years ago.
I've been reading the various examples on this board but haven't been able to whip up a dynamic pivot query to fit my code. Any help would be appreciated!
I have two tables
AuxDef
Fieldnumber Fieldlabel
------------------------
1 Buyer
2 Size Range
3 Source
4 Country
5 Vendor
ect... ect...
AuxFields
PageID FieldNumber TextValue
-----------------------------------
1 1 Sam
1 2 S-L
1 3 Domestic
1 4 Canada
2 1 Kyla
2 3 Import
2 5 VendorName
2 6 Off-Shore
2 7 Fit 1
2 8 Yes
4 1 Sara
4 3 Import
4 4 China
ect.. ect.. ect..
What I would like to do is create a dynamic pivot that joins the two tables by fieldnumber and uses the fieldlabels as the column headers after pageid. It would look similar to this.
PageID Buyer Size Range Source Country Vendor Type Status Exclusive ect..
------------------------------------------------------------------------------------------------
1 Sam S-L Domestic Canada
2 Kyla Import VendorName Off-Shore Fit 1 Yes
4 Sara Import China
I've tried examples on this site but I keep running into errors either when I replace the fields with my own or in the actual column generation.

You will need to create a dynamic pivot (credit to #bluefeet's answer here) something like this:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX);
SET #cols = STUFF((SELECT distinct ',' + QUOTENAME(c.FieldLabel)
FROM auxdef c
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT PageID, ' + #cols + ' from
(
select
PageId,
FieldLabel,
TextValue
from AuxFields af
inner join auxdef ad on af.FieldNumber = ad.FieldNumber
) x
pivot
(
max(TextValue)
for FieldLabel in (' + #cols + ')
) p '
execute(#query);
SQL Fiddle Demo

Here's another way
sql fiddle
declare #sql nvarchar(max)
select #sql = 'select af.pageId'
select #sql = #sql + ', max(case when ad.fieldLabel = ''' + ad.fieldLabel + ''' then af.textValue end) as [' + ad.fieldLabel + ']' + char(13)
from auxDef ad
select #sql = #sql + 'from auxDef ad' + char(13)
+ 'inner join auxFields af on af.fieldNumber = ad.fieldNumber' + char(13)
+ 'group by af.pageId'
exec(#sql)

Related

Data from questions and responses associated to an customer

I have the below data format in my tables, a customer answers test questions and those responses are saved in a table. Questions are grouped as sections. I am trying to get responses associated to a test and for certain sections.
Sectiontable
SecID SecName
1 Box
2 square
3 circle
CustomerTable
CID CName
92 John
93 Andrew
94 Chris
TestTable
testID testkey SecID
18 T1 1
19 T11 1
21 T2 2
22 T21 2
34 T3 3
35 T4 3
CustomerTestresponse
responseID CID testID responseText
1 92 18 T1Text
2 92 19 T11Text
3 92 34 T3Text
4 92 35 T4Text
5 92 22 T21Text
6 93 19 Myresponse
7 93 34 vendor
8 93 21 cutomerout
Expected Query output:
CID T1KeyResponse T11KeyResponse T3KeyResponse T4KeyResponse
92 T1Text T11Text T3Text T4Text
93 Myresponse vendor
It is a FOUR (4) step solution
-- adapted with great reverence from https://dba.stackexchange.com/questions/119844/how-to-pivot-without-fixed-columns-in-tsql
--(1) Declare variables
DECLARE
#cols AS NVARCHAR(MAX)
,#query AS NVARCHAR(MAX)
;
--(2) Concatenate list of new column headers, using FOR XML PATH and STUFF:
-- see Where SecID <> 2 (you may want to change this)
SELECT #cols =
STUFF(
(SELECT DISTINCT
',' + QUOTENAME(t.testkey + 'KeyResponse')
FROM qandrResponse as r
Left Join qandrTest as t
On r.testID = t.testID
Where SecID <> 2 -- you may want to filter differently
Order By ',' + QUOTENAME(t.testkey + 'KeyResponse')
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,''
);
-- Select #cols; -- uncomment to see the list, drag right side open to see full list
--(3) Create the full sql string that will be executed, using #cols from above
set #query
= N'SELECT CID, ' + #cols + N'
From (SELECT
r.CID
,t.testkey + ''' + 'KeyResponse' + N''' as Keys
,responseText
FROM qandrResponse as r
Left Join qandrTest as t
On r.testID = t.testID
) x
Pivot (
max(responseText) for Keys IN (' + #cols + N')
) as p';
-- Select #query; -- uncomment to see sql, prettyprint with http://www.dpriver.com/pp/sqlformat.htm
--(4) Execute the query
exec sp_executesql #query
-- Results
--CID T11KeyResponse T1KeyResponse T3KeyResponse T4KeyResponse
--92 T11Text T1Text T3Text T4Text
--93 Myresponse NULL vendor NULL

SQL Pivot - Dynamic Columns, No Aggregation

I'm trying to do a Pivot, but I'm not very experienced with pivots and I'm stuck - I can't figure out how to structure the query.
What I have:
Data Types (types of measurements that are recorded)
Locations
Data Sources (things at each location that will be measured)
Data Readings (measurements of the sources)
Additional information:
The number of Sources at any one Location can change
There will never be more than 5 sources at a single Location
Only 1 Reading is saved per Source/Type/date
In the returned table:
Table shows Data_Type info and Readings for a single Location and date
Columns: Data_Name, Units, Is_Required (from Data_Type table), plus one column for each Source
Rows: one row for each Data_Type
Rows should be ordered by Type_Display_Order
Sources (extra columns) should be ordered by Source_Display_Order
Some Readings are optional, and some Sources aren't measured daily - these still need to be included in the table
Example:
Table: Data_Type
Data_Type_ID Data_Name Units Is_Required (BIT) Type_Display_Order
-----------------------------------------------------------------------
1 Height In. 1 2
2 Length In. 0 3
3 Weight Lbs. 1 1
Table: Location
Location_ID Location
-----------------------
1 West
2 East
Table: Data_Source
Data_Source_ID Location_ID Source_Name Source_Display_Order
----------------------------------------------------------------
1 1 WCS 2
2 2 ECS 1
3 1 WBN 1
Table: Data_Reading
Data_Reading_ID Data_Type_ID Data_Source_ID Reading Reading_Date
----------------------------------------------------------------------
1 1 1 5 6/3/2016
2 3 2 3 5/1/2016
3 1 1 7 5/1/2016
4 2 3 2 6/3/2016
5 3 1 4 6/3/2016
Desired results from query for Location = "West", Date = 6/3/2016:
Data_Type_ID Data_Name Units Is_Required WBN WCS
---------------------------------------------------------
3 Weight Lbs. 1 NULL 4
1 Height In. 1 NULL 5
2 Length In. 0 NULL NULL
This solution seems to be similar: Pivot Dynamic Columns, no Aggregation but I'm still having some problems.
This is what I have so far:
DECLARE #date DATE, #locationID INT
SET #date = CAST('6/3/2016' AS DATE)
SET #locationID = 1
DECLARE #cols AS NVARCHAR(MAX), #query AS NVARCHAR(MAX)
SET #cols = STUFF((SELECT distinct ',' + QUOTENAME(s.Source_Name)
FROM Data_Source s
WHERE s.Location_ID = #locationID
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SET #query = 'SELECT Data_Type_ID, Data_Name, Units, Is_Required, ' + #cols +
' FROM
(
SELECT
t.Data_Type_ID
, t.Data_Name
, t.Units
, t.Is_Required
, r.Reading
, s.Source_Name
FROM
Data_Type t
LEFT JOIN
Data_Reading r ON t.Data_Type_ID = r.Data_Type_ID
LEFT JOIN
Data_Source s ON r.Data_Source_ID = s.Data_Source_ID
WHERE
r.Reading_Date = CAST(CAST(' + #date + ' AS NVARCHAR(10)) AS DATE)
AND s.Location_ID = CAST(' + #locationID + ' AS INT)
) x
PIVOT
(
MIN(Reading)
for Source_Name in (' + #cols + ')
) p '
I have the query working properly now, but I still have a few problems:
#cols is not sorted by Source_Display_Order
rows are not sorted by Type_Display_Order (I did have ORDER BY in the inner SELECT statement for part X, but I was getting errors saying I can't have an ORDER BY clause there)
Date comparison in WHERE statement doesn't work - for some reason, it always computes as False, even when the dates are the same
Solved!
DECLARE #date DATE, #locationID INT
SET #date = CAST('6/3/2016' AS DATE)
SET #locationID = 1
DECLARE #cols AS NVARCHAR(MAX), #query AS NVARCHAR(MAX)
SET #cols = STUFF((SELECT ',' + QUOTENAME(s.Source_Name)
FROM Data_Source s
WHERE s.Location_ID = #locationID
ORDER BY s.Source_Display_Order
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SET #query =
'SELECT
Data_Type_ID
, Data_Name
, Units
, Is_Required
, ' + #cols + '
FROM
(
SELECT
t.Data_Type_ID
, t.Data_Name
, t.Units
, t.Is_Required
, r.Reading
, s.Source_Name
, t.Type_Display_Order
FROM
Data_Type t
LEFT JOIN
Data_Reading r ON t.Data_Type_ID = r.Data_Type_ID
LEFT JOIN
Data_Source s ON r.Data_Source_ID = s.Data_Source_ID
WHERE
r.Reading_Date = ''' + CAST(#date AS NVARCHAR(10)) + '''
AND s.Location_ID = ' + CAST(#locationID AS NVARCHAR(20)) + '
) x
PIVOT
(
MIN(Reading)
for Source_Name in (' + #cols + ')
) p
ORDER BY
Type_Display_Order'
EXECUTE(#query)
To fix my problems:
Convert #date to NVARCHAR before adding to #query string and include extra quotes to surround the new NVARCHAR in quotes within #query
Remove DISTINCT clause from #cols and add ORDER BY (all of the names in my table are unique, so the DISTINCT is unnecessary)
Add Type_Display_Order to the inner SELECT statement, and add ORDER BY after the PIVOT statement

How to pivot in SQL SERVER [duplicate]

This question already has answers here:
SQL Server: Examples of PIVOTing String data
(7 answers)
Closed 8 years ago.
I have a table like this:
HospitalName--- DRG--DrgCount
K1 F67A 1
K1 F68A 1
K1 F69A 1
K1 F60A 1
K1 F61A 1
K2 F61A 1
I want to show this in SQL Server like this:
HospitalName F67A F68A F69A F60A F61A
K1 1 1 1 1 1
K2 0 0 0 0 1
I tried the code below :
SELECT TOP 1000 HospitalCode,Durum,[A01Z],[A03Z],[A05Z],[Z99A],[Z98A],[Z97A],[Z96A],[A06Z],[A07Z],[A08A],[A08B],[A09A],[A09B],[A40Z],[A41A],[A41B],[B01Z],[B02A],[B02B],[B02C],[B03A],[B03B],[B04A],[B04B],[B05Z],[B06A],[B06B],[B07A],[B07B],[B40Z],[B41Z],[B60A],[B60B],[B61A],[B61B],[B62Z],[B63Z],[B64A],[B64B],[B65Z],[B66A],[B66B],[B67A],[B67B],[B67C],[B68A],[B68B],[B69A],[B69B],[B70A],[B70B],[B70C],[B70D],[B71A],[B71B],[B72A],[B72B],[B73Z],[B74Z],[B75Z],[B76A],[B76B],[B77Z],[B78A],[B78B],[B79Z],[B80Z],[B81A],[B81B],[C01Z],[C02Z],[C03Z],[C04Z],[C05Z],[C10Z],[C11Z],[C12Z],[C13Z],[C14Z],[C15A],[C15B],[C16A],[C16B],[C60A],[C60B],[C61Z],[C62Z],[C63A],[C63B],[D01Z],[D02A],[D02B],[D02C],[D03Z],[D04A],[D04B],[D05Z],[D06Z],[D09Z],[D10Z],[D11Z],[D12Z],[D13Z],[D14Z],[D40Z],[D60A],[D60B],[D61Z],[D62Z],[D63A],[D63B],[D64Z],[D65Z],[D66A],[D66B],[D67A],[D67B],[E01A],[E01B],[E02A],[E02B],[E02C],[E40Z],[E41Z],[E60A],[E60B],[E61A],[E61B],[E62A],[E62B],[E62C],[E63Z],[E64Z],[E65A],[E65B],[E66A],[E66B],[E66C],[E67A],[E67B],[E68Z],[E69A],[E69B],[E69C],[E70A],[E70B],[E71A],[E71B],[E71C],[E72Z],[E73A],[E73B],[E73C],[E74A],[E74B],[E74C],[E75A],[E75B],[E75C],[F01A],[F01B],[F02Z],[F03Z],[F04A],[F04B],[F05A],[F05B],[F06A],[F06B],[F07A],[F07B],[F08A],[F08B],[F09A],[F09B],[F10Z],[F11A],[F11B],[F12Z],[F13Z],[F14A],[F14B],[F14C],[F15Z],[F16Z],[F17Z],[F18Z],[F19Z],[F20Z],[F21A],[F21B],[F40Z],[F41A],[F41B],[F42A],[F42B],[F60A],[F60B],[F60C],[F61Z],[F62A],[F62B],[F63A],[F63B],[F64Z],[F65A],[F65B],[F66A],[F66B],[F67A],[F67B],[F68Z],[F69A],[F69B],[F70A],[F70B],[F71A],[F71B],[F72A],[F72B],[F73A],[F73B],[F74Z],[F75A],[F75B],[F75C],[G01A],[G01B],[G02A],[G02B],[G03A],[G03B],[G03C],[G04A],[G04B],[G04C],[G05A],[G05B],[G06Z],[G07A],[G07B],[G08A],[G08B],[G09Z],[G10Z],[G11A],[G11B],[G12A],[G12B],[G42A],[G42B],[G43Z],[G44A],[G44B],[G44C],[G45A],[G45B],[G46A],[G46B],[G46C],[G60A],[G60B],[G61A],[G61B],[G62Z],[G63Z],[G64Z],[G65A],[G65B],[G66A],[G66B],[G67A],[G67B],[G68A],[G68B],[G69Z],[G70A],[G70B],[H01A],[H01B],[H02A],[H02B],[H02C],[H05A],[H05B],[H06Z],[H07A],[H07B],[H08A],[H08B],[H40Z],[H41A],[H41B],[H42A],[H42B],[H42C],[H60A],[H60B],[H60C],[H61A],[H61B],[H62A],[H62B],[H63A],[H63B],[H64A],[H64B],[I01Z],[I02A],[I02B],[I03A],[I03B],[I03C],[I04Z],[I05Z],[I06Z],[I07Z],[I08A],[I08B],[I09A],[I09B],[I10A],[I10B],[I11Z],[I12A],[I12B],[I12C],[I13A],[I13B],[I13C],[I14Z],[I15Z],[I16Z],[I17Z],[I18Z],[I19Z],[I20Z],[I21Z],[I23Z],[I24Z],[I25Z],[I27A],[I27B],[I28A],[I28B],[I29Z],[I30Z],[I60Z],[I61Z],[I63Z],[I64A],[I64B],[I65A],[I65B],[I66A],[I66B],[I67A],[I67B],[I68A],[I68B],[I68C],[I69A],[I69B],[I69C],[I70Z],[I71A],[I71B],[I71C],[I72A],[I72B],[I73A],[I73B],[I73C],[I74A],[I74B],[I74C],[I75A],[I75B],[I75C],[I76A],[I76B],[I76C],[I77A],[I77B],[I78A],[I78B],[J01Z],[J06A],[J06B],[J07A],[J07B],[J08A],[J08B],[J09Z],[J10Z],[J11Z],[J12A],[J12B],[J12C],[J13A],[J13B],[J14Z],[J60A],[J60B],[J62A],[J62B],[J63Z],[J64A],[J64B],[J65A],[J65B],[J67A],[J67B],[J68A],[J68B],[K01Z],[K02Z],[K03Z],[K04Z],[K05Z],[K06Z],[K07Z],[K08Z],[K09Z],[K40Z],[K60A],[K60B],[K61Z],[K62A],[K62B],[K62C],[K63Z],[K64A],[K64B],[L02A],[L02B],[L03A],[L03B],[L04A],[L04B],[L04C],[L05A],[L05B],[L06A],[L06B],[L07A],[L07B],[L08A],[L08B],[L09A],[L09B],[L09C],[L40Z],[L41Z],[L42Z],[L60A],[L60B],[L60C],[L61Z],[L62A],[L62B],[L63A],[L63B],[L63C],[L64Z],[L65A],[L65B],[L66Z],[L67A],[L67B],[L67C],[M01Z],[M02A],[M02B],[M03A],[M03B],[M04A],[M04B],[M05Z],[M06A],[M06B],[M40Z],[M60A],[M60B],[M61A],[M61B],[M62A],[M62B],[M63Z],[M64Z],[N01Z],[N02A],[N02B],[N03A],[N03B],[N04Z],[N05A],[N05B],[N06Z],[N07Z],[N08Z],[N09Z],[N10Z],[N11A],[N11B],[N60A],[N60B],[N61Z],[N62A],[N62B],[O01A],[O01B],[O01C],[O02A],[O02B],[O03Z],[O04Z],[O05Z],[O60A],[O60B],[O60C],[O61Z],[O63Z],[O64A],[O64B],[O66A],[O66B],[P01Z],[P02Z],[P03Z],[P04Z],[P05Z],[P06A],[P06B],[P60A],[P60B],[P61Z],[P62Z],[P63Z],[P64Z],[P65A],[P65B],[P65C],[P65D],[P66A],[P66B],[P66C],[P66D],[P67A],[P67B],[P67C],[P67D],[Q01Z],[Q02A],[Q02B],[Q60A],[Q60B],[Q60C],[Q61A],[Q61B],[Q61C],[Q62Z],[R01A],[R01B],[R02A],[R02B],[R03A],[R03B],[R04A],[R04B],[R60A],[R60B],[R60C],[R61A],[R61B],[R61C],[R62A],[R62B],[R63Z],[R64Z],[S60Z],[S65A],[S65B],[S65C],[T01A],[T01B],[T01C],[T60A],[T60B],[T61A],[T61B],[T62A],[T62B],[T63A],[T63B],[T64A],[T64B],[U40Z],[U60Z],[U61A],[U61B],[U62A],[U62B],[U63A],[U63B],[U64Z],[U65Z],[U66Z],[U67Z],[U68Z],[V60A],[V60B],[V61Z],[V62A],[V62B],[V63A],[V63B],[V64Z],[W01Z],[W02Z],[W03Z],[W04Z],[W60Z],[W61Z],[X02Z],[X04A],[X04B],[X05Z],[X06A],[X06B],[X07A],[X07B],[X60A],[X60B],[X60C],[X61Z],[X62A],[X62B],[X63A],[X63B],[X64A],[X64B],[Y01Z],[Y02A],[Y02B],[Y03Z],[Y60Z],[Y61Z],[Y62A],[Y62B],[Z01A],[Z01B],[Z40Z],[Z60A],[Z60B],[Z60C],[Z61Z],[Z62Z],[Z63A],[Z63B],[Z64A],[Z64B],[Z65Z],[901Z],[902Z],[903Z],[960Z],[961Z],[963Z]
FROM
(SELECT * FROM TIGPatient) p
PIVOT
(
COUNT (DRG)
FOR DRG IN
( [A01Z],[A03Z],[A05Z],[Z99A],[Z98A],[Z97A],[Z96A],[A06Z],[A07Z],[A08A],[A08B],[A09A],[A09B],[A40Z],[A41A],[A41B],[B01Z],[B02A],[B02B],[B02C],[B03A],[B03B],[B04A],[B04B],[B05Z],[B06A],[B06B],[B07A],[B07B],[B40Z],[B41Z],[B60A],[B60B],[B61A],[B61B],[B62Z],[B63Z],[B64A],[B64B],[B65Z],[B66A],[B66B],[B67A],[B67B],[B67C],[B68A],[B68B],[B69A],[B69B],[B70A],[B70B],[B70C],[B70D],[B71A],[B71B],[B72A],[B72B],[B73Z],[B74Z],[B75Z],[B76A],[B76B],[B77Z],[B78A],[B78B],[B79Z],[B80Z],[B81A],[B81B],[C01Z],[C02Z],[C03Z],[C04Z],[C05Z],[C10Z],[C11Z],[C12Z],[C13Z],[C14Z],[C15A],[C15B],[C16A],[C16B],[C60A],[C60B],[C61Z],[C62Z],[C63A],[C63B],[D01Z],[D02A],[D02B],[D02C],[D03Z],[D04A],[D04B],[D05Z],[D06Z],[D09Z],[D10Z],[D11Z],[D12Z],[D13Z],[D14Z],[D40Z],[D60A],[D60B],[D61Z],[D62Z],[D63A],[D63B],[D64Z],[D65Z],[D66A],[D66B],[D67A],[D67B],[E01A],[E01B],[E02A],[E02B],[E02C],[E40Z],[E41Z],[E60A],[E60B],[E61A],[E61B],[E62A],[E62B],[E62C],[E63Z],[E64Z],[E65A],[E65B],[E66A],[E66B],[E66C],[E67A],[E67B],[E68Z],[E69A],[E69B],[E69C],[E70A],[E70B],[E71A],[E71B],[E71C],[E72Z],[E73A],[E73B],[E73C],[E74A],[E74B],[E74C],[E75A],[E75B],[E75C],[F01A],[F01B],[F02Z],[F03Z],[F04A],[F04B],[F05A],[F05B],[F06A],[F06B],[F07A],[F07B],[F08A],[F08B],[F09A],[F09B],[F10Z],[F11A],[F11B],[F12Z],[F13Z],[F14A],[F14B],[F14C],[F15Z],[F16Z],[F17Z],[F18Z],[F19Z],[F20Z],[F21A],[F21B],[F40Z],[F41A],[F41B],[F42A],[F42B],[F60A],[F60B],[F60C],[F61Z],[F62A],[F62B],[F63A],[F63B],[F64Z],[F65A],[F65B],[F66A],[F66B],[F67A],[F67B],[F68Z],[F69A],[F69B],[F70A],[F70B],[F71A],[F71B],[F72A],[F72B],[F73A],[F73B],[F74Z],[F75A],[F75B],[F75C],[G01A],[G01B],[G02A],[G02B],[G03A],[G03B],[G03C],[G04A],[G04B],[G04C],[G05A],[G05B],[G06Z],[G07A],[G07B],[G08A],[G08B],[G09Z],[G10Z],[G11A],[G11B],[G12A],[G12B],[G42A],[G42B],[G43Z],[G44A],[G44B],[G44C],[G45A],[G45B],[G46A],[G46B],[G46C],[G60A],[G60B],[G61A],[G61B],[G62Z],[G63Z],[G64Z],[G65A],[G65B],[G66A],[G66B],[G67A],[G67B],[G68A],[G68B],[G69Z],[G70A],[G70B],[H01A],[H01B],[H02A],[H02B],[H02C],[H05A],[H05B],[H06Z],[H07A],[H07B],[H08A],[H08B],[H40Z],[H41A],[H41B],[H42A],[H42B],[H42C],[H60A],[H60B],[H60C],[H61A],[H61B],[H62A],[H62B],[H63A],[H63B],[H64A],[H64B],[I01Z],[I02A],[I02B],[I03A],[I03B],[I03C],[I04Z],[I05Z],[I06Z],[I07Z],[I08A],[I08B],[I09A],[I09B],[I10A],[I10B],[I11Z],[I12A],[I12B],[I12C],[I13A],[I13B],[I13C],[I14Z],[I15Z],[I16Z],[I17Z],[I18Z],[I19Z],[I20Z],[I21Z],[I23Z],[I24Z],[I25Z],[I27A],[I27B],[I28A],[I28B],[I29Z],[I30Z],[I60Z],[I61Z],[I63Z],[I64A],[I64B],[I65A],[I65B],[I66A],[I66B],[I67A],[I67B],[I68A],[I68B],[I68C],[I69A],[I69B],[I69C],[I70Z],[I71A],[I71B],[I71C],[I72A],[I72B],[I73A],[I73B],[I73C],[I74A],[I74B],[I74C],[I75A],[I75B],[I75C],[I76A],[I76B],[I76C],[I77A],[I77B],[I78A],[I78B],[J01Z],[J06A],[J06B],[J07A],[J07B],[J08A],[J08B],[J09Z],[J10Z],[J11Z],[J12A],[J12B],[J12C],[J13A],[J13B],[J14Z],[J60A],[J60B],[J62A],[J62B],[J63Z],[J64A],[J64B],[J65A],[J65B],[J67A],[J67B],[J68A],[J68B],[K01Z],[K02Z],[K03Z],[K04Z],[K05Z],[K06Z],[K07Z],[K08Z],[K09Z],[K40Z],[K60A],[K60B],[K61Z],[K62A],[K62B],[K62C],[K63Z],[K64A],[K64B],[L02A],[L02B],[L03A],[L03B],[L04A],[L04B],[L04C],[L05A],[L05B],[L06A],[L06B],[L07A],[L07B],[L08A],[L08B],[L09A],[L09B],[L09C],[L40Z],[L41Z],[L42Z],[L60A],[L60B],[L60C],[L61Z],[L62A],[L62B],[L63A],[L63B],[L63C],[L64Z],[L65A],[L65B],[L66Z],[L67A],[L67B],[L67C],[M01Z],[M02A],[M02B],[M03A],[M03B],[M04A],[M04B],[M05Z],[M06A],[M06B],[M40Z],[M60A],[M60B],[M61A],[M61B],[M62A],[M62B],[M63Z],[M64Z],[N01Z],[N02A],[N02B],[N03A],[N03B],[N04Z],[N05A],[N05B],[N06Z],[N07Z],[N08Z],[N09Z],[N10Z],[N11A],[N11B],[N60A],[N60B],[N61Z],[N62A],[N62B],[O01A],[O01B],[O01C],[O02A],[O02B],[O03Z],[O04Z],[O05Z],[O60A],[O60B],[O60C],[O61Z],[O63Z],[O64A],[O64B],[O66A],[O66B],[P01Z],[P02Z],[P03Z],[P04Z],[P05Z],[P06A],[P06B],[P60A],[P60B],[P61Z],[P62Z],[P63Z],[P64Z],[P65A],[P65B],[P65C],[P65D],[P66A],[P66B],[P66C],[P66D],[P67A],[P67B],[P67C],[P67D],[Q01Z],[Q02A],[Q02B],[Q60A],[Q60B],[Q60C],[Q61A],[Q61B],[Q61C],[Q62Z],[R01A],[R01B],[R02A],[R02B],[R03A],[R03B],[R04A],[R04B],[R60A],[R60B],[R60C],[R61A],[R61B],[R61C],[R62A],[R62B],[R63Z],[R64Z],[S60Z],[S65A],[S65B],[S65C],[T01A],[T01B],[T01C],[T60A],[T60B],[T61A],[T61B],[T62A],[T62B],[T63A],[T63B],[T64A],[T64B],[U40Z],[U60Z],[U61A],[U61B],[U62A],[U62B],[U63A],[U63B],[U64Z],[U65Z],[U66Z],[U67Z],[U68Z],[V60A],[V60B],[V61Z],[V62A],[V62B],[V63A],[V63B],[V64Z],[W01Z],[W02Z],[W03Z],[W04Z],[W60Z],[W61Z],[X02Z],[X04A],[X04B],[X05Z],[X06A],[X06B],[X07A],[X07B],[X60A],[X60B],[X60C],[X61Z],[X62A],[X62B],[X63A],[X63B],[X64A],[X64B],[Y01Z],[Y02A],[Y02B],[Y03Z],[Y60Z],[Y61Z],[Y62A],[Y62B],[Z01A],[Z01B],[Z40Z],[Z60A],[Z60B],[Z60C],[Z61Z],[Z62Z],[Z63A],[Z63B],[Z64A],[Z64B],[Z65Z],[901Z],[902Z],[903Z],[960Z],[961Z],[963Z])
) AS pvt
But in this code every DRG come with zero values.
How can we do this with SQL Server?
just in place of [DRG] put [DRGCount]
SELECT TOP 1000 HospitalCode,Durum,[A01Z],[A03Z],[A05Z],[Z99A],[Z98A],[Z97A],[Z96A],[A06Z],[A07Z],[A08A],[A08B],[A09A],[A09B],[A40Z],[A41A],[A41B],[B01Z],[B02A],[B02B],[B02C],[B03A],[B03B],[B04A],[B04B],[B05Z],[B06A],[B06B],[B07A],[B07B],[B40Z],[B41Z],[B60A],[B60B],[B61A],[B61B],[B62Z],[B63Z],[B64A],[B64B],[B65Z],[B66A],[B66B],[B67A],[B67B],[B67C],[B68A],[B68B],[B69A],[B69B],[B70A],[B70B],[B70C],[B70D],[B71A],[B71B],[B72A],[B72B],[B73Z],[B74Z],[B75Z],[B76A],[B76B],[B77Z],[B78A],[B78B],[B79Z],[B80Z],[B81A],[B81B],[C01Z],[C02Z],[C03Z],[C04Z],[C05Z],[C10Z],[C11Z],[C12Z],[C13Z],[C14Z],[C15A],[C15B],[C16A],[C16B],[C60A],[C60B],[C61Z],[C62Z],[C63A],[C63B],[D01Z],[D02A],[D02B],[D02C],[D03Z],[D04A],[D04B],[D05Z],[D06Z],[D09Z],[D10Z],[D11Z],[D12Z],[D13Z],[D14Z],[D40Z],[D60A],[D60B],[D61Z],[D62Z],[D63A],[D63B],[D64Z],[D65Z],[D66A],[D66B],[D67A],[D67B],[E01A],[E01B],[E02A],[E02B],[E02C],[E40Z],[E41Z],[E60A],[E60B],[E61A],[E61B],[E62A],[E62B],[E62C],[E63Z],[E64Z],[E65A],[E65B],[E66A],[E66B],[E66C],[E67A],[E67B],[E68Z],[E69A],[E69B],[E69C],[E70A],[E70B],[E71A],[E71B],[E71C],[E72Z],[E73A],[E73B],[E73C],[E74A],[E74B],[E74C],[E75A],[E75B],[E75C],[F01A],[F01B],[F02Z],[F03Z],[F04A],[F04B],[F05A],[F05B],[F06A],[F06B],[F07A],[F07B],[F08A],[F08B],[F09A],[F09B],[F10Z],[F11A],[F11B],[F12Z],[F13Z],[F14A],[F14B],[F14C],[F15Z],[F16Z],[F17Z],[F18Z],[F19Z],[F20Z],[F21A],[F21B],[F40Z],[F41A],[F41B],[F42A],[F42B],[F60A],[F60B],[F60C],[F61Z],[F62A],[F62B],[F63A],[F63B],[F64Z],[F65A],[F65B],[F66A],[F66B],[F67A],[F67B],[F68Z],[F69A],[F69B],[F70A],[F70B],[F71A],[F71B],[F72A],[F72B],[F73A],[F73B],[F74Z],[F75A],[F75B],[F75C],[G01A],[G01B],[G02A],[G02B],[G03A],[G03B],[G03C],[G04A],[G04B],[G04C],[G05A],[G05B],[G06Z],[G07A],[G07B],[G08A],[G08B],[G09Z],[G10Z],[G11A],[G11B],[G12A],[G12B],[G42A],[G42B],[G43Z],[G44A],[G44B],[G44C],[G45A],[G45B],[G46A],[G46B],[G46C],[G60A],[G60B],[G61A],[G61B],[G62Z],[G63Z],[G64Z],[G65A],[G65B],[G66A],[G66B],[G67A],[G67B],[G68A],[G68B],[G69Z],[G70A],[G70B],[H01A],[H01B],[H02A],[H02B],[H02C],[H05A],[H05B],[H06Z],[H07A],[H07B],[H08A],[H08B],[H40Z],[H41A],[H41B],[H42A],[H42B],[H42C],[H60A],[H60B],[H60C],[H61A],[H61B],[H62A],[H62B],[H63A],[H63B],[H64A],[H64B],[I01Z],[I02A],[I02B],[I03A],[I03B],[I03C],[I04Z],[I05Z],[I06Z],[I07Z],[I08A],[I08B],[I09A],[I09B],[I10A],[I10B],[I11Z],[I12A],[I12B],[I12C],[I13A],[I13B],[I13C],[I14Z],[I15Z],[I16Z],[I17Z],[I18Z],[I19Z],[I20Z],[I21Z],[I23Z],[I24Z],[I25Z],[I27A],[I27B],[I28A],[I28B],[I29Z],[I30Z],[I60Z],[I61Z],[I63Z],[I64A],[I64B],[I65A],[I65B],[I66A],[I66B],[I67A],[I67B],[I68A],[I68B],[I68C],[I69A],[I69B],[I69C],[I70Z],[I71A],[I71B],[I71C],[I72A],[I72B],[I73A],[I73B],[I73C],[I74A],[I74B],[I74C],[I75A],[I75B],[I75C],[I76A],[I76B],[I76C],[I77A],[I77B],[I78A],[I78B],[J01Z],[J06A],[J06B],[J07A],[J07B],[J08A],[J08B],[J09Z],[J10Z],[J11Z],[J12A],[J12B],[J12C],[J13A],[J13B],[J14Z],[J60A],[J60B],[J62A],[J62B],[J63Z],[J64A],[J64B],[J65A],[J65B],[J67A],[J67B],[J68A],[J68B],[K01Z],[K02Z],[K03Z],[K04Z],[K05Z],[K06Z],[K07Z],[K08Z],[K09Z],[K40Z],[K60A],[K60B],[K61Z],[K62A],[K62B],[K62C],[K63Z],[K64A],[K64B],[L02A],[L02B],[L03A],[L03B],[L04A],[L04B],[L04C],[L05A],[L05B],[L06A],[L06B],[L07A],[L07B],[L08A],[L08B],[L09A],[L09B],[L09C],[L40Z],[L41Z],[L42Z],[L60A],[L60B],[L60C],[L61Z],[L62A],[L62B],[L63A],[L63B],[L63C],[L64Z],[L65A],[L65B],[L66Z],[L67A],[L67B],[L67C],[M01Z],[M02A],[M02B],[M03A],[M03B],[M04A],[M04B],[M05Z],[M06A],[M06B],[M40Z],[M60A],[M60B],[M61A],[M61B],[M62A],[M62B],[M63Z],[M64Z],[N01Z],[N02A],[N02B],[N03A],[N03B],[N04Z],[N05A],[N05B],[N06Z],[N07Z],[N08Z],[N09Z],[N10Z],[N11A],[N11B],[N60A],[N60B],[N61Z],[N62A],[N62B],[O01A],[O01B],[O01C],[O02A],[O02B],[O03Z],[O04Z],[O05Z],[O60A],[O60B],[O60C],[O61Z],[O63Z],[O64A],[O64B],[O66A],[O66B],[P01Z],[P02Z],[P03Z],[P04Z],[P05Z],[P06A],[P06B],[P60A],[P60B],[P61Z],[P62Z],[P63Z],[P64Z],[P65A],[P65B],[P65C],[P65D],[P66A],[P66B],[P66C],[P66D],[P67A],[P67B],[P67C],[P67D],[Q01Z],[Q02A],[Q02B],[Q60A],[Q60B],[Q60C],[Q61A],[Q61B],[Q61C],[Q62Z],[R01A],[R01B],[R02A],[R02B],[R03A],[R03B],[R04A],[R04B],[R60A],[R60B],[R60C],[R61A],[R61B],[R61C],[R62A],[R62B],[R63Z],[R64Z],[S60Z],[S65A],[S65B],[S65C],[T01A],[T01B],[T01C],[T60A],[T60B],[T61A],[T61B],[T62A],[T62B],[T63A],[T63B],[T64A],[T64B],[U40Z],[U60Z],[U61A],[U61B],[U62A],[U62B],[U63A],[U63B],[U64Z],[U65Z],[U66Z],[U67Z],[U68Z],[V60A],[V60B],[V61Z],[V62A],[V62B],[V63A],[V63B],[V64Z],[W01Z],[W02Z],[W03Z],[W04Z],[W60Z],[W61Z],[X02Z],[X04A],[X04B],[X05Z],[X06A],[X06B],[X07A],[X07B],[X60A],[X60B],[X60C],[X61Z],[X62A],[X62B],[X63A],[X63B],[X64A],[X64B],[Y01Z],[Y02A],[Y02B],[Y03Z],[Y60Z],[Y61Z],[Y62A],[Y62B],[Z01A],[Z01B],[Z40Z],[Z60A],[Z60B],[Z60C],[Z61Z],[Z62Z],[Z63A],[Z63B],[Z64A],[Z64B],[Z65Z],[901Z],[902Z],[903Z],[960Z],[961Z],[963Z]
FROM
(SELECT * FROM TIGPatient) p
PIVOT
(
COUNT (DRGCount)
FOR DRG IN
( [A01Z],[A03Z],[A05Z],[Z99A],[Z98A],[Z97A],[Z96A],[A06Z],[A07Z],[A08A],[A08B],[A09A],[A09B],[A40Z],[A41A],[A41B],[B01Z],[B02A],[B02B],[B02C],[B03A],[B03B],[B04A],[B04B],[B05Z],[B06A],[B06B],[B07A],[B07B],[B40Z],[B41Z],[B60A],[B60B],[B61A],[B61B],[B62Z],[B63Z],[B64A],[B64B],[B65Z],[B66A],[B66B],[B67A],[B67B],[B67C],[B68A],[B68B],[B69A],[B69B],[B70A],[B70B],[B70C],[B70D],[B71A],[B71B],[B72A],[B72B],[B73Z],[B74Z],[B75Z],[B76A],[B76B],[B77Z],[B78A],[B78B],[B79Z],[B80Z],[B81A],[B81B],[C01Z],[C02Z],[C03Z],[C04Z],[C05Z],[C10Z],[C11Z],[C12Z],[C13Z],[C14Z],[C15A],[C15B],[C16A],[C16B],[C60A],[C60B],[C61Z],[C62Z],[C63A],[C63B],[D01Z],[D02A],[D02B],[D02C],[D03Z],[D04A],[D04B],[D05Z],[D06Z],[D09Z],[D10Z],[D11Z],[D12Z],[D13Z],[D14Z],[D40Z],[D60A],[D60B],[D61Z],[D62Z],[D63A],[D63B],[D64Z],[D65Z],[D66A],[D66B],[D67A],[D67B],[E01A],[E01B],[E02A],[E02B],[E02C],[E40Z],[E41Z],[E60A],[E60B],[E61A],[E61B],[E62A],[E62B],[E62C],[E63Z],[E64Z],[E65A],[E65B],[E66A],[E66B],[E66C],[E67A],[E67B],[E68Z],[E69A],[E69B],[E69C],[E70A],[E70B],[E71A],[E71B],[E71C],[E72Z],[E73A],[E73B],[E73C],[E74A],[E74B],[E74C],[E75A],[E75B],[E75C],[F01A],[F01B],[F02Z],[F03Z],[F04A],[F04B],[F05A],[F05B],[F06A],[F06B],[F07A],[F07B],[F08A],[F08B],[F09A],[F09B],[F10Z],[F11A],[F11B],[F12Z],[F13Z],[F14A],[F14B],[F14C],[F15Z],[F16Z],[F17Z],[F18Z],[F19Z],[F20Z],[F21A],[F21B],[F40Z],[F41A],[F41B],[F42A],[F42B],[F60A],[F60B],[F60C],[F61Z],[F62A],[F62B],[F63A],[F63B],[F64Z],[F65A],[F65B],[F66A],[F66B],[F67A],[F67B],[F68Z],[F69A],[F69B],[F70A],[F70B],[F71A],[F71B],[F72A],[F72B],[F73A],[F73B],[F74Z],[F75A],[F75B],[F75C],[G01A],[G01B],[G02A],[G02B],[G03A],[G03B],[G03C],[G04A],[G04B],[G04C],[G05A],[G05B],[G06Z],[G07A],[G07B],[G08A],[G08B],[G09Z],[G10Z],[G11A],[G11B],[G12A],[G12B],[G42A],[G42B],[G43Z],[G44A],[G44B],[G44C],[G45A],[G45B],[G46A],[G46B],[G46C],[G60A],[G60B],[G61A],[G61B],[G62Z],[G63Z],[G64Z],[G65A],[G65B],[G66A],[G66B],[G67A],[G67B],[G68A],[G68B],[G69Z],[G70A],[G70B],[H01A],[H01B],[H02A],[H02B],[H02C],[H05A],[H05B],[H06Z],[H07A],[H07B],[H08A],[H08B],[H40Z],[H41A],[H41B],[H42A],[H42B],[H42C],[H60A],[H60B],[H60C],[H61A],[H61B],[H62A],[H62B],[H63A],[H63B],[H64A],[H64B],[I01Z],[I02A],[I02B],[I03A],[I03B],[I03C],[I04Z],[I05Z],[I06Z],[I07Z],[I08A],[I08B],[I09A],[I09B],[I10A],[I10B],[I11Z],[I12A],[I12B],[I12C],[I13A],[I13B],[I13C],[I14Z],[I15Z],[I16Z],[I17Z],[I18Z],[I19Z],[I20Z],[I21Z],[I23Z],[I24Z],[I25Z],[I27A],[I27B],[I28A],[I28B],[I29Z],[I30Z],[I60Z],[I61Z],[I63Z],[I64A],[I64B],[I65A],[I65B],[I66A],[I66B],[I67A],[I67B],[I68A],[I68B],[I68C],[I69A],[I69B],[I69C],[I70Z],[I71A],[I71B],[I71C],[I72A],[I72B],[I73A],[I73B],[I73C],[I74A],[I74B],[I74C],[I75A],[I75B],[I75C],[I76A],[I76B],[I76C],[I77A],[I77B],[I78A],[I78B],[J01Z],[J06A],[J06B],[J07A],[J07B],[J08A],[J08B],[J09Z],[J10Z],[J11Z],[J12A],[J12B],[J12C],[J13A],[J13B],[J14Z],[J60A],[J60B],[J62A],[J62B],[J63Z],[J64A],[J64B],[J65A],[J65B],[J67A],[J67B],[J68A],[J68B],[K01Z],[K02Z],[K03Z],[K04Z],[K05Z],[K06Z],[K07Z],[K08Z],[K09Z],[K40Z],[K60A],[K60B],[K61Z],[K62A],[K62B],[K62C],[K63Z],[K64A],[K64B],[L02A],[L02B],[L03A],[L03B],[L04A],[L04B],[L04C],[L05A],[L05B],[L06A],[L06B],[L07A],[L07B],[L08A],[L08B],[L09A],[L09B],[L09C],[L40Z],[L41Z],[L42Z],[L60A],[L60B],[L60C],[L61Z],[L62A],[L62B],[L63A],[L63B],[L63C],[L64Z],[L65A],[L65B],[L66Z],[L67A],[L67B],[L67C],[M01Z],[M02A],[M02B],[M03A],[M03B],[M04A],[M04B],[M05Z],[M06A],[M06B],[M40Z],[M60A],[M60B],[M61A],[M61B],[M62A],[M62B],[M63Z],[M64Z],[N01Z],[N02A],[N02B],[N03A],[N03B],[N04Z],[N05A],[N05B],[N06Z],[N07Z],[N08Z],[N09Z],[N10Z],[N11A],[N11B],[N60A],[N60B],[N61Z],[N62A],[N62B],[O01A],[O01B],[O01C],[O02A],[O02B],[O03Z],[O04Z],[O05Z],[O60A],[O60B],[O60C],[O61Z],[O63Z],[O64A],[O64B],[O66A],[O66B],[P01Z],[P02Z],[P03Z],[P04Z],[P05Z],[P06A],[P06B],[P60A],[P60B],[P61Z],[P62Z],[P63Z],[P64Z],[P65A],[P65B],[P65C],[P65D],[P66A],[P66B],[P66C],[P66D],[P67A],[P67B],[P67C],[P67D],[Q01Z],[Q02A],[Q02B],[Q60A],[Q60B],[Q60C],[Q61A],[Q61B],[Q61C],[Q62Z],[R01A],[R01B],[R02A],[R02B],[R03A],[R03B],[R04A],[R04B],[R60A],[R60B],[R60C],[R61A],[R61B],[R61C],[R62A],[R62B],[R63Z],[R64Z],[S60Z],[S65A],[S65B],[S65C],[T01A],[T01B],[T01C],[T60A],[T60B],[T61A],[T61B],[T62A],[T62B],[T63A],[T63B],[T64A],[T64B],[U40Z],[U60Z],[U61A],[U61B],[U62A],[U62B],[U63A],[U63B],[U64Z],[U65Z],[U66Z],[U67Z],[U68Z],[V60A],[V60B],[V61Z],[V62A],[V62B],[V63A],[V63B],[V64Z],[W01Z],[W02Z],[W03Z],[W04Z],[W60Z],[W61Z],[X02Z],[X04A],[X04B],[X05Z],[X06A],[X06B],[X07A],[X07B],[X60A],[X60B],[X60C],[X61Z],[X62A],[X62B],[X63A],[X63B],[X64A],[X64B],[Y01Z],[Y02A],[Y02B],[Y03Z],[Y60Z],[Y61Z],[Y62A],[Y62B],[Z01A],[Z01B],[Z40Z],[Z60A],[Z60B],[Z60C],[Z61Z],[Z62Z],[Z63A],[Z63B],[Z64A],[Z64B],[Z65Z],[901Z],[902Z],[903Z],[960Z],[961Z],[963Z])
) AS pvt
This can be done using a Dynamic Crosstab.
Read this article by Jeff Moden for reference: http://www.sqlservercentral.com/articles/Crosstab/65048
DECLARE #sql1 VARCHAR(2000) = ''
DECLARE #sql2 VARCHAR(2000) = ''
DECLARE #sql3 VARCHAR(2000) = ''
SELECT #sql1 =
'SELECT
HospitalName
'
SELECT #sql2 = #sql2 +
' ,MAX(CASE WHEN DRG = ''' + DRG + ''' THEN DrgCount ELSE 0 END) AS [' + DRG + ']' + CHAR(10)
FROM(
SELECT DISTINCT DRG FROM temp
)t
ORDER BY DRG
SELECT #sql3 =
'FROM temp
GROUP BY HospitalName
ORDER BY HospitalName'
PRINT (#sql1 + #sql2 + #sql3)
EXEC (#sql1 + #sql2 + #sql3)
SELECT [HospitalName], [F67A], [F68A], [F69A], [F60A], [F61A] FROM tbl
PIVOT(
COUNT([DRG])
FOR [DRG] IN ([F67A], [F68A], [F69A], [F60A], [F61A])
) AS p
SQLFiddle
As for your query :
Remove Durum field from the SELECT ;
No need for a temporary table (SELECT * FROM TIGPatient) p;
So the following should work :
SELECT TOP 1000 HospitalCode
,[A01Z],[A03Z],[A05Z],[Z99A],[Z98A],[Z97A],[Z96A],[A06Z],[A07Z],[A08A],[A08B],[A09A],[A09B],[A40Z],[A41A],[A41B],[B01Z],[B02A],[B02B],[B02C],[B03A],[B03B],[B04A],[B04B],[B05Z],[B06A],[B06B],[B07A],[B07B],[B40Z],[B41Z],[B60A],[B60B],[B61A],[B61B],[B62Z],[B63Z],[B64A],[B64B],[B65Z],[B66A],[B66B],[B67A],[B67B],[B67C],[B68A],[B68B],[B69A],[B69B],[B70A],[B70B],[B70C],[B70D],[B71A],[B71B],[B72A],[B72B],[B73Z],[B74Z],[B75Z],[B76A],[B76B],[B77Z],[B78A],[B78B],[B79Z],[B80Z],[B81A],[B81B],[C01Z],[C02Z],[C03Z],[C04Z],[C05Z],[C10Z],[C11Z],[C12Z],[C13Z],[C14Z],[C15A],[C15B],[C16A],[C16B],[C60A],[C60B],[C61Z],[C62Z],[C63A],[C63B],[D01Z],[D02A],[D02B],[D02C],[D03Z],[D04A],[D04B],[D05Z],[D06Z],[D09Z],[D10Z],[D11Z],[D12Z],[D13Z],[D14Z],[D40Z],[D60A],[D60B],[D61Z],[D62Z],[D63A],[D63B],[D64Z],[D65Z],[D66A],[D66B],[D67A],[D67B],[E01A],[E01B],[E02A],[E02B],[E02C],[E40Z],[E41Z],[E60A],[E60B],[E61A],[E61B],[E62A],[E62B],[E62C],[E63Z],[E64Z],[E65A],[E65B],[E66A],[E66B],[E66C],[E67A],[E67B],[E68Z],[E69A],[E69B],[E69C],[E70A],[E70B],[E71A],[E71B],[E71C],[E72Z],[E73A],[E73B],[E73C],[E74A],[E74B],[E74C],[E75A],[E75B],[E75C],[F01A],[F01B],[F02Z],[F03Z],[F04A],[F04B],[F05A],[F05B],[F06A],[F06B],[F07A],[F07B],[F08A],[F08B],[F09A],[F09B],[F10Z],[F11A],[F11B],[F12Z],[F13Z],[F14A],[F14B],[F14C],[F15Z],[F16Z],[F17Z],[F18Z],[F19Z],[F20Z],[F21A],[F21B],[F40Z],[F41A],[F41B],[F42A],[F42B],[F60A],[F60B],[F60C],[F61Z],[F62A],[F62B],[F63A],[F63B],[F64Z],[F65A],[F65B],[F66A],[F66B],[F67A],[F67B],[F68Z],[F69A],[F69B],[F70A],[F70B],[F71A],[F71B],[F72A],[F72B],[F73A],[F73B],[F74Z],[F75A],[F75B],[F75C],[G01A],[G01B],[G02A],[G02B],[G03A],[G03B],[G03C],[G04A],[G04B],[G04C],[G05A],[G05B],[G06Z],[G07A],[G07B],[G08A],[G08B],[G09Z],[G10Z],[G11A],[G11B],[G12A],[G12B],[G42A],[G42B],[G43Z],[G44A],[G44B],[G44C],[G45A],[G45B],[G46A],[G46B],[G46C],[G60A],[G60B],[G61A],[G61B],[G62Z],[G63Z],[G64Z],[G65A],[G65B],[G66A],[G66B],[G67A],[G67B],[G68A],[G68B],[G69Z],[G70A],[G70B],[H01A],[H01B],[H02A],[H02B],[H02C],[H05A],[H05B],[H06Z],[H07A],[H07B],[H08A],[H08B],[H40Z],[H41A],[H41B],[H42A],[H42B],[H42C],[H60A],[H60B],[H60C],[H61A],[H61B],[H62A],[H62B],[H63A],[H63B],[H64A],[H64B],[I01Z],[I02A],[I02B],[I03A],[I03B],[I03C],[I04Z],[I05Z],[I06Z],[I07Z],[I08A],[I08B],[I09A],[I09B],[I10A],[I10B],[I11Z],[I12A],[I12B],[I12C],[I13A],[I13B],[I13C],[I14Z],[I15Z],[I16Z],[I17Z],[I18Z],[I19Z],[I20Z],[I21Z],[I23Z],[I24Z],[I25Z],[I27A],[I27B],[I28A],[I28B],[I29Z],[I30Z],[I60Z],[I61Z],[I63Z],[I64A],[I64B],[I65A],[I65B],[I66A],[I66B],[I67A],[I67B],[I68A],[I68B],[I68C],[I69A],[I69B],[I69C],[I70Z],[I71A],[I71B],[I71C],[I72A],[I72B],[I73A],[I73B],[I73C],[I74A],[I74B],[I74C],[I75A],[I75B],[I75C],[I76A],[I76B],[I76C],[I77A],[I77B],[I78A],[I78B],[J01Z],[J06A],[J06B],[J07A],[J07B],[J08A],[J08B],[J09Z],[J10Z],[J11Z],[J12A],[J12B],[J12C],[J13A],[J13B],[J14Z],[J60A],[J60B],[J62A],[J62B],[J63Z],[J64A],[J64B],[J65A],[J65B],[J67A],[J67B],[J68A],[J68B],[K01Z],[K02Z],[K03Z],[K04Z],[K05Z],[K06Z],[K07Z],[K08Z],[K09Z],[K40Z],[K60A],[K60B],[K61Z],[K62A],[K62B],[K62C],[K63Z],[K64A],[K64B],[L02A],[L02B],[L03A],[L03B],[L04A],[L04B],[L04C],[L05A],[L05B],[L06A],[L06B],[L07A],[L07B],[L08A],[L08B],[L09A],[L09B],[L09C],[L40Z],[L41Z],[L42Z],[L60A],[L60B],[L60C],[L61Z],[L62A],[L62B],[L63A],[L63B],[L63C],[L64Z],[L65A],[L65B],[L66Z],[L67A],[L67B],[L67C],[M01Z],[M02A],[M02B],[M03A],[M03B],[M04A],[M04B],[M05Z],[M06A],[M06B],[M40Z],[M60A],[M60B],[M61A],[M61B],[M62A],[M62B],[M63Z],[M64Z],[N01Z],[N02A],[N02B],[N03A],[N03B],[N04Z],[N05A],[N05B],[N06Z],[N07Z],[N08Z],[N09Z],[N10Z],[N11A],[N11B],[N60A],[N60B],[N61Z],[N62A],[N62B],[O01A],[O01B],[O01C],[O02A],[O02B],[O03Z],[O04Z],[O05Z],[O60A],[O60B],[O60C],[O61Z],[O63Z],[O64A],[O64B],[O66A],[O66B],[P01Z],[P02Z],[P03Z],[P04Z],[P05Z],[P06A],[P06B],[P60A],[P60B],[P61Z],[P62Z],[P63Z],[P64Z],[P65A],[P65B],[P65C],[P65D],[P66A],[P66B],[P66C],[P66D],[P67A],[P67B],[P67C],[P67D],[Q01Z],[Q02A],[Q02B],[Q60A],[Q60B],[Q60C],[Q61A],[Q61B],[Q61C],[Q62Z],[R01A],[R01B],[R02A],[R02B],[R03A],[R03B],[R04A],[R04B],[R60A],[R60B],[R60C],[R61A],[R61B],[R61C],[R62A],[R62B],[R63Z],[R64Z],[S60Z],[S65A],[S65B],[S65C],[T01A],[T01B],[T01C],[T60A],[T60B],[T61A],[T61B],[T62A],[T62B],[T63A],[T63B],[T64A],[T64B],[U40Z],[U60Z],[U61A],[U61B],[U62A],[U62B],[U63A],[U63B],[U64Z],[U65Z],[U66Z],[U67Z],[U68Z],[V60A],[V60B],[V61Z],[V62A],[V62B],[V63A],[V63B],[V64Z],[W01Z],[W02Z],[W03Z],[W04Z],[W60Z],[W61Z],[X02Z],[X04A],[X04B],[X05Z],[X06A],[X06B],[X07A],[X07B],[X60A],[X60B],[X60C],[X61Z],[X62A],[X62B],[X63A],[X63B],[X64A],[X64B],[Y01Z],[Y02A],[Y02B],[Y03Z],[Y60Z],[Y61Z],[Y62A],[Y62B],[Z01A],[Z01B],[Z40Z],[Z60A],[Z60B],[Z60C],[Z61Z],[Z62Z],[Z63A],[Z63B],[Z64A],[Z64B],[Z65Z],[901Z],[902Z],[903Z],[960Z],[961Z],[963Z]
FROM TIGPatient
PIVOT (
COUNT (DRG)
FOR DRG IN([A01Z],[A03Z],[A05Z],[Z99A],[Z98A],[Z97A],[Z96A],[A06Z],[A07Z],[A08A],[A08B],[A09A],[A09B],[A40Z],[A41A],[A41B],[B01Z],[B02A],[B02B],[B02C],[B03A],[B03B],[B04A],[B04B],[B05Z],[B06A],[B06B],[B07A],[B07B],[B40Z],[B41Z],[B60A],[B60B],[B61A],[B61B],[B62Z],[B63Z],[B64A],[B64B],[B65Z],[B66A],[B66B],[B67A],[B67B],[B67C],[B68A],[B68B],[B69A],[B69B],[B70A],[B70B],[B70C],[B70D],[B71A],[B71B],[B72A],[B72B],[B73Z],[B74Z],[B75Z],[B76A],[B76B],[B77Z],[B78A],[B78B],[B79Z],[B80Z],[B81A],[B81B],[C01Z],[C02Z],[C03Z],[C04Z],[C05Z],[C10Z],[C11Z],[C12Z],[C13Z],[C14Z],[C15A],[C15B],[C16A],[C16B],[C60A],[C60B],[C61Z],[C62Z],[C63A],[C63B],[D01Z],[D02A],[D02B],[D02C],[D03Z],[D04A],[D04B],[D05Z],[D06Z],[D09Z],[D10Z],[D11Z],[D12Z],[D13Z],[D14Z],[D40Z],[D60A],[D60B],[D61Z],[D62Z],[D63A],[D63B],[D64Z],[D65Z],[D66A],[D66B],[D67A],[D67B],[E01A],[E01B],[E02A],[E02B],[E02C],[E40Z],[E41Z],[E60A],[E60B],[E61A],[E61B],[E62A],[E62B],[E62C],[E63Z],[E64Z],[E65A],[E65B],[E66A],[E66B],[E66C],[E67A],[E67B],[E68Z],[E69A],[E69B],[E69C],[E70A],[E70B],[E71A],[E71B],[E71C],[E72Z],[E73A],[E73B],[E73C],[E74A],[E74B],[E74C],[E75A],[E75B],[E75C],[F01A],[F01B],[F02Z],[F03Z],[F04A],[F04B],[F05A],[F05B],[F06A],[F06B],[F07A],[F07B],[F08A],[F08B],[F09A],[F09B],[F10Z],[F11A],[F11B],[F12Z],[F13Z],[F14A],[F14B],[F14C],[F15Z],[F16Z],[F17Z],[F18Z],[F19Z],[F20Z],[F21A],[F21B],[F40Z],[F41A],[F41B],[F42A],[F42B],[F60A],[F60B],[F60C],[F61Z],[F62A],[F62B],[F63A],[F63B],[F64Z],[F65A],[F65B],[F66A],[F66B],[F67A],[F67B],[F68Z],[F69A],[F69B],[F70A],[F70B],[F71A],[F71B],[F72A],[F72B],[F73A],[F73B],[F74Z],[F75A],[F75B],[F75C],[G01A],[G01B],[G02A],[G02B],[G03A],[G03B],[G03C],[G04A],[G04B],[G04C],[G05A],[G05B],[G06Z],[G07A],[G07B],[G08A],[G08B],[G09Z],[G10Z],[G11A],[G11B],[G12A],[G12B],[G42A],[G42B],[G43Z],[G44A],[G44B],[G44C],[G45A],[G45B],[G46A],[G46B],[G46C],[G60A],[G60B],[G61A],[G61B],[G62Z],[G63Z],[G64Z],[G65A],[G65B],[G66A],[G66B],[G67A],[G67B],[G68A],[G68B],[G69Z],[G70A],[G70B],[H01A],[H01B],[H02A],[H02B],[H02C],[H05A],[H05B],[H06Z],[H07A],[H07B],[H08A],[H08B],[H40Z],[H41A],[H41B],[H42A],[H42B],[H42C],[H60A],[H60B],[H60C],[H61A],[H61B],[H62A],[H62B],[H63A],[H63B],[H64A],[H64B],[I01Z],[I02A],[I02B],[I03A],[I03B],[I03C],[I04Z],[I05Z],[I06Z],[I07Z],[I08A],[I08B],[I09A],[I09B],[I10A],[I10B],[I11Z],[I12A],[I12B],[I12C],[I13A],[I13B],[I13C],[I14Z],[I15Z],[I16Z],[I17Z],[I18Z],[I19Z],[I20Z],[I21Z],[I23Z],[I24Z],[I25Z],[I27A],[I27B],[I28A],[I28B],[I29Z],[I30Z],[I60Z],[I61Z],[I63Z],[I64A],[I64B],[I65A],[I65B],[I66A],[I66B],[I67A],[I67B],[I68A],[I68B],[I68C],[I69A],[I69B],[I69C],[I70Z],[I71A],[I71B],[I71C],[I72A],[I72B],[I73A],[I73B],[I73C],[I74A],[I74B],[I74C],[I75A],[I75B],[I75C],[I76A],[I76B],[I76C],[I77A],[I77B],[I78A],[I78B],[J01Z],[J06A],[J06B],[J07A],[J07B],[J08A],[J08B],[J09Z],[J10Z],[J11Z],[J12A],[J12B],[J12C],[J13A],[J13B],[J14Z],[J60A],[J60B],[J62A],[J62B],[J63Z],[J64A],[J64B],[J65A],[J65B],[J67A],[J67B],[J68A],[J68B],[K01Z],[K02Z],[K03Z],[K04Z],[K05Z],[K06Z],[K07Z],[K08Z],[K09Z],[K40Z],[K60A],[K60B],[K61Z],[K62A],[K62B],[K62C],[K63Z],[K64A],[K64B],[L02A],[L02B],[L03A],[L03B],[L04A],[L04B],[L04C],[L05A],[L05B],[L06A],[L06B],[L07A],[L07B],[L08A],[L08B],[L09A],[L09B],[L09C],[L40Z],[L41Z],[L42Z],[L60A],[L60B],[L60C],[L61Z],[L62A],[L62B],[L63A],[L63B],[L63C],[L64Z],[L65A],[L65B],[L66Z],[L67A],[L67B],[L67C],[M01Z],[M02A],[M02B],[M03A],[M03B],[M04A],[M04B],[M05Z],[M06A],[M06B],[M40Z],[M60A],[M60B],[M61A],[M61B],[M62A],[M62B],[M63Z],[M64Z],[N01Z],[N02A],[N02B],[N03A],[N03B],[N04Z],[N05A],[N05B],[N06Z],[N07Z],[N08Z],[N09Z],[N10Z],[N11A],[N11B],[N60A],[N60B],[N61Z],[N62A],[N62B],[O01A],[O01B],[O01C],[O02A],[O02B],[O03Z],[O04Z],[O05Z],[O60A],[O60B],[O60C],[O61Z],[O63Z],[O64A],[O64B],[O66A],[O66B],[P01Z],[P02Z],[P03Z],[P04Z],[P05Z],[P06A],[P06B],[P60A],[P60B],[P61Z],[P62Z],[P63Z],[P64Z],[P65A],[P65B],[P65C],[P65D],[P66A],[P66B],[P66C],[P66D],[P67A],[P67B],[P67C],[P67D],[Q01Z],[Q02A],[Q02B],[Q60A],[Q60B],[Q60C],[Q61A],[Q61B],[Q61C],[Q62Z],[R01A],[R01B],[R02A],[R02B],[R03A],[R03B],[R04A],[R04B],[R60A],[R60B],[R60C],[R61A],[R61B],[R61C],[R62A],[R62B],[R63Z],[R64Z],[S60Z],[S65A],[S65B],[S65C],[T01A],[T01B],[T01C],[T60A],[T60B],[T61A],[T61B],[T62A],[T62B],[T63A],[T63B],[T64A],[T64B],[U40Z],[U60Z],[U61A],[U61B],[U62A],[U62B],[U63A],[U63B],[U64Z],[U65Z],[U66Z],[U67Z],[U68Z],[V60A],[V60B],[V61Z],[V62A],[V62B],[V63A],[V63B],[V64Z],[W01Z],[W02Z],[W03Z],[W04Z],[W60Z],[W61Z],[X02Z],[X04A],[X04B],[X05Z],[X06A],[X06B],[X07A],[X07B],[X60A],[X60B],[X60C],[X61Z],[X62A],[X62B],[X63A],[X63B],[X64A],[X64B],[Y01Z],[Y02A],[Y02B],[Y03Z],[Y60Z],[Y61Z],[Y62A],[Y62B],[Z01A],[Z01B],[Z40Z],[Z60A],[Z60B],[Z60C],[Z61Z],[Z62Z],[Z63A],[Z63B],[Z64A],[Z64B],[Z65Z],[901Z],[902Z],[903Z],[960Z],[961Z],[963Z])
) AS p

Not able to combine multiple rows into single row based on certain conditions

In the image above, i have shown table structure i use to store result of student. However I need to select data in such a manner such that depending on particular FEID(examination ID),
I get marks obtained and subID of single student in single row. Something like below:
FEID SubID1 MarksObtained SubID2 MarksObtained SubID3 MarksObtained StdID
2 1 0 2 0 3 0 50
2 1 45 2 45 3 45 51
Result Column wont affect outcome as for a particular stdID and FEID it remains same for no matter how many SubID are there.
Basically I am storing each subject marks in single row and subjects are can be any number( more than 3 as in this case) , which is not known before hand. But for each I create one row to enter its marks
I tried sytax below .
DECLARE #cols nvarchar(MAX);
--get the list of subids from the table
SELECT #cols = SubjectName from tbSubjects where SubID IN(select distinct SubID from tbFinalMarks);
Declare #sql nvarchar(MAX) = 'SELECT StdId, FEID, ' + #cols + 'FROM
(
SELECT * FROM tbFinalMarks
)t
PIVOT
(
MAX(MarksObtained) FOR SubId IN (' + #cols + ')
)p';
Something like this will do it. It will also dynamically add new columns for new sub ids without you needing to worry about it.
DECLARE #cols nvarchar(MAX);
--get the list of subids from the table
SELECT #cols = COALESCE(#cols + ',', '') + '[' + CAST(SubId AS nvarchar) + ']' FROM (SELECT DISTINCT SubId FROM table);
Declare #sql nvarchar(MAX) = 'SELECT StdId, FEID, ' + #cols + 'FROM
(
SELECT * FROM table
)t
PIVOT
(
MAX(MarksObtained) FOR SubId IN (' + #cols + ')
)p';
EXECUTE sp_executesql #sql;
Although you can use pivot, I think the explicit aggregation approach is easier to construct:
select feid,
1 as SubId_1,
max(case when SubId = 1 then MarksObtained end) as MarksObtained_1,
2 as SubId_2,
max(case when SubId = 2 then MarksObtained end) as MarksObtained_2,
3 as SubId_3,
max(case when SubId = 3 then MarksObtained end) as MarksObtained_3,
stdid
from table t
group by feid, stdid;

display rows as column

I want to display rows as column in SQL Server.
My table looks like this:
images_id item_id images_name
-------------------------------
1 1 image1.jpg
2 1 image2.jpg
3 1 image3.jpg
4 2 image4.jpg
5 2 image5.jpg
6 2 image6.jpg
I'd like this output:
images_id item_id image1 image2 image3
------------------------------------------------------
1 1 image1.jpg image2.jpg image3.jpg
2 2 image4.jpg image5.jpg image6.jpg
Here is an image link.
Is this possible or not? item_id must be dynamically changeable (it is not stable).
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Pivot table for Microsoft SQL Server
This isn't possible without using dynamic SQL. PIVOT requires you to specify the columns still.
Let me know if dynamic SQL is acceptable and I'll spin you an example.
Here is how you can use Dynamic SQL for this:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
SET #cols = STUFF((SELECT distinct ',' + QUOTENAME('image' + cast(row_number() over(partition by itemid order by imageid) as varchar(5)))
FROM test c
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT itemid, ' + #cols + ' from
(
select itemid, imagename,
''image'' + cast(row_number() over(partition by itemid order by imageid) as varchar(5)) col
from test
) x
pivot
(
min(imagename)
for col in (' + #cols + ')
) p '
execute(#query)
See SQL Fiddle with Demo