Solved

SQL Group By Query Earliest Date

Posted on 2013-11-19
4
501 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
  • 2
  • 2
4 Comments
 
LVL 24

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 24

Accepted Solution

by:
chaau earned 300 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

912 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

23 Experts available now in Live!

Get 1:1 Help Now