SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mmkrishna1919
Yak Posting Veteran

India
88 Posts

Posted - 03/14/2013 :  10:17:50  Show Profile  Reply with Quote
Hi All,

I have two tables
table 1:
cno pno vno
100 10 001
100 10 002
100 10 003
200 20 001
200 21 001
200 22 001

table 2:
cno amt
100 1245
200 1500


i join these two tables based on cno i will get 6 rows in result set.

but my expected result set is:

cno pno vno cno amt
100 10 001 100 1245
200 20 001 200 1500

i need reocrds with cno=01 from table2

i tried with below way,is it correct or any other best approach is there?

select * from table1 t inner join table2 t2
on t.cno=t2.cno
and t.vno=(select min(t3.vno) from tabel1 t3
where t.cno=t3.cno
and t.pno=t3.pno)
and t.pno=(select min(t3.pno) from table1 t3
where t.cno=t3.cno
and t.vno=t3.vno)
Thanks..

M.MURALI kRISHNA

basicconfiguration
Constraint Violating Yak Guru

356 Posts

Posted - 03/14/2013 :  16:59:03  Show Profile  Reply with Quote
do you need records only for vno=001 and pno = 20, 10?
then

select a.*, b.* from table1 a join table2 b
on a.cno = b.cno
and a.vno = '001'
and a.pno in (10, 20)

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000