select customer_name,
kwh,
reading_date,
reading_time
from (
select customer_name,
kwh,
reading_time,
reading_date,
row_number() over (partition by customer_name order by reading_time) as rn
from readings
where reading_date = date '2012-11-17'
) t
where rn = 1
Som et alternativ:
select r1.customer_name,
r1.kwh,
r1.reading_date,
r1.reading_time
from readings r1
where reading_date = date '2012-11-17'
and reading_time = (select min(r2.reading_time)
from readings
where r2.customer_name = r1.customer_name
and r2.read_date = r1.reading_date);
Men jeg ville forvente, at den første var hurtigere.
Btw:hvorfor gemmer du dato og klokkeslæt i to separate kolonner? Er du klar over, at dette kunne håndteres bedre med et timestamp
kolonne?