I have a query that looks like this:
SELECT
"Present_Value" AS "Meter_Reading",
to_date(substr("Timestamp",2,12),'YYMMDDHH24MISS') AS "Time_Stamp",
Month(to_date(substr("Timestamp",2,12),'YYMMDDHH24 MISS')) AS "Time_Stamp_Month"
FROM theTable
WHERE ("Name" = 'WhatIAmLookingFor')
This works just fine and gets all the readings along with the time stamp info.
But I would like to have it only return the largest reading per month along with that reading's time stamp info. So I change it to:
SELECT
MAX("Present_Value") AS "Meter_Reading",
to_date(substr("Timestamp",2,12),'YYMMDDHH24MISS') AS "Time_Stamp",
Month(to_date(substr("Timestamp",2,12),'YYMMDDHH24 MISS')) AS "Time_Stamp_Month"
FROM theTable
WHERE ("Name" = 'WhatIAmLookingFor')
GROUP BY Month(to_date(substr("Timestamp",2,12),'YYMMDDHH24 MISS'))
and then it fails. The error is:
CDHFD0812E: Error executing SQL statement. An expression starting with "Timestamp" specified in a SELECT clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column function and no GROUP BY clause is specified.. SQLCODE=-119, SQLSTATE=42803, DRIVER=3.50.152
If I leave out the
to_date(substr("Timestamp",2,12),'YYMMDDHH24MISS') AS "Time_Stamp",
then it works (though it doesn't of course return the timestamp information).
The error message implies that I need to have the Time_Stamp part in the GROUP BY but I an new to writing SQL queries and I don't see why. Adding the timestamp to the GROUP BY gets the wrong results. I only need it grouped by month.
Can anyone help? Thanks for any tips!