General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesGeneral SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
  #1  
Old March 31st, 2005, 02:29 PM
Euclid Euclid is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 1 Euclid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 2 sec
Reputation Power: 0
Selecting Max Date for Many Records

I've got a table that's a list of clients waiting in line. Let's say the table has 4 fields:
(obligatory unique id),client name,worker, time

As the client's make their way though the facility, they see different workers and each worker makes a new entry in this table, with the current time, showing when they saw the client. Later on, this data will be used to analyse where clients spend the most time, so we can't overwrite any records.

What I need is a query that gets only the newest entry for each client.

so in a table like this:
bob, worker1, 1pm
bob, worker2, 2pm
jim, worker1, 3pm
jim, worker2, 4pm
jim, worker3, 5pm

The correct query would return only:
bob, worker2, 2pm
jim, worker3, 5pm

I've tried several different things with Max(time), and intersections, but I can't find a good solution. Surely there's a way to put it all in one sql statement.

Anyone have any ideas?
Thanks!

Reply With Quote
  #2  
Old April 25th, 2005, 01:25 PM
dejaone dejaone is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 34 dejaone User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 11 sec
Reputation Power: 4
try "group by" on client field and max(time) in "where" clause.

Reply With Quote
  #3  
Old April 25th, 2005, 06:52 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 4
You cannot use aggregate functions in WHERE clauses. Try a subquery:

Code:
 SELECT
  client name,
  worker,
  `time`
 FROM
  yourtable
 WHERE
  `time` IN
 	(SELECT
 	   MAX(`time`)
 	 FROM
 	   yourtable
 	 GROUP BY
 	   client name)
 


I realize this is probably is pseudotable, but I'd like to point out a couple of other things. TIME is a reserved word, so it's best not to use it as a column name. Also, I wouldn't recommend storing times as you've indicated here -- you should use a TIME type. If you store them as nAM/PM strings, your MAX() function won't work correctly.
__________________
"A pawn is the most important piece on the chessboard -- to a pawn"


Reply With Quote
  #4  
Old March 28th, 2008, 09:29 AM
casbornsen casbornsen is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 1 casbornsen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m
Reputation Power: 0
Thanks!

I had a problem with Army MOTEs having multiple dates on a single table and I only wanted to use the most recent one. The subquery you posted worked great! Hooah!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > Selecting Max Date for Many Records


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 1 hosted by Hostway