postgresql高级应用之行转列&汇总求和

postgresql高级应用之行转列&汇总求和

轉載請注名出處 https://www.cnblogs.com/funnyzpc/p/14732165.html

前言

节前公司业务方需要做一個統計報表,这个报表用于统计当月估计几个明星品的销售情况,而我们的数据是按行存储的就是 日期|产品|渠道|销售额这样,说是也奇了怪了,我们买的报(guan)表(yuan)系(la)统(ji) 竟然不能容易地实现。。。,于是我看了看,然后想了想,发现是可以通过sql算出这样一个报表(多亏了postgresql的高阶函数😂),然后直接将数据输出到报表系统 完事兒~ ,以下 我將sql關鍵部分描述下,至於對前端展示有興趣的同學可留言,可考慮作一節講講哈😄~

报表

首先,業務需要的報表長這樣子的,看起來似乎還OK哈~

postgresql高级应用之行转列&汇总求和
接下來我先給出我的測試脚本(均測試&無bug)~

表结构

drop table if EXISTS  report1 ;
CREATE TABLE "report1" (
  "id" numeric(22) NOT NULL,
  "date" date NOT NULL,
  "product" varchar(100),
  "channel" varchar(100),
  "amount" numeric(20,4)
);

表注释

字段 描述 id 主键 date 日期 product 产品 channel 渠道 amount 销售额

表数据

INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051726328010100000', '2021-05-04', '产品1', '京东', '8899.0000');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051726328010100001', '2021-05-04', '产品2', '京东', '99.0000');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051727068010100010', '2021-05-04', '产品1', '天猫', '230.0000');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051727068010100011', '2021-05-04', '产品2', '天猫', '9.9000');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051727068010100011', '2021-05-04', '产品3', '线下门店', '10.1000');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051727068010100000', '2021-05-04', '产品1', '其它', '10');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051727068010100099', '2021-05-04', '产品2', '其它', '20000');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051727068010100033', '2021-05-01', '产品1', '其它', '20000');
INSERT INTO "report1"("id", "date", "product", "channel", "amount") VALUES ('2105051727068010100044', '2021-05-01', '产品3', '线下门店', '12345');

postgresql高级应用之行转列&汇总求和

思考

如果你看到這裏請稍稍思考下,一開篇我説過我們的數據是按 日期|产品|渠道|销售额 這樣按行存儲的,以上截圖大家一看就懂,然後再看看開篇的報表截圖,我想大家可以同我一樣可以分析出以下幾點:

  • 報表縱向看大致分三部分
  • 一部分是前一日產品銷售明細
  • 然後一部分是前一日產品渠道產品合計
  • 最後一部分是按渠道做的月統計
  • 報表橫向看大致分兩部分
  • 上半部分是渠道明細及合計(日和月)
  • 最後一部分則是所有渠道的產品合計、日合計、月合計

好了,問題來了,如何做呢,我是這麽想的:首先要很清楚的是你的sql大致分兩大部分(兩個子查詢)

  • 一部分是前一日的數據
    [TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:67836ab6-4665-42f8-ab7e-9178ead6468d
    [En]

    [TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:b3f3d24b-e20a-40a0-9fbc-cc4aef0cd622

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:75cdf937-d76a-4c95-807e-ec7efd285783

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:ecfa22ef-213e-4b19-82ce-bc8004f95e15

第一部分數據(前一日的數據)

  • 我想我們立馬能做的第一部分sql恐怕就是行專列吧(似乎這是最容易實現的😄)
select
  channel,
  sum(case product when '产品1' then amount end) as c1,
  sum(case product when '产品2' then amount end) as c2,
  sum(case product when '产品3' then amount end) as c3
from report1
group by channel ;

postgresql高级应用之行转列&汇总求和

sql似乎沒什麽問題,但是我們少了一列,對那就是 按渠道日合計,當然如果您對postgresql窗口函數熟悉的話,這裏實現的方式估計你已經猜到了(窗口 over函數),上sql…

select
  channel,
  day_sum,
  sum(case product when '产品1' then amount end) as c1,
  sum(case product when '产品2' then amount end) as c2,
  sum(case product when '产品3' then amount end) as c3
from
  ( select *,sum(amount) over (partition by channel) as day_sum from report1  where date=to_date('2021-05-04','yyyy-MM-dd') ) as t1
group by t1.channel ,t1.day_sum;

postgresql高级应用之行转列&汇总求和

哈哈,上圖的 day_sum估計大家很熟悉了吧,哈哈哈~
看來已經成功地完成了 日數據部分,這裏可能的難點可能就兩點

  • 一是使用聚合函數(sum)+分組(group by)做行專列(當然 postgresql也有其他很好用的行專列擴展,這裏就不介紹啦~)
  • 另一個是使用窗口函數(over)對明細提前做 按渠道的窗口匯總,這樣渠道日合計(行)的數據就有啦~

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:5390d8ed-5b3d-4aca-8e9d-b4e18aa5b867

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:d6323721-0438-46a4-bec7-d9e2333c8b53

第二部分數據(月份匯總數據)

月份匯總的數據看似簡單的可怕,如果您熟練掌握postgresql中的日期處理的話估計分分鐘就能搞定,這裏就不耍大刀了,直接放出sql,哈哈哈😄

 select
  channel,sum(amount) as month_sum from report1
where
  date>=date(date_trunc('month',to_date('2021-05-04','yyyy-MM-dd'))) and date < date(date_trunc('month',to_date('2021-05-04','yyyy-MM-dd')) + '1 month')
group by
  channel

postgresql高级应用之行转列&汇总求和

報表數據最終求解

現在,我們將求解的兩部分數據按渠道 channel字段做 inner join合并以上兩部分數據,合并后的數據大致是這樣子的

postgresql高级应用之行转列&汇总求和

這個是sql

 select
    ttt.channel,
    sum(ttt.day_sum) as day_sum,
    sum(ttt.month_sum) as month_sum,
    sum(ttt.c1) as c1,
    sum(ttt.c2) as c2,
    sum(ttt.c3) as c3
from (
        select tt1.*,tt2.month_sum from
        (
        select
        channel,
      day_sum,
        sum(case product when '&#x4EA7;&#x54C1;1' then amount end) as c1,
        sum(case product when '&#x4EA7;&#x54C1;2' then amount end) as c2,
        sum(case product when '&#x4EA7;&#x54C1;3' then amount end) as c3
        from
        ( select *,sum(amount) over (partition by channel) as day_sum from report1  where date=to_date('2021-05-04','yyyy-MM-dd') ) as t1
        group by t1.channel ,t1.day_sum
        ) as tt1 left join
        (
          select channel,sum(amount) as month_sum from report1 where date>=date(date_trunc('month',to_date('2021-05-04','yyyy-MM-dd'))) and date < date(date_trunc('month',to_date('2021-05-04','yyyy-MM-dd')) + '1 month') group by  channel
        ) as tt2 on tt1.channel = tt2.channel
    ) ttt
GROUP BY ttt.channel
order by channel asc

看,匯總的數據已經有了,已經可以算作是最終結果了(如果你需要報表系統來計算匯總行數據的話),當然 ,我們的報表系統過於繁瑣(不是不能做,而是太麻煩),需要你將做好的菜喂給它吃,這時,該怎麽辦呢。。。,哈哈哈 我們似乎忘記了很久不用的 rollup函數(一開始我也沒發現有這麽個函數哈哈),試試看吧

 select
    ttt.channel,
    sum(ttt.day_sum) as day_sum,
    sum(ttt.month_sum) as month_sum,
    sum(ttt.c1) as c1,
    sum(ttt.c2) as c2,
    sum(ttt.c3) as c3
    from (
        select tt1.*,tt2.month_sum from
        (
        select
        channel,
      day_sum,
        sum(case product when '&#x4EA7;&#x54C1;1' then amount end) as c1,
        sum(case product when '&#x4EA7;&#x54C1;2' then amount end) as c2,
        sum(case product when '&#x4EA7;&#x54C1;3' then amount end) as c3
        from
        ( select *,sum(amount) over (partition by channel) as day_sum from report1  where date=to_date('2021-05-04','yyyy-MM-dd') ) as t1
        group by t1.channel ,t1.day_sum
        ) as tt1 left join
        (
          select channel,sum(amount) as month_sum from report1 where date>=date(date_trunc('month',to_date('2021-05-04','yyyy-MM-dd'))) and date < date(date_trunc('month',to_date('2021-05-04','yyyy-MM-dd')) + '1 month') group by  channel
        ) as tt2 on tt1.channel = tt2.channel
    ) ttt
    group by rollup(ttt.channel)
    order by channel asc

postgresql高级应用之行转列&汇总求和

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:e710e7f4-c64c-47ac-aa17-91b89413575e

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:ba041b5e-8132-41fe-9c89-370e88266220

總結

如果您肯下功夫學, postgresql世界有很多精彩的東西,當然也有一些東西對比mysql顯得繁瑣些,不過本著學習的心態,我們縂能剋服這些,同時我們還是能做出 &#x8D85;&#x51FA;&#x6211;&#x5011;&#x81EA;&#x8EAB;&#x80FD;&#x529B;&#x7BC4;&#x7587;的東西的,哈哈,各位加油哦~

下章,我將講一講如何實現通過sql實現前端合并單元格的效果,是不是很神奇(我保證你全網搜不到), 希望不翻車,哈哈哈~

Original: https://www.cnblogs.com/funnyzpc/p/14732165.html
Author: funnyZpC
Title: postgresql高级应用之行转列&汇总求和

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/562398/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球