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 2005 Forums
 Transact-SQL (2005)
 select only one record

Author  Topic 

sivaarc
Starting Member

9 Posts

Posted - 2009-03-09 : 11:58:07
i have a tables customer and customer address .
Each customer have a one are many address. But i have to list only one address for one customer.. How can i achive this using sql query?
---------------------
Customer Year
-------------------------
aa 2005
bb 2009
cc 2008

--------------------------
Customer Address City
------------------------
aa 1 aa
aa 2 bb
bb 1 aa
bb 2 cc
cc 1 bb


list should be like

aa 2005 1 aa
bb 2009 1 aa
cc 2008 1 bb

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-03-09 : 12:04:10
Can you show what you tried atleast?
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-03-09 : 12:04:33
something like this..

select a.customer,a.year,b.address,b.city
from customertable a inner join addresstable b on a.customer = b.customer and b.address = 1
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-03-10 : 00:10:38
try like this if ur using 2005 and above
select customer,[year],address,city from
(select row_number()over(partition by b.customer order by a.customer)as rid,a.customer,a.year,b.address,b.city
from customertable a inner join addresstable b on a.customer = b.customer )t where rid = 1
Go to Top of Page

sivaarc
Starting Member

9 Posts

Posted - 2009-03-10 : 01:36:46
thank you......


quote:
Originally posted by bklr

try like this if ur using 2005 and above
select customer,[year],address,city from
(select row_number()over(partition by b.customer order by a.customer)as rid,a.customer,a.year,b.address,b.city
from customertable a inner join addresstable b on a.customer = b.customer )t where rid = 1

Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-03-10 : 01:47:30
quote:
Originally posted by sivaarc

thank you......


quote:
Originally posted by bklr

try like this if ur using 2005 and above
select customer,[year],address,city from
(select row_number()over(partition by b.customer order by a.customer)as rid,a.customer,a.year,b.address,b.city
from customertable a inner join addresstable b on a.customer = b.customer )t where rid = 1





welcome
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-03-10 : 03:02:24
Also refer
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/12/return-top-n-rows.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -