?
Solved

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

Posted on 2010-11-11
14
Medium Priority
?
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +1
14 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 2000 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 93

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 57

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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
LVL 57

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 93

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
 

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

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

770 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