Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Group By Grab first record, not last?

Status
Not open for further replies.

tqeonline

MIS
Oct 5, 2009
304
US
I have a query that creates the hours between dates and actual and projected points.

Data that is returned looks like

2010-11-09 20:00:00.000 97.799 96
2010-11-09 21:00:00.000 98.600 96
2010-11-09 22:00:00.000 99.301 96
2010-11-09 23:00:00.000 100.000 96
2010-11-11 08:00:00.000 100.000 100

my query does a group by like this:
Code:
GROUP BY POINT,ACTUAL_PCT
ORDER BY MIN(CYCLEDATE)

As you can see from the data at 11-10-2010 0:00:00.000 they both hit 100. However it grabbed 11-11.

Is there a way in a group by to grab the very first item instead of the very last item in a group by?

In this example I'd like it to have selected 11-10 instead of 11-11

- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 
I resolved my issue by making it group by the date as well. Which in my case works. But for future states is there a way to do what i'm asking above?

- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 
GROUP BY cannot "grab the very first item instead of the very last item"

GROUP BY condenses/combines/aggregates multiple rows into one

if you want "first item" or "last item" this can only be done by joining to a derived table, a subquery with a GROUP BY which calculates the MIN or MAX of some column for the item

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top