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 |
|
mrsaif
Starting Member
43 Posts |
Posted - 2006-06-02 : 06:20:19
|
| I have 2 tablesA---------------------------------------------------------------id Name1 saif2 Waqar3 Rajo4 Kami5 Mati6 Vati7 Coti8 Foti9 Kati10 Qati______________________________________________________________another tableB --------------------------------------------------------------Bid Cid1 12 23 34 15 16 37 38 4=========================================i want to get all records from A whose (id in Bid and cid = 1) and all cid which does not exits in BI want these resultsId Name1 saif4 Kami5 Mati9 Kati10 Qati=========================How can i do this?????????Muhammad Saifullah |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-02 : 06:28:56
|
| Select * from A T where not exists(select * from B where bid=T.id)UnionSelect * from A T where exists(select * from B where cid=T.id)MadhivananFailing to plan is Planning to fail |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-02 : 06:38:45
|
Madhivanan - I think that's not right. Mrsaif - try this...  --datadeclare @A table (id int identity(1, 1), Name varchar(10))insert @A select 'saif'union all select 'Waqar'union all select 'Rajo'union all select 'Kami'union all select 'Mati'union all select 'Vati'union all select 'Coti'union all select 'Foti'union all select 'Kati'union all select 'Qati'declare @B table (Bid int, Cid int)insert @B select 1, 1union all select 2, 2union all select 3, 3union all select 4, 1union all select 5, 1union all select 6, 3union all select 7, 3union all select 8, 4--calculationSelect * from @A A where exists (select * from @B where Bid = A.id and Cid = 1) or not exists (select * from @B where bid = A.id)/*resultsid Name ----------- ---------- 1 saif4 Kami5 Mati9 Kati10 Qati*/ Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
mrsaif
Starting Member
43 Posts |
Posted - 2006-06-02 : 06:56:05
|
| Thanks Madhivanan and RyanRandall for you kind suggestions But it does not work for me i want to do this in one Query. Now i got the querywhich is select A.* from A left outer join B on a.id = b.id where (b.cid=1 or cid is null)Muhammad Saifullah |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-02 : 07:06:05
|
Well, what I gave should have worked for you, but what you have there is better (that's what I get for 'half-copying' Madhivanan ). So, well done I should have used this in my 'calculation' part...Select a.* from @A a left outer join @B b on a.id = b.bid where (b.cid = 1 or b.cid is null) Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-02 : 07:31:06
|
Thanks for the correction MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|