?
Solved

SQL Group By Query Earliest Date

Posted on 2013-11-19
4
Medium Priority
?
507 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

764 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