How to pivot in SQL SERVER [duplicate] - sql
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
Related
sql server sort dynamic pivot on large set of data
I am having trouble sorting a pivot based on a quite large set of data. I have looked at many examples, but none of them seems to address the issue of volume - or perhaps I am just missing something. I have had a very good look here: Sort Columns For Dynamic Pivot and PIVOT in sql 2005 and found much good advise, but I still cannot find the correct way to sort my pivot. I am using the following sql. It pivots the columns, but the result needs to be sorted for readability: SELECT a.* INTO #tempA FROM (SELECT top (5000) id, email, CONVERT(varchar,ROW_NUMBER() OVER (PARTITION BY email ORDER BY id)) AS PIVOT_CODE FROM Email) a order by PIVOT_CODE DECLARE #cols AS NVARCHAR(MAX), #sql AS NVARCHAR(MAX) SELECT #cols =STUFF((SELECT DISTINCT ', ' + QUOTENAME(col) FROM #tempA WITH (NOLOCK) cross apply ( SELECT 'id_' + PIVOT_CODE, id ) c (col, so) group by col, so --order by col FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'') set #sql = 'SELECT email, ' +#cols+ 'INTO ##AnotherPivotTest FROM ( SELECT email, col, value FROM #tempA WITH (NOLOCK) cross apply ( values (''id_'' + PIVOT_CODE, id) ) c (col, value) ) d pivot ( max(value) for col in (' + #cols+ ') ) piv' EXEC (#sql) SELECT * FROM ##AnotherPivotTest The result is a chaos to look at: ============================================================================================== | email | id_19 | id_24 | id_2 | id_16 | id_5 | id_9 | id_23 | .... | id_1 | .... | id_10 | ============================================================================================== | xx#yy.dk | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 1234 | NULL | NULL | ============================================================================================== I would very much like the Ids to be sorted - beginning with id_1. As you can see, I have attempted to place an 'order by' in the selection for 'cols', but that gives me the error: "ORDER BY items must appear in the select list if SELECT DISTINCT is specified." And without DISTINCT, I get another error: "The number of elements in the select list exceeds the maximum allowed number of 4096 elements." I'm stuck, so any help will be greatly appreciated!
Not sure what causes the problem but I've solved my order problem in my pivot table by inserting the data coming from tempA into another temp table and ordering them there INSERT INTO #tempB SELECT * FROM #tempA ORDER BY PIVOT_CODE Then selecting distinct ones like so: SELECT #cols = #cols + QUOTENAME(PIVOT_CODE) + ',' FROM (SELECT DISTINCT PIVOT_CODE FROM #tempB ORDER BY PIVOT_CODE) SELECT #cols = SUBSTRING(#cols, 0, LEN(#cols)) --trims "," at end
You can also just use a cursor to determine your cols and the order them Cursor with cols ordered declare #gruppe nvarchar(max) declare #gruppeSql nvarchar(max) declare #SQL nvarchar(max) DECLARE myCustomers CURSOR FOR select top 10 FirstName from [dbo].[DimCustomer] Order by FirstName set #gruppeSql = '' OPEN myCustomers FETCH NEXT FROM myCustomers INTO #gruppe IF (##FETCH_STATUS>=0) BEGIN SET #gruppeSql = #gruppeSql +'[' +#gruppe+']' FETCH NEXT FROM myCustomers INTO #gruppe END WHILE (##FETCH_STATUS<>-1) BEGIN IF (##FETCH_STATUS<>-2) SET #gruppeSql = #gruppeSql + ',[' +#gruppe+']' FETCH NEXT FROM myCustomers INTO #gruppe END CLOSE myCustomers DEALLOCATE myCustomers SET #gruppeSql = replace(#gruppesql,'''','') /*Select to preview your cols*/ select #gruppeSql Dynamic pivot SET #SQL = ' Select * from ( SELECT SalesAmount, FirstName FROM [AdventureWorksDW2014].[dbo].[FactInternetSales] a inner join dbo.DimCustomer b on a.CustomerKey = b.CustomerKey ) x pivot ( sum(SalesAmount) for FirstName in ('+#gruppesql+') ) p' print #sql exec(#sql)
how can I use Pivot using a variable in SQL?
I have paycode numbers and Names from table Paycodes and I have Amount in monthlyTransaction. as follows: Paycodes Code Name 1 Basic Salary 2 Variable Deduction Amount 3 Fixed/Var Insurance PayCode MonthlyTransaction Code Amount 1 3000 2 10000 1 130000 1 150000 3 120000 I want it to be like this using pivot Basic Salary Variable Deduction Amount Fixed/Var Insurance PayCode 31000 10000 120000 I want to use pivot to sum the Amount of each Paycode and I used this:- DECLARE #data AS NVARCHAR(MAX) DECLARE #query AS NVARCHAR(MAX) -- DECLARE #data TABLE --( -- PaycodeName NVARCHAR(max) --) --INSERT INTO #data -- ( PaycodeName ) --select dbo.Paycode.PayCodeName FROM dbo.Paycode select #data = Paycode.PayCodeName FROM Paycode set #query = 'SELECT * FROM ( SELECT Paycode.Name , MonthlyTransaction.Amount From MonthlyTransaction LEFT JOIN dbo.Paycode ON Paycode.code = MonthlyTransaction.Paycode ) AS s PIVOT ( SUM(Amount) FOR Paycode.Name IN ('+#data+') ) AS pvt ' EXECUTE Sp_executesql #query When I print #data it retrieve the last paycode only ! Can anyone help ?
To get all the paycodes in #data use SELECT #data = #data + Paycode.PayCodeName + ', ' FROM Paycode Then remove the last comma
the answer is : DECLARE #codeNameCol NVARCHAR(max) SELECT #codeNameCol= COALESCE(#codeNameCol + ',','') + QUOTENAME(RTRIM(LTRIM(PayCodeName))) FROM (SELECT DISTINCT PayCodeName FROM Paycode) AS codeNameCol DECLARE #querys NVARCHAR(max) Set #querys=' SELECT * FROM ( SELECT dbo.EmpAssignment.EmployeeId , PayCodeName , Amount FROM dbo.MonthlyTransaction LEFT JOIN dbo.Paycode ON Paycode.code = MonthlyTransaction.Paycode LEFT JOIN dbo.EmpAssignment ON EmpAssignment.EmpId = MonthlyTransaction.EmpId LEFT JOIN dbo.PayrollGroup ON PayrollGroup.PayrollGroup = EmpAssignment.PayrollGroup ) DataTable PIVOT ( SUM(Amount) FOR PayCodeName IN ( '+#codeNameCol+' ) ) PivotTable;' EXEC (#querys)
This should do the trick SELECT 1 Code, 'Basic Salary' Name INTO #Paycode UNION SELECT 2 Code, 'Variable Deduction Amount' Name UNION SELECT 3 Code, 'Fixed/Var Insurance PayCode' Name SELECT 1 Code, 3000 Amount INTO #MonthTrans UNION SELECT 2 Code, 10000 Amount UNION SELECT 1 Code, 130000 Amount UNION SELECT 1 Code, 150000 Amount UNION SELECT 3 Code, 120000 Amount DECLARE #data AS NVARCHAR(MAX) SELECT #data = ISNULL(#data,'') + '[' +CAST(Code AS varchar) + '], ' FROM #Paycode SELECT #data=SUBSTRING(#data, 0, LEN(#data)) DECLARE #query AS NVARCHAR(MAX) SELECT #query = 'SELECT * FROM #MonthTrans M PIVOT ( SUM(Amount) FOR Code IN (' + #data + ') ) pvt' EXECUTE Sp_executesql #query drop table #paycode drop table #MonthTrans
Write a query to count different kind of records in single line result
I have a table with 2 columns TimeStamp and ID in MS SQLSERVER TimeStamp ID 2015-05-20 1 2015-05-20 2 2015-05-20 1 2015-05-21 1 2015-05-21 2 2015-05-21 2 2015-05-21 1 My requirement is to calculate number of records for every Id according to date. Requirement: Date No of records for Id=1 No. records for ID=2 Total 2015-05-20 2 1 3 2015-05-21 2 2 4 Please let me know how can i do this for other columns as well. Thanks
Using Pivot - can look like this: SELECT [TimeStamp], [1] AS [No of records for Id=1], [2] AS [No of records for Id=2], [1]+[2] AS Total FROM dbo.YourTable PIVOT ( COUNT(ID) FOR ID IN ([1],[2]) ) pvt ORDER BY [TimeStamp]
For completion the Dynamic Way: DECLARE #id_values NVARCHAR(MAX) DECLARE #stmt NVARCHAR(MAX) SET #id_values = '' SET #stmt = '' --Get List of ID's SELECT #id_values = #id_values + ',[' + CAST(ID AS VARCHAR) + ']' FROM dbo.YourTable GROUP BY ID SET #id_values = RIGHT(#id_values, LEN(#id_values)-1) --Remove Leading Comma --Build PIVOT Statement SET #stmt = 'SELECT [TimeStamp],' + #id_values + ','+ REPLACE(#id_values,',','+') + ' AS Total FROM dbo.YourTable PIVOT ( COUNT(ID) FOR ID IN (' + #id_values + ') ) pvt ORDER BY [TimeStamp]' --Execute EXEC sp_executesql #stmt
I've got your answer using subqueries: SQLFIDDLE: http://sqlfiddle.com/#!3/8c28c/5 SELECT DISTINCT t1.myts, (SELECT COUNT(*) FROM mytable t2 WHERE t2.myts = t1.myts AND t2.id = 1) AS id_1_count, (SELECT COUNT(*) FROM mytable t2 WHERE t2.myts = t1.myts AND t2.id = 2) AS id_2_count, (SELECT COUNT(*) FROM mytable t2 WHERE t2.myts = t1.myts) AS total FROM mytable t1 If you add an id here though you would need another subquery
How to swap table values to column in sql [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 8 years ago. Improve this question TABLE A P Q R 1 A a 2 B a 3 C b 4 D b How can I convert it to Table B P a b 1 A - 2 B - 3 - C 4 - D Hint: a is mapped with A&B not with C&D in table A. Similarly b is mapped with C&D not with A&B. Pivot is not working. Please help.
This could be achieved using a Dynamic CrossTab. For reference: http://www.sqlservercentral.com/articles/Crosstab/65048/ SAMPLE DATA CREATE TABLE SampleData( P INT, Q CHAR(1), R CHAR(1) ) INSERT INTO SampleData SELECT 1, 'A', 'a' UNION ALL SELECT 2, 'B', 'a' UNION ALL SELECT 3, 'C', 'b' UNION ALL SELECT 4, 'D', 'b' Dynamic Crosstab DECLARE #sql1 VARCHAR(4000) = '', #sql2 VARCHAR(4000) = '', #sql3 VARCHAR(4000) = '' SELECT #sql1 = 'SELECT P' + CHAR(10) SELECT #sql2 = #sql2 + ' ,MAX(CASE WHEN R = ''' + R + ''' THEN Q END) AS [' + R + ']' + CHAR(10) FROM( SELECT DISTINCT R FROM SampleData )t ORDER BY R SELECT #sql3 = 'FROM SampleData GROUP BY P ORDER BY P ' PRINT(#sql1 + #sql2 + #sql3) EXEC(#sql1 + #sql2 + #sql3) RESULT P a b ----------- ---- ---- 1 A NULL 2 B NULL 3 NULL C 4 NULL D
Try this select P, case when R='a' then a else '-' end as a, case when R='b' then '-' else a end as b from table_A
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;