Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Flatten rows in a join


Flatten rows in a join

Author
Message
tyson.price
tyson.price
SSC Journeyman
SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)

Group: General Forum Members
Points: 75 Visits: 642
I have a stored procedure that currently brings back one row per policy number. A new table has been introduced that needs to be joined to that stored procedure. That table currently has six entries per policy but that can increase or decrease. This is sort of what I am looking at.

Although it is much more complex I think this will illustrate the issue:

create table #Policies(PolicyNo int, SomeData Char(5))
insert into #Policies(policyno,somedata) values (1,'abcd')

create table #NewTable(PolicyNo int,NewData char(5),akey int)
insert into #NewTable(PolicyNo,NewData,akey) values(1, 'aaaa',1)
insert into #NewTable(PolicyNo,NewData,akey) values(1, 'bbbb',2)
insert into #NewTable(PolicyNo,NewData,akey) values(1, 'cccc',3)
insert into #NewTable(PolicyNo,NewData,akey) values(1, 'dddd',4)



Lets say this is the current stored procedure:

select * from #Policies p where policyno = 1

I need to join #NewTable to it:

select p.policyno,t.Newdata,t.akey from #Policies p
join #NewTable t on t.policyno = p.policyno
where p.policyno = 1

But I need the output to look like this:

PolicyNo NewData akey NewData akey NewData akey NewData akey

1 aaaa 1 bbbb 2 cccc 3 dddd 4

Any ideas assuming at this point I can't change the table structure. If the stored procdure always brought back one row I could flatten #NewTable first and join to that. The real issue is the stored procedure could bring back many policies and each one needs to be one row per policy with the #NewTable data flattened out per policy row

EDIT #NewTable is dynamic so the number of entries is not fixed.
Phil Parkin
Phil Parkin
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8381 Visits: 19501
It looks like a dynamic crosstab query might help. Have a look here for an example.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
tyson.price
tyson.price
SSC Journeyman
SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)

Group: General Forum Members
Points: 75 Visits: 642
What I ended up doing just for expediency’s sake is:

In the select:

,'GR' + convert(Varchar,t1.pkRate_Barcodes) 'ac2'
,'GR' + convert(Varchar,t2.pkRate_Barcodes) 'ac26'
,'GR' + convert(Varchar,t3.pkRate_Barcodes) 'GrpRatCont'
,'GR' + convert(Varchar,t4.pkRate_Barcodes) 'EnrollQuest'
,'GR' + convert(Varchar,t5.pkRate_Barcodes) 'EnrollQuestInv'
,'GR' + convert(Varchar,t6.pkRate_Barcodes) 'U-153'



The join:

join Rate_BarCodes t1 on gr.EstExperienceYear = t1.EstExperienceyear 
and ge.fkgroup = t1.fkgroup and t1.document_type_name = 'AC-2'
and e.policyno = t1.policyno

join Rate_BarCodes t2 on gr.EstExperienceYear = t2.EstExperienceyear
and ge.fkgroup = t2.fkgroup and t2.document_type_name = 'AC-26'
and e.policyno = t2.policyno

join Rate_BarCodes t3 on gr.EstExperienceYear = t3.EstExperienceyear
and ge.fkgroup = t3.fkgroup and t3.document_type_name = 'GrpRatCont'
and e.policyno = t3.policyno

join Rate_BarCodes t4 on gr.EstExperienceYear = t4.EstExperienceyear
and ge.fkgroup = t4.fkgroup and t4.document_type_name = 'EnrollQuest'
and e.policyno = t4.policyno

join Rate_BarCodes t5 on gr.EstExperienceYear = t5.EstExperienceyear
and ge.fkgroup = t5.fkgroup and t5.document_type_name = 'EnrollQuestInv'
and e.policyno = t5.policyno

join Rate_BarCodes t6 on gr.EstExperienceYear = t6.EstExperienceyear
and ge.fkgroup = t6.fkgroup and t6.document_type_name = 'U-153'
and e.policyno = t6.policyno



I had left out part of the requirements. Each column name from the #NewTable had to have the column name in the select. That is becuase it needs to be exposed to Crystal reports.

I didn't check into cross tabs or pivot.


Thanks to those that replied.
adrian.facio
adrian.facio
Old Hand
Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)Old Hand (342 reputation)

Group: General Forum Members
Points: 342 Visits: 386
I think it's ok that you write queries fast, but just remember that quickfixes sometimes end up stablished as final solution when there is better options available.
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