I am trying to change the count DISTINCT portion of the query below, because the current query returns WINDOW definition is not supported. What is most seamless way to do this calc using PostgreSQL? Thank you.
SELECT transaction_date, brand, Description, Amount, newval
into temp.table
FROM (SELECT transaction_date, brand, Description, Amount,
(Amount / count(DISTINCT unique_mem_id) over (partition by to_char(transaction_date, 'YYYY-MM'),brand
)) as newval
FROM source.table
)
WHERE DESCRIPTION iLIKE '%Criteria%';
Due to the use-case it seems better to split the code.
Create a table month_brand based on the following query:
select to_char(transaction_date, 'YYYY-MM') as yyyymm
,brand
,count (distinct unique_mem_id) as count_distinct_unique_mem_id
from source.table
group by yyyymm
,brand
;
Join month_brand with your source table:
select t.transaction_date, t.brand, t.Description, t.Amount, t.Amount / m.count_distinct_unique_mem_id as newval
from source.table as t
join month_brand as m
on m.yyyymm = to_char(t.transaction_date, 'YYYY-MM')
where t.description ilike '%Criteria%'
;
Instead of count(distinct ...)
, 2 phase solution:
select *
into temp.table
from (SELECT transaction_date, brand, Description, Amount,
(Amount / count(case rn when 1 then unique_mem_id end) over (partition by to_char(transaction_date, 'YYYY-MM'),brand)) as newval
FROM (SELECT transaction_date, brand, Description, Amount,unique_mem_id
row_numner () over (partition by to_char(transaction_date, 'YYYY-MM'),brand,unique_mem_id) as rn
FROM source.table
)
)
WHERE DESCRIPTION iLIKE '%Criteria%'
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With