How to swap table values to column in sql [closed] - sql

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

Related

Restructure table by removing NULL values

I have a table in SQL that looks like this:
Customer Product 1999 2000 2001 2002 2003
Smith 51 NULL NULL 15 14 NULL
Jones 14 11 7 NULL NULL NULL
Jackson 13 NULL NULL NULL 3 9
The figures under each year column are amounts, in dollars. Each customer has two consecutive years of amounts, and the rest of the years are zero. I would like to re-structure this table so that instead of wide list of years, it just has two columns Amount-Year1 and Amount-Year2. So it selects the two non-zero years and puts them in those columns, in the correct order. This would greatly reduce the size of my table.
So far I've been able to re-structure it so that there is one amount column and one year column, but I then get multiple rows per customer, which unfortunately I can't have (due to downstream analysis). Can anyone think of a way to get the two Amount-Year columns?
I would like the final table to look like this:
Customer Product Amount_Y1 Amount_Y2
Smith 51 15 14
Jones 14 11 7
Jackson 13 3 9
I don't mind that I lose the information about the specific years, as I can get that from another source. The actual table has data for all years between 1999 and 2018, and there will be further years in the future.
Thanks
Thankfully UNPIVOT removes NULLs anyway, so we can do this with UNPIVOT/ROWNUMBER(),PIVOT:
declare #t table (Customer varchar(15),Product int,[1999] int,
[2000] int,[2001] int,[2002] int,[2003] int)
insert into #T(CUstomer,Product,[1999],[2000],[2001],[2002],[2003]) values
('Smith' ,51,NULL,NULL, 15, 14,NULL),
('Jones' ,14, 11, 7,NULL,NULL,NULL),
('Jackson',13,NULL,NULL,NULL, 3, 9)
;With Numbered as (
select
Customer,Product,Value,
ROW_NUMBER() OVER (PARTITION BY Customer,Product
ORDER BY Year) rn
from
#t t
unpivot
(Value for Year in ([1999],[2000],[2001],[2002],[2003])) u
)
select
*
from
Numbered n
pivot
(SUM(Value) for rn in ([1],[2])) w
Results:
Customer Product 1 2
--------------- ----------- ----------- -----------
Jackson 13 3 9
Jones 14 11 7
Smith 51 15 14
Use COALESCE that will do the job for you. The query is dynamics so that if tomorrow year columns are changed, i.e. removed or added you do not have to change anything.
Sample query: (Assuming table to be table1 and column names to be same as year).
DECLARE #columnsdesc nvarchar(max), #columnsasc nvarchar(max)
SET #columnsdesc = ''
SELECT #columnsdesc = (select + '[' + ltrim(c.Name) + ']' + ','
FROM sys.columns c
JOIN sys.objects o ON o.object_id = c.object_id
WHERE o.type = 'U' and o.Name = 'table1' and c.Name not in ('Customer', 'Product')
ORDER BY c.Name desc for xml path ( '' ))
SET #columnsasc = ''
SELECT #columnsasc = (select + '[' + ltrim(c.Name) + ']' + ','
FROM sys.columns c
JOIN sys.objects o ON o.object_id = c.object_id
WHERE o.type = 'U' and o.Name = 'table1' and c.Name not in ('Customer', 'Product')
ORDER BY c.Name asc for xml path ( '' ))
SELECT #columnsasc = LEFT( #columnsasc,LEN(#columnsasc)-1)
SELECT #columnsdesc = LEFT( #columnsdesc,LEN(#columnsdesc)-1)
DECLARE #sql nvarchar(max)
SET #sql = 'SELECT Customer, Product, COALESCE('+ #columnsasc +') as Amount_Y1,
COALESCE(' + #columnsdesc +' ) as Amount_Y2
FROM Table1'
EXEC(#sql)
If you're dealing with a temporary table, then the code will change slightly:
Test it here: http://rextester.com/MRVR48808
DECLARE #columnsdesc nvarchar(max), #columnsasc nvarchar(max)
SET #columnsdesc = ''
SELECT #columnsdesc = (select + '[' + ltrim(c.Name) + ']' + ','
FROM tempdb.sys.columns c --Changes here
JOIN tempdb.sys.objects o ON o.object_id = c.object_id --Changes here
WHERE o.type = 'U' and o.Name like '#table1%' and c.Name not in ('Customer', 'Product') --Changes here
ORDER BY c.Name desc for xml path ( '' ))
SET #columnsasc = ''
SELECT #columnsasc = (select + '[' + ltrim(c.Name) + ']' + ','
FROM tempdb.sys.columns c --Changes here
JOIN tempdb.sys.objects o ON o.object_id = c.object_id --Changes here
WHERE o.type = 'U' and o.Name like '#table1%' and c.Name not in ('Customer', 'Product') --Changes here
ORDER BY c.Name asc for xml path ( '' ))
SELECT #columnsasc = LEFT( #columnsasc,LEN(#columnsasc)-1)
SELECT #columnsdesc = LEFT( #columnsdesc,LEN(#columnsdesc)-1)
DECLARE #sql nvarchar(max)
SET #sql = 'SELECT Customer, Product, COALESCE('+ #columnsasc +') as Amount_Y1,
COALESCE(' + #columnsdesc +' ) as Amount_Y2
FROM #Table1' --Changes here
EXEC(#sql)
Try using COALESCE as follows : For one field from beginning to end and for the second in the reverse manner.
SELECT Customer,Product, COALESCE([1999],[2000],[2001],[2002],[2003]) as Y1,
COALESCE([2003],[2002],[2001],[2000],[1999]) as Y2
FROM #TEMPDATA
I would do this using cross apply:
select t.customer, t.product, v.Amount_Y1, v.Amount_Y2
from t cross apply
(select max(case when which = 1 then val end) as Amount_Y1,
max(case when which = 2 then val end) as Amount_Y2
from (select val, yr, row_number() over (order by yr) as which
from (values (t.[1999], 1999), (t.[2000], 2000), (t.[2001], 2001),
(t.[2002], 2002), (t.[2003], 2003)
) v(val, yr)
where val is not null
) v

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

Use Value of a Column into another Column Name [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
Here are the values:
id | cat | AttribGroup | AttribID | Value
1 1 test 001 beautiful
1 1 test 002 handsome
Now what I what I want to happen is:
id | cat | test_001 | test_002
1 1 beautiful handsome
You may check this fiddle
It consists of a PIVOT and then join the results to the initial table.
Since i guess that your values are not going to be just test_001 and test_002 you are going to need some dynamic SQL in order to define the columns.
The code :
DECLARE #cols AS VARCHAR(MAX),
#query AS VARCHAR(MAX)
SET #cols = STUFF((SELECT distinct ',[' + AttribGroup + '_' + AttribID +']'
FROM Table1 c
FOR XML PATH(''), TYPE
).value('.', 'VARCHAR(MAX)')
,1,1,'')
SET #query =
';WITH MyCTE AS
(
SELECT *
FROM
(
SELECT AttribGroup + ''_'' + AttribID AS ColName,
CONVERT(VARCHAR,id)+''_''+CONVERT(VARCHAR,cat) AS idcat,
Value
FROM Table1
) p
PIVOT
(
MAX (Value)
FOR ColName IN (' + #cols + ')
) AS pvt
)
SELECT t.id,
t.cat,
m.*
FROM MyCTE m
JOIN
(
SELECT id,
cat,
CONVERT(VARCHAR,id)+''_''+CONVERT(VARCHAR,cat) as idcat
FROM Table1
GROUP BY id,
cat
) t
ON m.idcat = t.idcat'
EXEC(#query)

How do I dynamically pivot row values into columns without aggregare?

Lets say I have the following table representing results from a user survey system.
SurveyID ResponseID QuestionID Answer
-------- ---------- ---------- ------
1 1 1 'Answer 1'
1 1 2 'Answer 2'
1 1 3 'Answer 3'
1 2 1 'red'
1 2 2 'blue'
1 2 3 'green'
What I want is a pivoted output such that shown below.
SurveyID ResponseID Q1 Q2 Q3
-------- ---------- -- -- --
1 1 'Answer 1' 'Answer 2' 'Answer 3'
1 2 'red' 'blue' 'green'
I know how to achieve this if there were always only the same three questions but this database hosts multiple surveys which could have any number of unique QuestionIDs so I need the Q1, Q2, Q3 columns to be dynamic depending upon the number and IDs of that survey's questions.
I thought this would be a fairly standard problem but I cannot find anything that fully satisfies this issue. Any solution must work with SQL Server 2005.
Hope that makes sense. Thanks.
1) Pivots need an aggregate. You may know in advance that you are only interested in one row, but SQL doesn't know that. If you are only dealing with one row per group, just use MIN() as your aggregate.
2) Dynamic pivot is not a standard problem for SQL. That's a task for the presentation layer, not the data layer. You will have to use dynamic SQL, which still won't be able to handle an arbitrary number of columns and will open up injection attacks if you aren't careful.
If you still want to do it this way:
CREATE TABLE #t (Surveyid int, Responseid int, Questionid int, Answer varchar(max))
INSERT #t VALUES (1,1,1,'Answer1'),(1,1,2,'Answer2'),(1,1,3,'Answer3'),(1,2,1,'red'),(1,2,2,'blue'),(1,2,3,'green')
DECLARE #qids nvarchar(4000)
SELECT #qids = COALESCE(#qids+',','') + qid
FROM (SELECT DISTINCT QUOTENAME(Questionid) qid FROM #t) t
EXEC ('SELECT [SurveyID],[ResponseID],'+#qids+' FROM #t PIVOT(MIN(Answer) FOR Questionid IN('+#qids+')) p')
OK, finally discovered how to do this so thought I would share.
DECLARE #SurveyID SMALLINT;
DECLARE #SQL as VARCHAR(MAX);
DECLARE #Columns AS VARCHAR(MAX);
DECLARE #ColumnHeadings AS Varchar(MAX);
SET #SurveyID = 1;
SELECT
#Columns = COALESCE(#Columns + ', ','') + '[' + QuestionID + ']'
,#ColumnHeadings = COALESCE(#ColumnHeadings + ', ','') + '[' + QuestionID + '] AS [Q' + QuestionNumber + ']'
FROM
(
SELECT DISTINCT
CAST(a.QuestionID AS VARCHAR) AS QuestionID
,CASE WHEN q.QuestionNumber IS NULL THEN '' ELSE q.QuestionNumber END AS QuestionNumber
FROM dbo.Answers AS a
JOIN dbo.Questions AS q
ON a.QuestionID = q.ID
JOIN dbo.SurveyResponses AS r
ON a.ResponseID = r.ID
WHERE r.SurveyID = #SurveyID
)
SET #SQL = '
WITH PivotData AS
(
SELECT
a.QuestionID
,a.ResponseID
,a.Answer
FROM dbo.Answers AS a
JOIN dbo.SurveyResponses AS r
ON a.ResponseID = r.ID
)
SELECT
ResponseID
,' + #ColumnHeadings + '
FROM PivotData
PIVOT
(
MAX(Answer)
FOR QuestionID
IN (' + #Columns + ')
) AS PivotResult
ORDER BY ResponseID' ASC
EXEC (#SQL);

Make 1 row out of many rows from another table

I have a MSSQL database which in one table holds bio info about a person:
ID: Name : Age : Sex
In another table it holds their answers to a number of questions like this:
PersonID : QuestionID : Answer
Is it possible to display all of them via MSSQLMSE into one record like this:
ID : Name : Age : Sex : Question1Answer : Question2Answer : Question3Answer : And so on?
Try this:
SELECT ID, name, age, sex, Question1answer, Question2answer
FROM
(
SELECT
p.Id,
p.Name,
p.Age,
p.sex,
questionanswer = 'Question' + CAST(q.questionid AS VARCHAR(10)) + 'answer',
q.Answer
FROM Persons p
INNER JOIN Questions q ON p.Id = q.UserID
) t
PIVOT
(
MAX(Answer)
FOR questionanswer IN([Question1answer], [Question2answer])
) p;
SQL Fiddle Demo
This will give you:
| ID | NAME | AGE | SEX | QUESTION1ANSWER | QUESTION2ANSWER |
-----------------------------------------------------------------
| 1 | Ahmed | 25 | M | Yes | No |
| 2 | Mohammed | 30 | M | No | Never |
| 3 | Sara | 25 | F | No | Never |
However: If you want to do this dynamically for any number of questions per user, you can do this:
DECLARE #cols AS NVARCHAR(MAX);
DECLARE #query AS NVARCHAR(MAX);
select #cols = STUFF((SELECT distinct
',' +
QUOTENAME('Question' +
CAST(questionid AS VARCHAR(10)) +
'answer')
FROM questions
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'');
SET #query = 'SELECT ID, name, age, sex, ' + #cols +
' FROM
(
SELECT
p.Id,
p.Name,
p.Age,
p.sex,
questionanswer = ''Question'' +
CAST(q.questionid AS VARCHAR(10)) +
''answer'',
q.Answer
FROM Persons p
INNER JOIN Questions q ON p.Id = q.UserID
) t
PIVOT
(
MAX(Answer)
FOR questionanswer IN( ' + #cols + ') ) p ';
SQL Fiddle Dynamic Demo
select ID,Name,Age,Sex,
(select top 1 Answer from Questions
where PersonId=prerson.id AND QuestionID=1) as Question1Answer,
(select top 1 Answer from Questions
where PersonId=prerson.id AND QuestionID=2) as Question2Answer,
(select top 1 Answer from Questions
where PersonId=prerson.id AND QuestionID=3) as Question3Answer
from prerson
The PIVOT command in sql server would be appropriate to transpose table results, see http://msdn.microsoft.com/en-us/library/ms177410(v=sql.100).aspx
The script below works with a predefined set of questions. For a variable amount of columns this link would help http://blog-mstechnology.blogspot.com/2010/06/t-sql-pivot-operator-with-dynamic.html.
DECLARE #Person TABLE(
ID INT,
Name VARCHAR(50),
Age INT,
Sex CHAR(1)
)
DECLARE #Questions TABLE(
ID INT,
QuestionID INT,
Question VARCHAR(200),
Answer VARCHAR(200)
)
INSERT INTO #Person VALUES
(1,'Andrew',56,'M'),
(2,'Marge',65,'F')
INSERT INTO #Questions VALUES
(1,1,'Question1','Andrews Answer 1'),
(1,2,'Question2','Andrews Answer 2'),
(2,1,'Question1','Marge Answer 1'),
(2,3,'Question3','Marge Answer 3')
SELECT ID,Age,Name,Sex,
[1] AS 'Question1Answer',
[2] AS 'Question2Answer',
[3] AS 'Question3Answer'
FROM(
SELECT P.ID,P.Age,P.Name,P.Sex,Q.QuestionID,Q.Answer
FROM #Person P
INNER JOIN #Questions Q ON Q.ID = P.ID
) Source
PIVOT
(
MAX(Answer)
FOR QuestionID IN ([1],[2],[3])
)AS PT
Try this:
SELECT
ID,
Name,
Age,
Sex,
[1] AS Question1Answer,
[2] AS Question1Answer,
[3] AS Question1Answer
FROM
(SELECT
i.ID,
i.Name,
i.Age,
i.Sex,
q.QuestionId,
q.Answer
FROM
dbo.Info AS i
INNER JOIN dbo.Questions AS q on q.ID = i.ID) AS m
PIVOT
(
MAX(Answer) FOR QuestionId in ([1],[2],[3])
) as PivotTable
I think this (SQL Fiddle here) is what you are looking for. For a given person id;
Note: p = Person Table, a = Answer Table and qId = QuestionId
DECLARE #S VARCHAR(Max)
DECLARE #PersonId INT = 1
SELECT #S=ISNULL(#S+' : ','') + convert(varchar(10), id) +' : '+ name
+' : '+ convert(varchar(10), age)+' : '+sex
FROM p
WHERE p.Id = #PersonId
SELECT #S=ISNULL(#S+' : ','') +
'Question' + convert(varchar(10), qid) +'Answer : '+ answer
FROM a
WHERE pId = #PersonId
SELECT #S
--Following results is in one line
--1 : X : 25 : M : Question11Answer : Here is my answer for 11 :
--Question12Answer : Here is my answer for 12 :
--Question101Answer : Here is my answer for 101
Try this method: (Set compatability level of database to 90 or higher)
begin
Declare #MainSQL nVarchar(Max)
Declare #Param nVarchar(Max)
Declare #QId int
set #Param = ''
Declare curParam Cursor For select Distinct QuestionID from Questions
open curParam
Fetch next from curParam into #QId
While ##Fetch_Status = 0
Begin
set #Param = #Param +'[' +convert(nVarchar,#QId ) + '],'
Fetch next from curParam into #QId
End
close curParam;
Deallocate curParam;
set #Param = left(#Param,len(#Param)-1)
Set #MainSQL = 'Select ID,Name,Age,***
From
(select P.ID,Name,Age,
Q.QuestionID, Q.Answer From Person P Left outer join
Questions Q
ON P.ID = Q.PersonID) S
Pivot
(Max(Answer)
For QuestionID in(***)
) as Pvt'
Set #MainSQL =replace(#MainSQL,'***',#Param);
--print #MainSQL
execute sp_executesql #MainSQL
End