proposal: window function - change_number
| От | Pavel Stehule |
|---|---|
| Тема | proposal: window function - change_number |
| Дата | |
| Msg-id | CAFj8pRA_AsBQpeuwXjcd9dKQqRNhhnbY5knmpRJPEUimPewovw@mail.gmail.com обсуждение исходный текст |
| Ответы |
Re: proposal: window function - change_number
Re: proposal: window function - change_number |
| Список | pgsql-hackers |
Hi
I tried to solve following task:
I have a table
start, reason, km
=============
2014-01-01 08:00:00, private, 10
2014-01-01 09:00:00, commerc, 20
2014-01-01 10:00:00, commerc, 20
2014-01-01 11:00:00, private, 8
and I would reduce these rows to
2014-01-01 08:00:00, private, 10
2014-01-01 09:00:00, commerc, 20 + 20 = 40
2014-01-01 11:00:00, private, 8
I tried to solve following task:
I have a table
start, reason, km
=============
2014-01-01 08:00:00, private, 10
2014-01-01 09:00:00, commerc, 20
2014-01-01 10:00:00, commerc, 20
2014-01-01 11:00:00, private, 8
and I would reduce these rows to
2014-01-01 08:00:00, private, 10
2014-01-01 09:00:00, commerc, 20 + 20 = 40
2014-01-01 11:00:00, private, 8
It is relative hard to it now with SQL only. But we can simplify this task with window function that returns number of change in some column. Then this task can be solved by
select min(start), min(reason), sum(km)
from (select start, reason, km, change_number(reason) over (order by start))
group by change_number;
Do you think, so it has sense?
Regards
Pavel
В списке pgsql-hackers по дате отправления: