Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Group By Query Earliest Date

Posted on 2013-11-19
4
Medium Priority
?
509 Views
Last Modified: 2013-11-19
Hey Everyone,

I have been spinning my wheels on a SQL Query I have been working on. The below query returns several fields with a Group By. The "When_Finished" column has multiple date values in a "dd/mm/yyy hh:mm:ss" format. I need the query to only return the lowest date and time value. I have tried MIN() to accomplish this, but it continues to return all dates

Example:

Details | Member_Group | Session_Name | Percentage_Score | When Finished

11111  | Group 1            | Test 1             | 90                        | 01/01/13 5:15PM
11111  | Group 1            | Test 1             | 0                          | 01/01/13 4:15PM
11111  | Group 1            | Test 2             | 100                      | 01/02/13 1:00PM
11111  | Group 1            | Test 3             | 85                        | 01/04/13 12:15PM

What I need returned is:

11111  | Group 1            | Test 1             | 0                          | 01/01/13 4:15PM
11111  | Group 1            | Test 2             | 100                      | 01/01/13 1:00PM
11111  | Group 1            | Test 3             | 85                        | 01/04/13 12:15PM

SELECT     dbo.G_Participant.Details, A_Result.Member_Group, dbo.G_Schedule.Session_Name AS Expr1, A_Result.Percentage_Score, A_Result.When_Finished
FROM         dbo.G_Participant INNER JOIN
                      dbo.A_Result AS A_Result ON dbo.G_Participant.Details = A_Result.Participant_Details INNER JOIN
                      dbo.G_Schedule ON dbo.G_Participant.Participant_ID = dbo.G_Schedule.Participant_ID AND A_Result.Session_MID = dbo.G_Schedule.Session_MID AND 
                      A_Result.Session_LID = dbo.G_Schedule.Session_LID INNER JOIN
                      dbo.G_Group ON dbo.G_Schedule.Group_ID = dbo.G_Group.Group_ID
WHERE     (A_Result.Status = 2)
GROUP BY dbo.G_Participant.Details, A_Result.Member_Group, A_Result.Percentage_Score, dbo.G_Schedule.Session_Name, A_Result.When_Finished
HAVING      (A_Result.Member_Group LIKE 'Claims Ownership%')
ORDER BY dbo.G_Participant.Details

Open in new window

0
Comment
Question by:Kds4evr
[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
  • 2
  • 2
4 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 39661382
You need to remove A_Result.When_Finished from the group by, like this:
SELECT     dbo.G_Participant.Details, A_Result.Member_Group, dbo.G_Schedule.Session_Name AS Expr1, A_Result.Percentage_Score, min(A_Result.When_Finished)
FROM         dbo.G_Participant INNER JOIN
                      dbo.A_Result AS A_Result ON dbo.G_Participant.Details = A_Result.Participant_Details INNER JOIN
                      dbo.G_Schedule ON dbo.G_Participant.Participant_ID = dbo.G_Schedule.Participant_ID AND A_Result.Session_MID = dbo.G_Schedule.Session_MID AND 
                      A_Result.Session_LID = dbo.G_Schedule.Session_LID INNER JOIN
                      dbo.G_Group ON dbo.G_Schedule.Group_ID = dbo.G_Group.Group_ID
WHERE     (A_Result.Status = 2)
GROUP BY dbo.G_Participant.Details, A_Result.Member_Group, A_Result.Percentage_Score, dbo.G_Schedule.Session_Name
HAVING      (A_Result.Member_Group LIKE 'Claims Ownership%')
ORDER BY dbo.G_Participant.Details

Open in new window

0
 

Author Comment

by:Kds4evr
ID: 39661392
Thanks chaau,

I gave it a whirl and still the same thing. Displays both the low and the high date on the return. Is a (SELECT TOP 1 .....) AS When_Finished_Top perhaps the approach on this? I had tried a few vairations, but I cannot get the where clause right.
0
 
LVL 25

Accepted Solution

by:
chaau earned 1200 total points
ID: 39661401
Sorry. I can see now. It looks like you also want to select the minimum percentage score (is it so?). In this case, modify the query like this:
SELECT     dbo.G_Participant.Details, A_Result.Member_Group, dbo.G_Schedule.Session_Name AS Expr1, min(A_Result.Percentage_Score), min(A_Result.When_Finished)
FROM         dbo.G_Participant INNER JOIN
                      dbo.A_Result AS A_Result ON dbo.G_Participant.Details = A_Result.Participant_Details INNER JOIN
                      dbo.G_Schedule ON dbo.G_Participant.Participant_ID = dbo.G_Schedule.Participant_ID AND A_Result.Session_MID = dbo.G_Schedule.Session_MID AND 
                      A_Result.Session_LID = dbo.G_Schedule.Session_LID INNER JOIN
                      dbo.G_Group ON dbo.G_Schedule.Group_ID = dbo.G_Group.Group_ID
WHERE     (A_Result.Status = 2)
GROUP BY dbo.G_Participant.Details, A_Result.Member_Group, dbo.G_Schedule.Session_Name
HAVING      (A_Result.Member_Group LIKE 'Claims Ownership%')
ORDER BY dbo.G_Participant.Details

Open in new window

0
 

Author Closing Comment

by:Kds4evr
ID: 39661409
That did it! Perfect thank you.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

610 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