Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with dates as (
- select date_trunc('month', now() + (interval '1' month * generate_series(0,11))) as date
- ),
- all_vc as (
- select distinct cv.description as description, cv.id as id
- from static_referent.callvertical as cv
- join historian.callhistorian as ch on ch.call_vertical = cv.id
- join dynamic_referent.callbacktodo as cdt on cdt."call" = ch.id
- ),
- date_vc as (
- select dates.date as date, all_vc.description as description, all_vc.id as id
- from dates
- cross join all_vc
- )
- min_date as (
- select min(date_vc.date) as date, date_vc.id
- from date_vc
- group by date_vc.id
- ),
- callbacks as (
- select cdt."date" as date, hc.call_vertical as id
- from dynamic_referent.callbacktodo as cdt
- join historian.callhistorian as hc on hc.id = cdt."call"
- ),
- prev_date as (
- select min_date.date as date, min_date.id as id, count(date_trunc('month', cb.date)) as count
- from min_date
- join callbacks as cb on date_trunc('month', cb.date) < min_date.date and cb.id = min_date.id
- group by min_date.id, min_date.date
- ),
- c_count as (
- select count(date_trunc('month', cb.date)) as count, date_trunc('month', cb.date) as date, cb.id
- from callbacks as cb
- group by date_trunc('month', cb.date), cb.id
- )
- select to_char(date_vc.date, 'YY/MM'), date_vc.description as call_vertical, (coalesce(c_count.count, 0) + coalesce(prev_date.count, 0)) as count
- from date_vc
- left join c_count on c_count.date = date_vc.date and date_vc.id = c_count.id
- left join prev_date on prev_date.date = c_count.date and prev_date.id = c_count.id
- order by to_char(date_vc.date, 'MM') asc, call_vertical
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement