SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to use dynamic pivot on this data


How to use dynamic pivot on this data

Author
Message
NineIron
NineIron
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7563 Visits: 837
I've tried to understand dynamic sql but, to no avail. Could you tell me how to write the code that will pivot the diagnoses and use the DiagnosisUrnID as column headings. Maybe Diag1, Diag2, Diag3........
Then, a short description of what the different steps of the query are doing?


create table #T

(

account_num varchar(10),

DiagnosisUrnID int,

Diagnosis varchar(10)

)

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('a1111',1,'Z12.11')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('a1112',2,'K62.1')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('a1113',3,'D12.3')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('a1114',4,'D12.2')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('a1115',5,'K57.30')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('a1116',6,'K64.0')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('a1117',7,'I10')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('a1118',8,'E78.5')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('a1119',9,'E66.9')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('a1120',10,'Z68.30')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('a1121',11,'F32.9')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('b2222',1,'M65.331')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('b2223',2,'E11.40')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('b2224',3,'Z79.84')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('c3333',1,'Z30.432')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('c3334',2,'F17.210')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('d4444',1,'G56.21')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('d4445',2,'G56.01')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('d4446',3,'F17.210')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('d4447',4,'I10')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('d4448',5,'E78.5')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('d4449',6,'E11.42')

insert into #T(account_num, DiagnosisUrnID, Diagnosis) values('d4450',7,'Z79.84')



Thom A
Thom A
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59507 Visits: 17701
With the data you've provided, could you show what you want your expected result set to be?


Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
John Mitchell-245523
John Mitchell-245523
SSC Guru
SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)

Group: General Forum Members
Points: 106465 Visits: 18617
Step by step dynamic pivot script? Your wish is my command. It's all out there for you if you search for it.

John
NineIron
NineIron
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7563 Visits: 837
Thom A - Friday, February 2, 2018 8:51 AM
With the data you've provided, could you show what you want your expected result set to be?
Attached is what I need it to look like.
Thanx.

Attachments
example.txt (22 views, 281 bytes)
NineIron
NineIron
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7563 Visits: 837
John Mitchell-245523 - Friday, February 2, 2018 8:52 AM
Step by step dynamic pivot script? Your wish is my command. It's all out there for you if you search for it.

John


Sorry for imposing. Some of us struggle with this stuff. I've found several examples and explanations but, there's nothing like having someone explain, using your own example.
John Mitchell-245523
John Mitchell-245523
SSC Guru
SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)SSC Guru (106K reputation)

Group: General Forum Members
Points: 106465 Visits: 18617
You're not imposing. It's just that it's easier to assist someone who wants help with a specific issue, rather than asking for an end-to-end solution. You should try working through that article I linked to - you may find it's closer to your situation than you imagine. And if it isn't, then please do ask for help.

By the way, might it be possible to put an upper limit on the number of columns? If you could do that, you wouldn't need dynamic SQL and your problem would get a lot simpler.

John
NineIron
NineIron
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7563 Visits: 837
John Mitchell-245523 - Friday, February 2, 2018 9:24 AM
You're not imposing. It's just that it's easier to assist someone who wants help with a specific issue, rather than asking for an end-to-end solution. You should try working through that article I linked to - you may find it's closer to your situation than you imagine. And if it isn't, then please do ask for help.

By the way, might it be possible to put an upper limit on the number of columns? If you could do that, you wouldn't need dynamic SQL and your problem would get a lot simpler.

John


I found that link during my search but, still couldn't get it. I tried putting an upper limit on the number of columns but, the user insists that they need all of them.
pietlinden
pietlinden
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42244 Visits: 16083
NineIron,
Are you doing the summary for this stuff in Access or where? I'm only asking because doing this in SSRS is stupid easy (use a Matrix)
Pieter
NineIron
NineIron
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7563 Visits: 837
pietlinden - Friday, February 2, 2018 10:05 AM
NineIron,
Are you doing the summary for this stuff in Access or where? I'm only asking because doing this in SSRS is stupid easy (use a Matrix)
Pieter


The results are going to be sftp'd to an outside vendor.
pietlinden
pietlinden
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42244 Visits: 16083
Oh, so are you doing that through SSIS?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search