PostgreSQL将数据作为数据透视表获取

时间:2015-11-24 11:31:01

标签: sql postgresql pivot postgresql-9.3

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

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
);

1 个答案:

答案 0 :(得分:0)

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

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;