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
 gathering a single record
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

seeker62
Starting Member

40 Posts

Posted - 04/17/2013 :  11:59:18  Show Profile  Reply with Quote
I have many contracts and each one has many shipping orders. I want to develop a query that would give me one shipping order for each contract. I have used

select so from sotable sot inner join contracttable ct on sot.contract = ct.contract where exists (select top 1 so from sotable subsot inner join contracttable subct where ct.contractnum = subct.contractnum)

this still gives me all the so for the contract. only want 1 so per contract to show in query

James K
Flowing Fount of Yak Knowledge

3327 Posts

Posted - 04/17/2013 :  12:13:21  Show Profile  Reply with Quote
quote:
Originally posted by seeker62

I have many contracts and each one has many shipping orders. I want to develop a query that would give me one shipping order for each contract. I have used

select so from sotable sot inner join contracttable ct on sot.contract = ct.contract where exists (select top 1 so from sotable subsot inner join contracttable subct where ct.contractnum = subct.contractnum)

this still gives me all the so for the contract. only want 1 so per contract to show in query

If all you want is one so per contract, this should do it. But don't you need other columns, such as which contract etc.?
select max(so) from sotable group by contract
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 04/18/2013 :  02:33:58  Show Profile  Reply with Quote

select your required columns...
from
(
select *,max(so) over (partition by contract) as maxso from sotable
)t
where maxso = so


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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