Solved

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

Posted on 2010-11-11
14
336 Views
Last Modified: 2013-12-12
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?
0
Comment
Question by:evibesmusic
  • 5
  • 5
  • 2
  • +1
14 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34115909
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
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34115922
$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
 
LVL 51

Expert Comment

by:HainKurt
ID: 34116056
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
 
LVL 51

Expert Comment

by:HainKurt
ID: 34116114
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
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34116123
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
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34116436
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
 

Author Comment

by:evibesmusic
ID: 34116510
@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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Closing Comment

by:evibesmusic
ID: 34116517
Cheers to all that attempted to help me with this issue.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34116556
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
 

Author Comment

by:evibesmusic
ID: 34116660
@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
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34116685
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
 

Author Comment

by:evibesmusic
ID: 34116760
@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
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34116817
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
 

Author Comment

by:evibesmusic
ID: 34116832
@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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now