Solved

SQL Group By Query Earliest Date

Posted on 2013-11-19
4
499 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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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 video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

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

21 Experts available now in Live!

Get 1:1 Help Now