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 |
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-02-28 : 17:00:40
|
| I need the help in getting the output.if A1 exists in the @prod3 table then the @prod2 table prod2 value for the respective sid should be A1Please help me in getting the desired output.thanks for yur help in advance..declare @Prod1 table(Csid bigint,sid bigint)insert into @Prod1select 4103, 101 union allselect 4101, 195 union allselect 4103, 101 Union allselect 4104, 201 Union allselect 4105, 202 declare @Prod2 table(sid int,Prod2 char(2))insert into @Prod2select 101, 'T1' union allselect 101, 'T2' union allselect 195, 'T5' Union allSelect 201, 'T3' Union allSelect 202, 'T1' declare @Prod3 table(sid int,Prod3 char(2))insert into @Prod3select 101, '72' union allselect 195, 'A5' Union allSelect 201, 'A3' Union allSelect 202, 'A1' Below is the query--case when max(p3.prod3)='A1' then p2.Prod2='A1'select p1.Csid, p1.sid, prod2 = case when count(*) > 1 then '4' when max(p2.Prod2) = 'T5' then '1'when max(p2.Prod2) = 'T3' then '2'when max(p2.Prod2) = 'T1' then '5'else max(p2.Prod2) end,p3.prod3from @Prod1 p1 inner join @Prod2 p2 on p1.sid = p2.sid inner join @prod3 p3 on p3.sid=p1.sidgroup by p1.Csid, p1.sid,p3.prod3 Output:Csid sid prod2 -------------------- -------------------- ----- 4103 101 4 4101 195 14104 201 24105 202 A1 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2007-02-28 : 17:31:29
|
I might have misunderstood your question, but won't this do what you want?select p1.Csid, p1.sid, prod2 = case when max(p3.prod3) = 'A1' then 'A1'when count(*) > 1 then '4' when max(p2.Prod2) = 'T5' then '1'when max(p2.Prod2) = 'T3' then '2'when max(p2.Prod2) = 'T1' then '5'else max(p2.Prod2) end,p3.prod3from @Prod1 p1 inner join @Prod2 p2on p1.sid = p2.sidinner join @prod3 p3 on p3.sid=p1.sidgroup by p1.Csid, p1.sid,p3.prod3 |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-02-28 : 19:41:58
|
| Thanks it works!! |
 |
|
|
|
|
|
|
|