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 > Database Server Software > MySQL > Detecting last records entered in a table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-13-10, 09:44
DW_Scott DW_Scott is offline
Registered User
 
Join Date: Aug 2010
Posts: 4
Detecting last records entered in a table

Hello,

I need to select the last record that was entered for a each case_id in a table and then see if that each last record for each case_id was made in the last 7 days.

This returned query is to populate an alarm that the case has not had enough contact.

The following code almost works - however it selects the first record in the table for the case and not the last. ie if case_id 3 has four records: 2010-07-01, 2010-08-02, 2010-08-10, 2010-08-11 and todays date is 2010-08-13, it always seems to stop at 2010-07-01 and return that the case has not had enough contact.

I have tried a number of things but cant quite get it to work.

Does anyone have a suggestion.

SELECT case_notes.case_id As CaseID, case_management.contact_time As ContactTime, person.firstname, person.surname FROM case_notes,case_management, person WHERE case_notes.case_id = case_management.case_id AND ( date_sub(curdate(), interval 0 month) - case_notes.whenadded > 7 ) AND case_management.contact_time = 'Weekly' AND case_management.active = 'Active' AND case_management.person_id = person.person_id GROUP BY case_notes.case_id ORDER BY case_notes.case_id

thanks
Scott
Reply With Quote
  #2 (permalink)  
Old 08-13-10, 10:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by DW_Scott View Post
I need to select the last record that was entered for a each case_id
Code:
SELECT case_notes.case_id As CaseID
     , case_management.contact_time As ContactTime
     , person.firstname
     , person.surname 
  FROM ( SELECT case_id
              , MAX(whenadded) AS last_added
           FROM case_notes
         GROUP
             BY case_id ) AS m
INNER
  JOIN case_notes
    ON case_notes.case_id = m.case_id
   AND case_notes.whenadded = m.last_added
INNER
  JOIN case_management
    ON case_management.case_id = case_notes.case_id
   AND case_management.contact_time = 'Weekly' 
   AND case_management.active = 'Active' 
INNER
  JOIN person 
    ON person.person_id = case_management.person_id
 WHERE case_notes.whenadded >= CURRENT_DATE - INTERVAL 7 DAY
ORDER 
    BY case_notes.case_id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 08-14-10 at 00:09.
Reply With Quote
  #3 (permalink)  
Old 08-13-10, 19:25
DW_Scott DW_Scott is offline
Registered User
 
Join Date: Aug 2010
Posts: 4
Thanks for the reply.

I see what you are doing but am getting an error

Unknown column 'm.case_notes' in 'on clause'

The following works as I need - thanks- the results appear not to be available in the next join.

( SELECT case_id
, MAX(whenadded) AS last_added
FROM case_notes
GROUP
BY case_id ) As m

I have tried it in phpmyadmin and navicat

Any further ideas
Reply With Quote
  #4 (permalink)  
Old 08-14-10, 00:09
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by DW_Scott View Post
Unknown column 'm.case_notes' in 'on clause'
aaargh... typo

i fixed it, see above
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-14-10, 01:14
DW_Scott DW_Scott is offline
Registered User
 
Join Date: Aug 2010
Posts: 4
Thanks that works great - except for one thing.

If there are two entries for the case_note on the last day entered, the person is listed twice in the result.

Is there a way to limit the result to only one per case_id.

Thanks again
Reply With Quote
  #6 (permalink)  
Old 08-14-10, 05:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by DW_Scott View Post
Is there a way to limit the result to only one per case_id.
make sure the whenadded column is DATETIME, not DATE

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 08-14-10, 09:08
DW_Scott DW_Scott is offline
Registered User
 
Join Date: Aug 2010
Posts: 4
Thanks again - I really appreciate your help.
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