gpt4 book ai didi

sql - PostgreSQL 获取数据作为数据透视表

转载 作者:行者123 更新时间:2023-11-29 11:51:45 26 4
gpt4 key购买 nike

我有一个返回以下数据的查询。

enter image description here

我想将此结果更改为如下所示的数据透视表

Customer  Unit Oct2015  Nov2015  Dec2015  Jan2016
customer4 A4 3423
TEST A3 4762.6
customer2 A2 1000

我想使用日期字段过滤数据。因此,如果我选择 2015 年 10 月至 2016 年 1 月,上面的结果将会出现。

我尝试了以下查询

CREATE EXTENSION tablefunc;


SELECT * FROM crosstab(
$$ select customer_name,date_part('month', breakup_date) as month,amount from (
select se.enquiry_id,s.ref_no,customer_name,ppd.property,breakup_date,land+construction+service_tax+vat as amount
from work_sheet_detail wsd
left join sales_estimate s on wsd.estimate_id=s.ref_no
left join sales_enquiry se on s.enquiry_id=se.enquiry_id
left join project_property_details ppd on s.property_id=ppd.property_id
where s.status_id=1
) as data order by 1 $$,
$$ SELECT m FROM generate_series(1,12) m $$
) AS (
customer_name character varying, "Jan" int, "Feb" int, "Mar" int, "Apr" int, "May" int, "Jun" int, "Jul" int, "Aug" int, "Sep" int, "Oct" int, "Nov" int, "Dec" int
);

但是不会给出一个客户一个月内的金额总和。数据透视表也是月份明智的,因为我需要年和月列。

enter image description here

我已经弥补了以下查询。但是在这里我得到了月份和年份的总和,但无法为其创建系列。

CREATE EXTENSION IF NOT EXISTS tablefunc ;


SELECT * FROM crosstab(
$$ select customer_name,breakup_date as month,amount from (
with sum_amount as (
select se.enquiry_id,to_char(breakup_date,'Mon YYYY') as breakup_date,s.ref_no,sum(land+construction+service_tax+vat) as amount
from work_sheet_detail wsd
left join sales_estimate s on wsd.estimate_id=s.ref_no
left join sales_enquiry se on s.enquiry_id=se.enquiry_id
where s.status_id=1
group by 1,2,3
)
select se.enquiry_id,customer_name,ppd.property,breakup_date,amount
from sum_amount sm
left join sales_estimate s on sm.ref_no=s.ref_no
left join sales_enquiry se on s.enquiry_id=se.enquiry_id
left join project_property_details ppd on s.property_id=ppd.property_id
where s.status_id=1

) as data order by 1 $$,
$$ SELECT m FROM generate_series(1,12) m $$
) AS (
customer_name character varying, "Jan" int, "Feb" int, "Mar" int, "Apr" int, "May" int, "Jun" int, "Jul" int, "Aug" int, "Sep" int, "Oct" int, "Nov" int, "Dec" int
);

最佳答案

如果您可以灵活地在数据透视表中只显示一年,那么此选择可能会有所帮助:

select customer, unit
, sum(case date_part('MONTH', b_date) when 1 then amount else 0 end) as Jan
, sum(case date_part('MONTH', b_date) when 2 then amount else 0 end) as Feb
, sum(case date_part('MONTH', b_date) when 3 then amount else 0 end) as Mar
, sum(case date_part('MONTH', b_date) when 4 then amount else 0 end) as Apr
, sum(case date_part('MONTH', b_date) when 5 then amount else 0 end) as May
, sum(case date_part('MONTH', b_date) when 6 then amount else 0 end) as June
, sum(case date_part('MONTH', b_date) when 7 then amount else 0 end) as July
, sum(case date_part('MONTH', b_date) when 8 then amount else 0 end) as Aug
, sum(case date_part('MONTH', b_date) when 9 then amount else 0 end) as Sept
, sum(case date_part('MONTH', b_date) when 10 then amount else 0 end) as Oct
, sum(case date_part('MONTH', b_date) when 11 then amount else 0 end) as Nov
, sum(case date_part('MONTH', b_date) when 12 then amount else 0 end) as Dec
from customers
where date_part('YEAR', b_date) = 2015
group by name, property;

关于sql - PostgreSQL 获取数据作为数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33892513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com