MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old June 8th, 2004, 07:44 AM
asmo asmo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 22 asmo User rank is Private First Class (20 - 50 Reputation Level)asmo User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
the most recent address from each user by date

Alright here's a table I have:

Id Effective Addy
1 10/10/2003 123 Test Lane
1 12/10/2003 No Exist Court
1 1/5/2004 Here I am!
2 6/6/2003 Some place
3 12/25/2003 Old place
3 1/30/2004 Now Place

I want to take the most recent address from each user by date- basically, I want this returned:

Id Addy
1 Here I am!
2 Some place
3 Now Place.

So I want each user's 'current' address. Is there a way in SQL to do this?

EDIT: I kind of changed my question, I just want the Id and the current Addy.

Can anyone give me a hand? Thanks a lot!

Reply With Quote
  #2  
Old June 8th, 2004, 07:57 AM
asmo asmo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 22 asmo User rank is Private First Class (20 - 50 Reputation Level)asmo User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Okay- here's where I'm at:

SELECT test.Id, FIRST(test.Addy) AS CurrAddy
FROM test
GROUP BY test.Id, test.Effect
ORDER BY test.Id,test.Effect DESC;


That gives me:

Id CurrAddy
1 Here I am!
1 No Exist Court
1 123 Test Lane
2 Some place
3 Now Place
3 Old place

That's almost what I want, now I just need the top 1 of each Id number. Any suggestions?

Reply With Quote
  #3  
Old June 8th, 2004, 08:43 PM
brightlight brightlight is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 371 brightlight User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 32 m 27 sec
Reputation Power: 5
Hi,

Following is the MS SQL Query. Change the table name and the column names accordingly

Quote:

SELECT *
FROM tb1 a
WHERE (Effective =
(SELECT MAX(effective)
FROM tb1 b
WHERE a.id = b.id))
ORDER BY id

__________________
Regards,
Brightlight

Reply With Quote
  #4  
Old June 9th, 2004, 01:16 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
This could also work...
Code:
select	ID, Addy
from	Table 1 T1
join	(
	 select	ID, max(Effective) as maxEffective
	 from	Table1 T
	 group	by ID
	) as A
  on	T1.ID = A.ID and T1.Effective = A.maxEffective

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > the most recent address from each user by date


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway