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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 inner join select top

Author  Topic 

cjhardie
Yak Posting Veteran

58 Posts

Posted - 2006-09-13 : 16:10:23
I am working on a query to make my name column show the top 1 and i cant figure it out any help??

select b.businessname,a.address1, isnull(a.address2,'') as address2,a.city,sta.stateabbr, a.postalcode,co.countryname,c.firstname + c.lastname,e.emailaddress,p.areacode,p.phonenumber,so.supplyorderid,so.orderdate,s.supplyid,st.shippingtypedesc,sod.quantity,sod.price
from business b
inner join supplyorder so on b.businessid = so.businessid and so.deletedate is null
inner join address a on so.addressid = a.addressid and a.deletedate is null
inner join state sta on a.stateid = sta.stateid
inner join supplyorderdetail sod on so.supplyorderid = sod.supplyorderid and sod.deletedate is null
inner join shippingtype st on so.shippingtypeid = st.shippingtypeid and st.deletedate is null
inner join supply s on sod.supplyid = s.supplyid and s.deletedate is null
inner join contact c on c.businessid = (select top 1 c1.firstname from contact c1 where c1.businessid = b.businessid)
inner join email e on c.contactid = e.contactid
inner join phone p on c.contactid = p.contactid
inner join country co on sta.countryid = co.countryid

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-09-13 : 16:22:55
This should do it

select b.businessname,a.address1, isnull(a.address2,'') as address2,a.city,sta.stateabbr, a.postalcode,co.countryname,c.firstname + c.lastname,e.emailaddress,p.areacode,p.phonenumber,so.supplyorderid,so.orderdate,s.supplyid,st.shippingtypedesc,sod.quantity,sod.price
from business b
inner join supplyorder so on b.businessid = so.businessid and so.deletedate is null
inner join address a on so.addressid = a.addressid and a.deletedate is null
inner join state sta on a.stateid = sta.stateid
inner join supplyorderdetail sod on so.supplyorderid = sod.supplyorderid and sod.deletedate is null
inner join shippingtype st on so.shippingtypeid = st.shippingtypeid and st.deletedate is null
inner join supply s on sod.supplyid = s.supplyid and s.deletedate is null
inner join contact c on c.contactid = (select max(c1.contactid) from contact c1 where c1.businessid = b.businessid)
inner join email e on c.contactid = e.contactid
inner join phone p on c.contactid = p.contactid
inner join country co on sta.countryid = co.countryid


I'm assuming you want to just show one contact for the business, I used the max contact id, you could change that to the min one.
Go to Top of Page

cjhardie
Yak Posting Veteran

58 Posts

Posted - 2006-09-13 : 16:38:07
Thanks I just changed it to top 1 instead...states the same thing its just that we use select top more because we use numbers also.
But thanks for the help
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2006-09-13 : 21:11:56
You realise that "TOP 1" is essentially a random row? At least min() max() gives you consistency.
I'd always be suspect of requirements that can be satisfied by providing random data.
Go to Top of Page

cjhardie
Yak Posting Veteran

58 Posts

Posted - 2006-09-14 : 09:35:25
Yeah I know that it is random, but I just need one out of each and it doesnt matter which one.
Go to Top of Page
   

- Advertisement -