How to return multiple query results based on the date column in one of two tables?

Experts,

I have a situation in which I am querying two tables with the following:

$sql = "SELECT * FROM photos_clinicians c, photos p WHERE c.Department='$Department' AND c.Site='$Site' AND c.ResourceID=p.EmployeeID ORDER BY p.LastName";

The problem is that the table 'photos p' allows for multiple entries that are the same, with the exception of the 'p.date' column.

Example of 'photos' table:

FirstName | LastName | EmployeeID | Date

Steve | Smith | 1234567 | 01/01/09
Steve | Smith | 1234567 | 01/01/10
Bob | Johnson | 7985632 | 02/01/09
John | Thomas | 5986347 | 05/06/10
Bob | Johnson | 7985632 | 03/02/10

I would like to limit the output of my query based on 'p.Date' so that only the latest entry according to 'p.Date' is printed to the screen without losing the rest of the query data?

So if the query above were modified my result set would look like:

Steve | Smith | 1234567 | 01/01/10
John | Thomas | 5986347 | 05/06/10
Bob | Johnson | 7985632 | 03/02/10

When I use 'LIMIT 1' in the query, the query only shows one total result when there could be hundreds.  Can I use some form of 'Distinct' in my query to get the results I am looking for?

Any suggestions?
evibesmusicAsked:
Who is Participating?
 
cyberkiwiConnect With a Mentor Commented:
Assuming table photo has an ID column that is sorted the same way as date

$sql = "
SELECT * FROM photos_clinicians c, photos p
WHERE c.Department='$Department' AND c.Site='$Site' AND c.ResourceID=p.EmployeeID
AND p.ID in (
   SELECT MAX(p.ID) FROM photos_clinicians c, photos p
   WHERE c.Department='$Department' AND c.Site='$Site' AND c.ResourceID=p.EmployeeID
   GROUP BY c.ResourceID
)
ORDER BY p.LastName
";

// this is a literal multi-line php string
0
 
Patrick MatthewsCommented:
$sql = "SELECT p.FirstName, p.LastName, p.EmployeeID, MIN(p.Date) AS Date FROM photos_clinicians c, photos p WHERE c.Department='$Department' AND c.Site='$Site' AND c.ResourceID=p.EmployeeID GROUP BY p.FirstName, p.LastName, p.EmployeeID ORDER BY p.LastName";
0
 
HainKurtSr. System AnalystCommented:
i am thinking of such a query, not sure if we can make it run though:

select * from (
SELECT c.*, p.*, @rownum:=iif(@preEmpID=p.EmployeeID,1,0)*@rownum + 1 as rank, @preEmpID=p.EmployeeID
FROM photos_clinicians c, photos p, (SELECT @rownum := 0, @preEmpID=0) r
WHERE c.Department='$Department' AND c.Site='$Site' AND c.ResourceID=p.EmployeeID
ORDER BY p.EmployeeID, Date Desc
) x where rank = 1
ORDER BY LastName

idea is implementing rank function here, on each record rank is calculated. if empid does not chnage it increments 1 otherwise reset to 0. then another one selects records with rank=1

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
HainKurtSr. System AnalystCommented:
or just replace "photos p" in your original query with

(
select p1.* from Photos p1, (select EmployeeID, max(Date) as maxDate from photos) p2
where p1.EmployeeID=p2.EmployeeID and p1.date=p2.maxDate
) p

so your solution becomes
$sql = "SELECT * FROM photos_clinicians c, (select p1.* from Photos p1, (select EmployeeID, max(Date) as maxDate from photos) p2 where p1.EmployeeID=p2.EmployeeID and p1.date=p2.maxDate) p WHERE c.Department='$Department' AND c.Site='$Site' AND c.ResourceID=p.EmployeeID ORDER BY p.LastName";

Open in new window

0
 
Patrick MatthewsCommented:
My apologies, my suggestion should have been:
$sql = "SELECT p.FirstName, p.LastName, p.EmployeeID, MAX(p.Date) AS Date FROM photos_clinicians c, photos p WHERE c.Department='$Department' AND c.Site='$Site' AND c.ResourceID=p.EmployeeID GROUP BY p.FirstName, p.LastName, p.EmployeeID ORDER BY p.LastName";

Open in new window

0
 
cyberkiwiCommented:
Patrick,

I think the listed sample result is a sample only.

I took "without losing the rest of the query data" to mean other columns from both tables.. who knows!
0
 
evibesmusicAuthor Commented:
@cyberkiwi:

Thank you for your help.  I've used your query with one modification made to it.  See below:

I made the modification to your call to 'p.ID' as yes, there is a column 'ID' in both tables but, in some cases the ID of each row is not indicative of the latest entry.  Thus I changed 'p.ID' to 'p.Date' as the I really wanted to pull the information based on the latest date.  Sorry if I confused the issue in my original post.

Either way...CHEERS!  Thanks for your help.

$sql = "
SELECT * FROM photos_clinicians c, photos p
WHERE c.Department='$Department' AND c.Site='$Site' AND c.ResourceID=p.EmployeeID
AND p.Date in (
   SELECT MAX(p.Date) FROM photos_clinicians c, photos p
   WHERE c.Department='$Department' AND c.Site='$Site' AND c.ResourceID=p.EmployeeID
   GROUP BY c.ResourceID
)
ORDER BY p.LastName
";

Open in new window

0
 
evibesmusicAuthor Commented:
Cheers to all that attempted to help me with this issue.
0
 
cyberkiwiCommented:
It gives you the wrong result though (may still get duplicates), if it is not the unique ID.
Let me see (you should list all the columns you need, but c.*, p.* is equal to the list without the x subquery)

$sql = "
SELECT c.*, p.*
FROM (
   SELECT c.ResourceID, MAX(p.Date) pDate FROM photos_clinicians c, photos p
   WHERE c.Department='$Department' AND c.Site='$Site' AND c.ResourceID=p.EmployeeID
   GROUP BY c.ResourceID
) x
inner join photos_clinicians c on c.resourceid=x.resourceid
inner join photos p on c.ResourceID=p.EmployeeID and p.date = x.pdate
WHERE c.Department='$Department' AND c.Site='$Site'
ORDER BY p.LastName
";
0
 
evibesmusicAuthor Commented:
@cyberkiwi:

I have tested the query specifically on entries in the db that would give me duplicate results and they don't appear?  Would the use of 'MAX(p.Date)' only return results where the Maximum date has been established?
0
 
cyberkiwiCommented:
FirstName | LastName | EmployeeID | Date

Steve | Smith | 1234567 | 01/01/09
Steve | Smith | 1234567 | 01/01/10  ***
Bob | Johnson | 7985632 | 01/01/10  <<<<
John | Thomas | 5986347 | 05/06/10  ***
Bob | Johnson | 7985632 | 03/02/10  ***

The subquery returns a list of Max dates for any resource, so it returns the dates marked in ***
But see the one marked <<<<.
It will get returned as well because there is no correlation between max date and outer query.
0
 
evibesmusicAuthor Commented:
@cyberkiwi:

So with that said, you would like me to try the query posted here:  11/11/10 02:54 PM, ID: 34116556

And - if so, should I list all of the columns I would like data from rather than using the wildcard * ?
0
 
cyberkiwiCommented:
Yes the new query would work correctly (if there is no error to it!)
And yes, * is really good for testing and stuff, but do expand it when you can.
0
 
evibesmusicAuthor Commented:
@cyberkiwi:

Great!  Thank you very much.  I will use the second query and expand upon the wild card...I have already tested the query and it works without errors...had to correct some of the spelling for the columns but other than that we are good to go.

Thanks Cyberkiwi...I'd award more points if I could.  I'm full of odd questions so, check back if you want more points.

Cheers!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.