Reputation: 137
first off: thanks for looking; your time is appreciated!
to the point:
SCHEMA: for myTable
nonUniqueID YEAR MONTH DAY HOUR numericValue
1 2012 01 01 01 99.9
1 2012 01 01 02 65.2
1 2012 01 01 03 -88
7 2012 02 08 21 9.08
1 2012 01 01 09 99.913
1 2013 01 01 01 99.999999
Basically, it's two values nonUniqueID
and numericValue
with a granular date.
Now, i need to get the single greatest date in this entire database. With the data above, the expected result would be:
nonUniqueID YEAR MONTH DAY HOUR numericValue
1 2013 01 01 01 99.999999
Because this is SQLite, i know that i will end up having to use MAX() several times. The order will have to be MAX(YEAR) then MAX(MONTH) then MAX(DAY) then MAX(HOUR).
Basically, the query would work like this (i just don't know enough about SQL syntax to create the query):
Here is a SQL that i adapted from another StackExchange question that does not work
Select * From (
Select max(YEAR) as Y FROM myTable
union
Select max(MONTH) as M FROM myTable
union
Select max(DAY) as D FROM myTable
union
Select max(HOUR) as H FROM myTable
) myTable;
which returns
Y
-----
21
08
02
2013
Compare this to expected out:
nonUniqueID YEAR MONTH DAY HOUR numericValue
1 2013 01 01 01 99.999999
it returned 4 records instead of the one record with the 4 values.
Can somebody please help me with my query? THANK YOU!
Upvotes: 1
Views: 1629
Reputation: 5680
Try this
SELECT nonUniqueID , YEAR , MONTH , DAY , HOUR , numericValue FROM myTable as a
INNER JOIN (Select max(MONTH) FROM myTable) as b
ON a.YEAR = B.YEAR
INNER JOIN (Select max(DAY) FROM myTable) as c
ON b.YEAR = c.YEAR
INNER JOIN (Select max(HOUR) FROM myTable) as d
ON c.YEAR = d.YEAR
WHERE a.YEAR = (Select max(YEAR) FROM myTable)
It returns 4 values because you use UNION
it means that the result are join in one column
EDIT
Ive just updated my answer see if that works Im not quite sure to the performance of this query
Upvotes: 3