Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

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

Yak Posting Veteran

95 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)


Constraint Violating Yak Guru

358 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?

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  
 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.14 seconds. Powered By: Snitz Forums 2000