dbTalk Databases Forums  

A simple query that returns the most current address

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss A simple query that returns the most current address in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Rex
 
Posts: n/a

Default A simple query that returns the most current address - 07-23-2007 , 09:26 PM






Hi,

I have an Address table which contains more than one addresses for a
particular member. I want to write a query that would only display
most current address. All addresses have a unique ID (addID).
Example:

memberID addID address1
-------- ------ --------------------------------------------------
295 69 13 Auster St
295 70 465 Lorre Ct
295 71 P.O. Box 321
722 171 10 Hannaford Rd
722 172 Dubai, United Arab Emirates

Quote:
From the table data above. The query should only return
memberID addID address1
-------- ------ --------------------------------------------------
295 71 P.O. Box 321
722 172 Dubai, United Arab Emirates

I tried using Max and Group by function but it shows me all the rows.
If you can provide me with a sample code that would greatly
appreciated.

cheers



Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: A simple query that returns the most current address - 07-23-2007 , 10:07 PM






Here are a few ways:

-- SQL Server 2000
SELECT memberID, addID, address1
FROM Addresses AS A
WHERE addID IN (
SELECT TOP 1 addID
FROM Addresses AS A1
WHERE A1.memberID = A.memberID
ORDER BY A1.addID DESC)

-- or
SELECT memberID, addID, address1
FROM Addresses AS A
WHERE addID = (
SELECT MAX(addID)
FROM Addresses AS A1
WHERE A1.memberID = A.memberID)

-- SQL Server 2005
;WITH cte
(memberID, addID, address1, rn)
AS
(
SELECT memberID, addID, address1,
ROW_NUMBER() OVER(
PARTITION BY memberID
ORDER BY addID DESC)
FROM Addresses
)
SELECT memberID, addID, address1
FROM cte
WHERE rn < 2;


HTH,

Plamen Ratchev
http://www.SQLStudio.com




Reply With Quote
  #3  
Old   
Ed Murphy
 
Posts: n/a

Default Re: A simple query that returns the most current address - 07-23-2007 , 11:07 PM



Rex wrote:

Quote:
I have an Address table which contains more than one addresses for a
particular member. I want to write a query that would only display
most current address. All addresses have a unique ID (addID).
Example:

memberID addID address1
-------- ------ --------------------------------------------------
295 69 13 Auster St
295 70 465 Lorre Ct
295 71 P.O. Box 321
722 171 10 Hannaford Rd
722 172 Dubai, United Arab Emirates

From the table data above. The query should only return

memberID addID address1
-------- ------ --------------------------------------------------
295 71 P.O. Box 321
722 172 Dubai, United Arab Emirates

I tried using Max and Group by function but it shows me all the rows.
If you can provide me with a sample code that would greatly
appreciated.
Assuming that each member's most current address has the largest
addID value, and that addID values are not re-used from one member
to the next:

select memberID, addID, address1
from the_table
where addID in (
select max(addID)
from the_table
group by memberID
)


Reply With Quote
  #4  
Old   
Rex
 
Posts: n/a

Default Re: A simple query that returns the most current address - 07-23-2007 , 11:46 PM



On Jul 24, 12:07 pm, "Plamen Ratchev" <Pla... (AT) SQLStudio (DOT) com> wrote:
Quote:
Here are a few ways:

-- SQL Server 2000
SELECT memberID, addID, address1
FROM Addresses AS A
WHERE addID IN (
SELECT TOP 1 addID
FROM Addresses AS A1
WHERE A1.memberID = A.memberID
ORDER BY A1.addID DESC)

-- or
SELECT memberID, addID, address1
FROM Addresses AS A
WHERE addID = (
SELECT MAX(addID)
FROM Addresses AS A1
WHERE A1.memberID = A.memberID)

-- SQL Server 2005
;WITH cte
(memberID, addID, address1, rn)
AS
(
SELECT memberID, addID, address1,
ROW_NUMBER() OVER(
PARTITION BY memberID
ORDER BY addID DESC)
FROM Addresses
)
SELECT memberID, addID, address1
FROM cte
WHERE rn < 2;

HTH,

Plamen Ratchevhttp://www.SQLStudio.com
Thanks a lot



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.