If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > how do I return the highest date per group

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-25-04, 12:28
jdoyle jdoyle is offline
Registered User
 
Join Date: Oct 2003
Posts: 24
how do I return the highest date per group

I need to build a unit history table to track daily changes in an unit operations table. I want to compare a copy of the operations table to a result set containing the entries with the latest date for each unit from the history table, delete duplicates from the copy table and insert the remainder into the history table.

My problem is the query returning the entries with the latest date per unit. I know how to return all units with the max date in the table but this misses any units that didn't change on the higest date in the table. This is resulting in duplicate entries every other day.

This is the query so far. The problem is with the AS b part of the delete statement.

>begin tran
>
>insert into mobile_changes
>(Mobile, FID, RID, PIN, Commission, Barred, Barred_ACSE, LES)
>
>select Mobile, FID, RID, PIN, Commission, Barred, Barred_ACSE, LES
>from A1MobList union all
>select Mobile, FID, RID, PIN, Commission, Barred, Barred_ACSE, LES
>from A2MobList union all
>select Mobile, FID, RID, PIN, Commission, Barred, Barred_ACSE, LES
>from R1MobList union all
>select Mobile, FID, RID, PIN, Commission, Barred, Barred_ACSE, LES
>from R2MobList
>GO
>
>delete mobile_changes from mobile_changes as a,
>(select * from mobile_history where date_time = (select max(date_time)
>from mobile_history)) as b
>where
>a.mobile = b.mobile and
>a.fid = b.fid and
>a.rid = b.rid and
>a.pin = b.pin and
>a.commission = b.commission and
>a.barred = b.barred and
>a.barred_ACSE = b.barred_ACSE and
>a.LES = b.LES
>GO
>
>insert into mobile_history
>(Mobile, FID, RID, PIN, Commission, Barred, Barred_ACSE, LES)
>select
>Mobile, FID, RID, PIN, Commission, Barred, Barred_ACSE, LES
>from Mobile_Changes
>GO
>
>delete from mobile_changes
>GO
>
>commit tran
Reply With Quote
  #2 (permalink)  
Old 02-26-04, 07:32
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: how do I return the highest date per group

You need to get the max(date_time) per unit, and then use that to find the latest record for the unit:
PHP Code:
select *
from xxx
where 
(unitdate_timein
select unitmax(date_time)
  
from xxx
  group by unit
); 
Note that if (unit, date_time) are not unique in the table then you could get back more than 1 row per unit.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 02-26-04, 09:05
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
not all databases support that structure, tony

a correlated subquery is a good alternative, works even in Access:
PHP Code:
select *
  
from xxx as t1
 where date_time 
=
       ( 
select max(date_time)
           
from xxx
          where unit 
t1.unit 
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 02-26-04, 09:25
jdoyle jdoyle is offline
Registered User
 
Join Date: Oct 2003
Posts: 24
Re: how do I return the highest date per group

Thanks
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On