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

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

最佳答案

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

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/

相关文章:

sql - 如何识别重叠的数字范围?

mysql - SQL 查询有问题

sql - 类似于记事本教程 :CursorIndexOutOfBoundsException: Index 0 requested, 的应用程序上的 Android SQL 问题,大小为 0

sql - Ruby sql 修剪模型中的整个列

sql - 数据透视表错误 56901 非常量表达式

postgresql - 如何将所有用户对象分配给 db_owner 角色

javascript - 在 Windows 上使用 gpg 连接到 postgresql 数据库时出错

postgresql - 如何查找哪些 postgresql 文本字段包含特定字符串的大多数出现?

php - MySQL 将数据透视为年月

sql - 平均每小时平均转置SQLite行和列