Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
subhaoviya
Posting Yak Master
135 Posts |
Posted - 2012-01-04 : 02:59:04
|
| i am having 1 mapping table and two look up tables.mapping table -> map1lookuptables --> layTable, CPDtablethe mapping table referring laytable's PK and CPDtable's PK.the scenario is like map1 table referring 5 times of laytable's PK for single record.eg)create table map1(ID number,Layid1 number,layid2 number,layid3 number,layid4 number,layid5 number,CPDID number)create table LayTable(LayID number,SourceName varchar2(100))create table CPDTable(cpdid number,cpdName Varchar2(100))insert into laytableselect 1,'phone1' from dualunion allselect 2,'phone2' from dualunion allselect 3,'phone3' from dualunion allselect 4,'phone4' from dualunion allselect 5,'phone5' from dualinsert into map1select 1,1,2,3,4,5,1 from dualinsert into cpdtableselect 1,'phone' from duali formed a query likeselect c.cpdname as CPDname ,case when m.layid1=l.layid then sourcename END as SC1 ,case when m.layid2=l.layid then sourcename END as SC2,,case when m.layid3=l.layid then sourcename END as SC3,case when m.layid4=l.layid then sourcename END as SC4,case when m.layid5=l.layid then sourcename END as SC5 from map1 m,laytable L,cpdtable Cwhere (m.layid1=L.layidor m.layid2=l.layidor m.layid3=l.layidor m.layid4=l.layidor m.layid5=l.layid) and m.CPDID=C.CPDID(+) i am getting output ascpdname sc1 sc2 sc3 sc4 sc5phone phone1 phone phone2phone phone3.....but i am expectingcpdname sc1 sc2 sc3 sc4 sc5phone phone1 phone2 phone3 phone4 phone5how to form a queryplease help methanks in advance subha |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-04 : 06:02:54
|
| [code]select c.cpdName,lt1.SourceName AS [sc1],lt2.SourceName AS [sc2],lt3.SourceName AS [sc3],lt4.SourceName AS [sc4],lt5.SourceName AS [sc5]from map1 minner join laytable lt1on lt1.LayID = m.Layid1 inner join laytable lt2on lt2.LayID = m.Layid2 inner join laytable lt3on lt3.LayID = m.Layid3 inner join laytable lt4on lt4.LayID = m.Layid4 inner join laytable lt5on lt5.LayID = m.Layid5 inner join CPDTable cON c.cpdid = m.cpdid [/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
subhaoviya
Posting Yak Master
135 Posts |
Posted - 2012-01-04 : 06:28:44
|
| Its working thanks friend. |
 |
|
|
Ranjit.ileni
Posting Yak Master
183 Posts |
Posted - 2012-01-04 : 07:09:37
|
| also select c.cpdname ,max(case when m.Layid1=l.layid then l.SourceName end) as sc1 ,max(case when m.Layid2=l.layid then l.SourceName end) as sc2 ,max(case when m.Layid3=l.layid then l.SourceName end) as sc3 ,max(case when m.Layid4=l.layid then l.SourceName end) as sc4 ,max(case when m.Layid5=l.layid then l.SourceName end) as sc5from map1 minner join cpdtable c on c.cpdid=m.cpdidleft join laytable l on 1=1group by cpdname--Ranjit |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-04 : 12:21:58
|
quote: Originally posted by Ranjit.ileni also select c.cpdname ,max(case when m.Layid1=l.layid then l.SourceName end) as sc1 ,max(case when m.Layid2=l.layid then l.SourceName end) as sc2 ,max(case when m.Layid3=l.layid then l.SourceName end) as sc3 ,max(case when m.Layid4=l.layid then l.SourceName end) as sc4 ,max(case when m.Layid5=l.layid then l.SourceName end) as sc5from map1 minner join cpdtable c on c.cpdid=m.cpdidleft join laytable l on 1=1group by cpdname--Ranjit
why do you need an inner join on trivial condition which ie effectively a cross join?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|