在PostgreSQL中将时间序列转换为表(列:ISOWeek,行:KPI)

时间:2019-02-26 18:08:02

标签: sql postgresql

在Datastudio中以有意义的方式呈现数据时遇到麻烦。我意识到我需要预先对数据进行预处理才能显示属性。理想情况下,我想在每个ISO周(YYYYww)的单独行中显示每个KPI的数据。

如果有人对如何实现有任何想法,我很乐意听到任何建议。

谢谢!

SELECT x.iso_week,sum(x.sume) as total_sum,count(distinct x.did) as total_count,sum(x.sume)-sum(y.sume) as old_sum , sum(y.sume) as new_sum, count(distinct x.did)-count(distinct y.did) as old_count, count(distinct y.did) as new_count, x.firstname FROM 
(
SELECT o.firstname, (case when d."properties__deal_currency_code__value" = 'GBP' then d."properties__amount__value__do"*1.12 else d."properties__amount__value__do" end ) as sume, 
to_char(d.properties__closedate__value::date, 'IYYYIW') as iso_week, d."dealId" as did FROM "hmy"."deals" as d
JOIN "harmony"."owners" as o on o."ownerid" = cast(d.properties__hs_all_owner_ids__value as bigint)
WHERE d.properties__dealstage__value in ('108766', '40190', '100540') 
) x

left join

(
select d."dealId" as did, (case when d."properties__deal_currency_code__value" = 'GBP' then d."properties__amount__value__do"*1.12 else d."properties__amount__value__do" end ) as sume from "hmy"."deals" as d
where d.properties__misc___value = 'First order'
) y on x.did=y.did

group by x.iso_week,x.firstname
order by iso_week

这是当前视图 enter image description here

由于有多个用户,所以我将为每个用户单独查询(Antonin的数据)。

想要的结果 enter image description here

0 个答案:

没有答案