Skip to content

Instantly share code, notes, and snippets.

@deinspanjer
Last active January 3, 2016 22:29
Show Gist options
  • Save deinspanjer/8528424 to your computer and use it in GitHub Desktop.
Save deinspanjer/8528424 to your computer and use it in GitHub Desktop.
Postgres query that implements the "last non-null value"
steelwheels=# select a1_h0_l0
steelwheels-# , a1_h0_l1
steelwheels-# , a1_h0_l2
steelwheels-# , a0_h0_l0
steelwheels-# , a0_h0_l1
steelwheels-# , m0
steelwheels-# , first_value(m0)
steelwheels-# over (
steelwheels(# partition by p0
steelwheels(# order by a1_h0_l0
steelwheels(# , a1_h0_l1
steelwheels(# , a1_h0_l2
steelwheels(# , a0_h0_l0
steelwheels(# , a0_h0_l1)
steelwheels-# as m1
steelwheels-# , p0
steelwheels-# from (
steelwheels(# select a1_h0_l0
steelwheels(# , a1_h0_l1
steelwheels(# , a1_h0_l2
steelwheels(# , a0_h0_l0
steelwheels(# , a0_h0_l1
steelwheels(# , m0
steelwheels(# , sum(case when m0 is null then 0 else 1 end)
steelwheels(# over ( order by a1_h0_l0 , a1_h0_l1 , a1_h0_l2 , a0_h0_l0 , a0_h0_l1 )
steelwheels(# as p0
steelwheels(# from (
steelwheels(# select d0.year_id as a1_h0_l0
steelwheels(# , d0.qtr_name as a1_h0_l1
steelwheels(# , d0.month_name as a1_h0_l2
steelwheels(# , d1.territory as a0_h0_l0
steelwheels(# , d1.country as a0_h0_l1
steelwheels(# , sum(case when d1.country is null then null else orderfact.quantityordered end) as m0
steelwheels(# from orderfact
steelwheels(# join (select time_id, year_id, qtr_id, qtr_name, month_id, month_name
steelwheels(# from dim_time where year_id = 2003
steelwheels(# ) d0 on orderfact.time_id = d0.time_id
steelwheels(# full outer join (
steelwheels(# select customernumber, territory, country
steelwheels(# from customer_w_ter where territory = 'APAC' and country = 'Australia'
steelwheels(# ) d1 on orderfact.customernumber = d1.customernumber
steelwheels(# group by a1_h0_l0
steelwheels(# , d0.qtr_id
steelwheels(# , a1_h0_l1
steelwheels(# , d0.month_id
steelwheels(# , a1_h0_l2
steelwheels(# , a0_h0_l0
steelwheels(# , a0_h0_l1
steelwheels(# order by d0.year_id
steelwheels(# , d0.qtr_id
steelwheels(# , d0.month_id
steelwheels(# , a0_h0_l0
steelwheels(# , a0_h0_l1
steelwheels(# ) as meas
steelwheels(# ) part;
a1_h0_l0 | a1_h0_l1 | a1_h0_l2 | a0_h0_l0 | a0_h0_l1 | m0 | m1 | p0
----------+----------+----------+----------+-----------+------+------+----
2003 | QTR1 | Feb | | | | | 0
2003 | QTR1 | Jan | | | | | 0
2003 | QTR1 | Mar | | | | | 0
2003 | QTR2 | Apr | APAC | Australia | 525 | 525 | 1
2003 | QTR2 | Apr | | | | 525 | 1
2003 | QTR2 | Jun | | | | 525 | 1
2003 | QTR2 | May | APAC | Australia | 66 | 66 | 2
2003 | QTR2 | May | | | | 66 | 2
2003 | QTR3 | Aug | | | | 66 | 2
2003 | QTR3 | Jul | APAC | Australia | 266 | 266 | 3
2003 | QTR3 | Jul | | | | 266 | 3
2003 | QTR3 | Sep | APAC | Australia | 546 | 546 | 4
2003 | QTR3 | Sep | | | | 546 | 4
2003 | QTR4 | Dec | | | | 546 | 4
2003 | QTR4 | Nov | APAC | Australia | 1111 | 1111 | 5
2003 | QTR4 | Nov | | | | 1111 | 5
2003 | QTR4 | Oct | | | | 1111 | 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment