Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tough SQL statement 6

Status
Not open for further replies.

levinll

Programmer
Joined
Oct 29, 2001
Messages
65
Location
US
I'm using the below data and I'm trying to write some SQL code so for each uniqueHeader_ID in the table, I only get those records that have the latest date in the EditDate field. For example, for Header_ID #66963 I'd want to get back rows 6,7,8,9, and 10. For Header_ID #66964 I'd want to get back rows 11,12,13,14, and 15.

Can this be done ?

ID Header_ID EditDate
1 66963 10/28/2002 12:43:55 PM
2 66963 10/28/2002 12:43:55 PM
3 66963 10/28/2002 12:43:55 PM
4 66963 10/28/2002 12:43:55 PM
5 66963 10/28/2002 12:43:55 PM
6 66963 10/28/2002 12:47:04 PM
11 66964 10/28/2002 1:18:26 PM
13 66964 10/28/2002 1:18:26 PM
15 66964 10/28/2002 1:18:26 PM
7 66963 10/28/2002 12:47:04 PM
8 66963 10/28/2002 12:47:04 PM
9 66963 10/28/2002 12:47:04 PM
10 66963 10/28/2002 12:47:04 PM
12 66964 10/28/2002 1:18:26 PM
14 66964 10/28/2002 1:18:26 PM
 

i can't read queries unless i format them nicely :-)

Code:
select *  
  from GetEditedProcessedRecords AS XX 
 where XX.WeekEnding < @bdate 
   and XX.EditDate between @bdate 
                       and @edate 
   and (((
       (XX.EditDate)=
        ( select min(EditDate) 
            from GetEditedProcessedRecords 
           where Header_ID=XX.Header_ID )
       )))

the subquery gets the lowest EditDate overall for each Header_ID

however, the main query checks EditDate within a certain range

if the lowest EditDate overall is not in that range, you get no results

is that your problem?


rudy
 
I'm no SQL guru, so I can't say why the Min function doesn't work (I get no records back), and the Max function does.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top