OH NO! More SQL woes
I think I'm probably overlooking something really simple... pleeze help!
I have a table, lets call it Sheet1, and it looks like this:
Code:
ID Date value quantity
1 5/30/2008 52 7
1 3/6/2010 36 5
1 8/7/2009 346 78
1 4/15/2001 46 26
1 4/12/2003 23 37
1 5/17/2009 6 4
2 2/9/1999 74 478
2 5/3/1997 46 234
2 4/4/2004 568 47
3 1/1/1988 267 56
3 2/5/1977 54 54
3 4/5/2003 37 5
3 3/3/2003 54 54
4 2/5/1995 45 67
4 4/30/2010 34 23
4 5/6/2007 34 23
4 4/18/1988 46 24
5 3/8/2008 23 54
5 9/9/1979 123 56
5 3/30/1998 135 39
5 4/16/2003 57 462
I am trying to write a query that gives me the FIRST value and LAST quantity sorted by date, for each individual ID.
So, the result set I want is:
Code:
ID firstOfValue lastOfQuantity
1 46 5
2 46 47
3 54 5
4 46 23
5 123 54
So, I want to GROUP BY ID and ORDER BY Date.... but I can't figure it out.
So far I have this SQL Query:
Code:
SELECT Sheet1.ID, First(Sheet1.value) AS FirstOfvalue, Last(Sheet1.quantity) AS LastOfquantity
FROM Sheet1
GROUP BY Sheet1.ID
ORDER BY Sheet1.Date;
But that gives the error "You tried to execute a blah blah that does not include Sheet1.Date as part of an aggregate function."
But, if I include Date in the GROUP BY then I don't get 1 value for each ID, I get every single entry because the dates don't match....
I've also tried:
Code:
SELECT Alias.ID, First(Alias.Date) AS FirstOfDate, First(Alias.value) AS FirstOfvalue, Last(Alias.quantity) AS LastOfquantity
FROM (SELECT * FROM Sheet1
ORDER BY Sheet1.ID, Sheet1.Date ASC
) AS Alias
GROUP BY Alias.ID;
But, that doesn't work either
Is this easier than I am making it?
Thanks for reading
Cheers!
