postgresql使用filter进行多维度聚合的解决方法

你有没有碰到过有这样一种场景,就是我们需要看一下某个时间段内各种维度的汇总,比如这样:最近三年我们卖了多少货?有多少订单?平均交易价格多少?每个店铺卖了多少?交易成功的订单有多少?交易失败的订单有多少? 等等...,假使这些数据的明细都在一个表内,该这么做呢? 有没有简单方式?还有如何减少全表扫描以更改的拿到数据?

如果只是简单的利用聚合拿到数据可能您需要写很多sql,具体表现为每一个问题写一段sql 相互之间join起来,这样也许是个好主意,不过对于未充分优化的数据库系统,针对每一块的问题求解可能就是一个巨大的表扫描,当然还有一个问题就是重复的where条件,所以能不能把相同的where条件抽取出来以简化sql呢?让我们思考一下,也许有这样的解决办法~ (结论是有,当然有,哈哈哈~)

首先我提供下基本的表结构及测试数据

基本表结构

CREATE TABLE "order_info" (
  "id" numeric(22) primary key ,
  "oid" varchar(100) COLLATE "pg_catalog"."default",  -- 订单号
  "shop" varchar(100) COLLATE "pg_catalog"."default", -- 店铺
  "date" date NOT NULL, --订单日期
  "status" varchar(100) COLLATE "pg_catalog"."default", -- 订单状态
  "payment" numeric(18,2), -- 交易支付金额
  "product" varchar(100) COLLATE "pg_catalog"."default" -- 产品名称
  );

初始化表数据

INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217794', '16135476150276171', '店铺2', '2019-07-01', '交易失败', '139.00', '某某单品02');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217761', '16132502190562224', '店铺2', '2020-05-01', '交易成功', '9.90', '某某礼盒');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217795', '16122384743927326', '店铺3', '2019-06-01', '交易失败', '357.00', '某某套装');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217796', '16138945194036971', '店铺2', '2019-05-01', '交易中', '59.90', '某某单品');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217797', '16131909251901209', '店铺1', '2019-04-01', '交易失败', '359.00', '某某赠品');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217798', '16135391935074761', '店铺2', '2019-03-01', '交易失败', '139.00', '某某单品01');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217762', '16132472268456370', '店铺3', '2020-04-01', '交易成功', '79.00', '某某单品02');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217763', '16122960304700879', '店铺2', '2020-03-01', '交易成功', '357.00', '某某单品03');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217764', '16139491271154103', '店铺1', '2020-02-01', '交易成功', '139.00', '某某礼盒');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217765', '16122930818314343', '店铺2', '2020-01-01', '交易成功', '79.00', '某某套装');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217766', '12581133644786193', '店铺3', '2019-12-01', '交易成功', '79.00', '某某单品06');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217767', '16122904539659361', '店铺2', '2019-11-01', '交易成功', '359.00', '某某单品07');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217752', '16136227870425525', '店铺1', '2021-02-01', '交易成功', '4.90', '某某单品08');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217753', '16139781339192958', '店铺2', '2021-01-01', '交易失败', '89.00', '某某礼盒');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217754', '16136217317281545', '店铺3', '2020-12-01', '交易中', '6.90', '某某套装');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217756', '16123091065663616', '店铺1', '2020-10-01', '交易失败', '95.00', '某某单品01');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217757', '16123013684517817', '店铺2', '2020-09-01', '交易中', '79.00', '某某单品02');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217758', '16139678011781848', '店铺3', '2020-08-01', '交易中', '59.90', '某某单品03');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217759', '16139576187535157', '店铺2', '2020-07-01', '交易成功', '9.90', '某某单品04');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217791', '16132066938478413', '店铺4', '2019-10-01', '交易成功', '359.00', '某某单品05');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217792', '12589185047405699', '店铺5', '2019-09-01', '交易成功', '6.90', '某某单品06');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217760', '16139601047542860', '店铺1', '2020-06-01', '交易成功', '359.00', '某某单品07');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217837', '16138184483906283', '店铺4', '2021-03-04', '交易成功', '359.00', '某某单品02');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217838', '16134581997874325', '店铺5', '2021-03-04', '交易成功', '299.00', '某某单品03');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217839', '16131099658443817', '店铺3', '2021-03-04', '交易成功', '9.90', '某某单品04');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217840', '16131081649792689', '店铺2', '2021-03-04', '交易成功', '15.89', '某某单品05');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217841', '16131087729266410', '店铺1', '2021-03-04', '交易成功', '49.00', '某某礼盒');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217842', '16138126191679446', '店铺2', '2021-03-04', '交易成功', '6.90', '某某套装');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217843', '16138166422967430', '店铺3', '2021-03-04', '交易成功', '579.00', '某某单品');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217844', '16121412752067761', '店铺2', '2021-03-04', '交易成功', '359.00', '某某赠品');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217845', '12580980977280299', '店铺3', '2021-03-04', '交易成功', '359.00', '某某单品01');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217799', '16135358470437562', '店铺2', '2019-02-01', '交易成功', '339.00', '某某单品02');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217800', '16135320673129243', '店铺1', '2019-01-01', '交易成功', '299.00', '某某单品03');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217801', '16131874317933316', '店铺2', '2021-03-04', '交易失败', '359.00', '某某礼盒');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217802', '16131792695743424', '店铺3', '2021-03-04', '交易中', '79.00', '某某套装');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217803', '16122278134767414', '店铺2', '2021-03-04', '交易失败', '99.00', '某某单品06');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217804', '16131790093817033', '店铺3', '2021-03-04', '交易成功', '15.89', '某某单品03');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217805', '16135230297238674', '店铺2', '2021-03-04', '交易成功', '247.81', '某某礼盒');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217806', '16135220588746073', '店铺1', '2021-03-04', '交易成功', '25.79', '某某套装');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217831', '16131159355051065', '店铺3', '2021-03-04', '交易成功', '359.00', '某某单品07');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217832', '16131196017949185', '店铺2', '2021-03-04', '交易成功', '4.90', '某某单品08');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217833', '16131207902538323', '店铺1', '2021-03-04', '交易成功', '339.00', '某某礼盒');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217834', '12580998687179491', '店铺2', '2021-03-04', '交易成功', '15.89', '某某套装');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217835', '16138210374123403', '店铺3', '2021-03-04', '交易成功', '189.00', '某某单品11');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217836', '16138242030068870', '店铺2', '2021-03-04', '交易成功', '39.90', '某某单品01');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217846', '16134490408511254', '店铺3', '2021-03-04', '交易成功', '238.00', '某某单品07');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217847', '16134370276544509', '店铺2', '2021-03-04', '交易成功', '100.00', '某某单品08');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217854', '16121202131801564', '店铺1', '2021-03-04', '交易成功', '359.00', '某某礼盒');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217855', '16121178732153257', '店铺2', '2021-03-04', '交易成功', '499.00', '某某套装');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217856', '16130716264223504', '店铺3', '2021-03-04', '交易成功', '9.81', '某某单品11');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217857', '16130734211002184', '店铺2', '2021-03-04', '交易成功', '9.90', '某某单品01');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217858', '16134100289526412', '店铺5', '2021-03-04', '交易成功', '359.00', '某某单品02');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217859', '16134103486626066', '店铺3', '2021-03-04', '交易成功', '189.00', '某某单品03');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217860', '16121142702989101', '店铺2', '2021-03-04', '交易成功', '259.00', '某某单品04');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217861', '16137767910421049', '店铺1', '2021-03-04', '交易成功', '299.00', '某某单品05');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217862', '16121018164688502', '店铺5', '2021-03-04', '交易成功', '299.00', '某某单品06');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217887', '16120248152353139', '店铺3', '2021-03-04', '交易成功', '9.90', '某某单品07');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217888', '16136951424489400', '店铺2', '2021-06-07', '交易成功', '9.90', '某某单品08');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217889', '16136924750406856', '店铺1', '2021-05-07', '交易成功', '6.90', '某某单品02');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217916', '16119522769335722', '店铺2', '2021-02-07', '交易中', '6.90', '某某单品03');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217917', '12588728512745597', '店铺1', '2021-01-07', '交易成功', '89.00', '某某礼盒');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217848', '16138039330168579', '店铺2', '2021-03-04', '交易成功', '314.00', '某某套装');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217849', '16130922810196821', '店铺3', '2021-03-04', '交易失败', '199.00', '某某单品06');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217890', '16136941319549862', '店铺2', '2021-04-07', '交易成功', '79.00', '某某单品07');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217793', '16135470341712568', '店铺1', '2019-08-01', '交易成功', '180.00', '某某单品08');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217755', '16132741910343927', '店铺2', '2020-11-01', '交易成功', '6.90', '某某单品11');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217807', '16138852921447547', '店铺2', '2021-03-04', '交易成功', '238.00', '某某单品06');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217891', '16133225738639350', '店铺1', '2021-03-07', '交易失败', '49.00', '某某单品08');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217850', '12591040185524596', '店铺2', '2021-03-04', '交易中', '6.90', '某某礼盒');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217851', '16130856267945884', '店铺3', '2021-03-04', '交易成功', '299.00', '某某套装');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217852', '16121205784010168', '店铺2', '2021-03-04', '交易失败', '19.70', '某某单品11');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217853', '16137863356208213', '店铺1', '2021-03-04', '交易中', '19.70', '某某单品01');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217958', '12588659047949994', '店铺2', '2019-08-07', '交易成功', '9.90', '某某单品11');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217959', '16117515001200723', '店铺3', '2019-07-07', '交易成功', '99.00', '某某单品01');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217960', '16126968285988680', '店铺2', '2019-06-07', '交易成功', '6.90', '某某单品02');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217985', '12588376827205292', '店铺3', '2019-05-07', '交易成功', '337.00', '某某单品03');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217986', '12588344485529392', '店铺2', '2019-04-07', '交易成功', '139.00', '某某单品04');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217987', '16125503474522303', '店铺1', '2021-03-04', '交易失败', '9.81', '某某单品05');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217988', '16129065212801070', '店铺2', '2021-03-04', '交易中', '359.00', '某某礼盒');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217989', '16125466354777343', '店铺3', '2021-03-04', '交易中', '49.00', '某某套装');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217918', '16136147162483080', '店铺2', '2020-12-07', '交易成功', '6.90', '某某单品02');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217919', '12580777996543594', '店铺3', '2020-11-07', '交易成功', '299.00', '某某单品03');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217926', '16135916055519587', '店铺2', '2020-04-07', '交易成功', '359.00', '某某单品04');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217927', '16128748461350415', '店铺3', '2020-03-07', '交易成功', '9.90', '某某单品05');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217952', '16130772755076508', '店铺2', '2020-02-07', '交易成功', '139.00', '某某单品06');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217953', '16130750443205377', '店铺4', '2020-01-07', '交易成功', '4.90', '某某单品07');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217954', '16117587731623017', '店铺5', '2019-12-07', '交易成功', '4.90', '某某单品08');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217955', '16127065063959102', '店铺3', '2019-11-07', '交易成功', '69.00', '某某单品02');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217920', '16128970251579383', '店铺2', '2020-10-07', '交易成功', '90.00', '某某单品03');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217921', '16128964832564531', '店铺2', '2020-09-07', '交易成功', '175.00', '某某礼盒');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217922', '16135999993916188', '店铺3', '2020-08-07', '交易成功', '139.00', '某某套装');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217923', '16136051439214988', '店铺2', '2020-07-07', '交易成功', '9.90', '某某单品06');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217924', '16119347018161682', '店铺5', '2020-06-07', '交易成功', '9.90', '某某单品07');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217925', '16132344851576556', '店铺3', '2020-05-07', '交易成功', '9.90', '某某单品08');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217956', '16130631650814848', '店铺2', '2019-10-07', '交易成功', '79.00', '某某礼盒');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217957', '16130549587928221', '店铺1', '2019-09-07', '交易成功', '6.90', '某某套装');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217990', '12590493961403993', '店铺2', '2021-03-04', '交易成功', '129.00', '某某单品');
INSERT INTO "order_info"("id", "oid", "shop", "date", "status", "payment", "product") VALUES ('051802588006217991', '16115933800269974', '店铺1', '2021-03-04', '交易成功', '79.00', '某某赠品');

准备个问题

这里我找几个基本的问题,比如: 1.我们要找最近两年(2019、2020)有多少笔交易?+ 2.交易成功的平均价格多少? + 3.交易成功的订单有多少? + 4.店铺1、2、3分别卖了多少?

使用filter前

对于以上同类多维度数据求解这里推荐filter,可能熟悉同学大概会记得有这么个用法,不过我们还是简单的思考下:
如果我们将条件筛选放在一个查询里面(不含子查询及表连接) , 这样会在末尾where条件内放置公共条件, 随后我们使用filter对每个结果进行特定的筛选,也许就好了
OK,来尝试使用filter解决以下问题: 找最近两年(2019、2020)有多少笔交易?

问题求解

我们上面抛出了个问题: 找最近两年(2019、2020)有多少笔交易?
很显然这个结果集框定的范围是2019年和2020年 ,所以~

select
	count(1)  as 交易总订单_20_and_19,
	count(1)  filter  ( where date>=to_date('2020-01-01','yyyy-MM-dd') and date < to_date('2021-01-01','yyyy-MM-dd')  )  as 交易总订单_20,
	count(1)  filter ( where date>=to_date('2019-01-01','yyyy-MM-dd') and date < to_date('2020-01-01','yyyy-MM-dd')  )  as 交易总订单_19
from  order_info
where date   >= date_trunc('year',to_date('2021-07-12','yyyy-MM-dd')+interval '-2 year')::date
and date < date_trunc('year',to_date('2021-07-12','yyyy-MM-dd'))::date

运行结果:

交易总订单_20_and_19 | 交易总订单_20 | 交易总订单_19
----------------------+---------------+---------------
                   45 |            24 |            21
(1 row)

如果你是首次使用filter子句,这里我简单的验证下,就验证2019年多少订单吧:

select count(1)   as 交易总订单_19  from order_info where date>=to_date('2019-01-01','yyyy-MM-dd') and date < to_date('2020-01-01','yyyy-MM-dd')  ;

交易总订单_19
---------------
            21
(1 row)

【注意,不论您筛选的上面什么范围内的数据,一定要考虑 where条件一定要框定当前所有结果集合最大的范围,不然sql运行的结果不及预计~ 】

最后,对于一开始的问题给出一个参考sql:

select
	count(1)  as 交易总订单_20_and_19,
	count(1)  filter  ( where date>=to_date('2020-01-01','yyyy-MM-dd') and date < to_date('2021-01-01','yyyy-MM-dd')  )  as 交易总订单_20,
	count(1)  filter ( where date>=to_date('2019-01-01','yyyy-MM-dd') and date < to_date('2020-01-01','yyyy-MM-dd')  )  as 交易总订单_19,
	avg(payment) filter (where  status='交易成功' )  as 交易成功的均价,
	count(1) filter (where  status='交易成功' )  as 交易成功的订单数,
	count(1) filter (where  status!='交易成功' )  as 交易失败的订单数,
	sum(payment) filter (where  status='交易成功' and shop='店铺1' )  as 店铺1交易额,
	sum(payment) filter (where  status='交易成功' and shop='店铺2' )  as 店铺2交易额,
	sum(payment) filter (where  status='交易成功' and shop='店铺3' )  as 店铺3交易额
from  order_info
where date   >= date_trunc('year',to_date('2021-07-12','yyyy-MM-dd')+interval '-2 year')::date
and date < date_trunc('year',to_date('2021-07-12','yyyy-MM-dd'))::date

到此这篇关于postgresql使用filter进行多维度聚合的文章就介绍到这了,更多相关postgresql多维度聚合内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • MongoDB聚合group的操作指南

    MongoDB 聚合 MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). 基本语法为:db.collection.aggregate( [ <stage1>, <stage2>, ... ] ) 现在在mycol集合中有以下数据: { "_id" : 1, "name" : "tom", "sex" :

  • pandas分组聚合详解

    一 前言 pandas学到分组迭代,那么基础的pandas系列就学的差不多了,自我感觉不错,知识追寻者用pandas处理过一些数据,蛮好用的: 知识追寻者(Inheriting the spirit of open source, Spreading technology knowledge;) 二 分组 2.1 数据准备 # -*- coding: utf-8 -*- import pandas as pd import numpy as np frame = pd.DataFrame({ '

  • SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明

    1 分组聚合的原因 SQL中分组函数和聚合函数之前的文章已经介绍过,单说这两个函数有可能比较好理解,分组函数就是group by,聚合函数就是COUNT.MAX.MIN.AVG.SUM. 拿上图中的数据进行解释,假设按照product_type这个字段进行分组,分组之后结果如下图. SELECT product_type from productgroup by product_type 从图中可以看出被分为了三组,分别为厨房用具.衣服和办公用品,就相当于对product_type这个字段进行了

  • postgresql使用filter进行多维度聚合的解决方法

    你有没有碰到过有这样一种场景,就是我们需要看一下某个时间段内各种维度的汇总,比如这样:最近三年我们卖了多少货?有多少订单?平均交易价格多少?每个店铺卖了多少?交易成功的订单有多少?交易失败的订单有多少? 等等...,假使这些数据的明细都在一个表内,该这么做呢? 有没有简单方式?还有如何减少全表扫描以更改的拿到数据? 如果只是简单的利用聚合拿到数据可能您需要写很多sql,具体表现为每一个问题写一段sql 相互之间join起来,这样也许是个好主意,不过对于未充分优化的数据库系统,针对每一块的问题求解

  • PostgreSQL更新表时时间戳不会自动更新的解决方法

    PostgreSQL更新表时时间戳不会自动更新的解决方法,具体如下 操作系统:CentOS7.3.1611_x64 PostgreSQL版本:9.6 问题描述 PostgreSQL执行Insert语句时,自动填入时间的功能可以在创建表时实现,但更新表时时间戳不会自动自动更新. 在mysql中可以在创建表时定义自动更新字段,比如 : create table ab ( id int, changetimestamp timestamp NOT NULL default CURRENT_TIMEST

  • PostgreSQL数据库服务端监听设置及客户端连接方法教程

    众所周知,PostgreSQL 是一个自由的对象-关系数据库服务器(数据库管理系统),是一个可以免费使用的开放源代码数据库系统.本文详细介绍了PostgreSQL数据库服务端监听设置及客户端连接方法,具体如下: 一.背景介绍: 本文所述PostgreSQL服务端运行在RedHat Linux上,IP为:192.168.230.128 客户端安装在Windows XP上, IP为:192.168.230.1 二.配置方法: 1.修改服务端/opt/postgresql/data/postgresq

  • 基于tomcat8 编写字符编码Filter过滤器无效问题的解决方法

    同事遇到编码问题时想做一个解决全站的字符编码过滤器,过滤器类和配置如下: 过滤器类: <span style="font-size:12px;">package com.chaoxing.newspaper.web.filter; import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.r

  • Numpy 改变数组维度的几种方法小结

    来自 <Python数据分析基础教程:Numpy 学习指南(第2版)> Numpy改变数组维度的方法有: reshape() ravel() flatten() 用元组设置维度 transpose() resize() 下面将依次进行说明 0. 首先,创建一个多维数组 from numpy import * a = arange(24) 得到: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] 1.reshape

  • pytorch 调整某一维度数据顺序的方法

    在pytorch中,Tensor是以引用的形式存在的,故而并不能直接像python交换数据那样 a = torch.Tensor(3,4) a[0],a[1] = a[1],a[0] # 这会导致a的结果为a=(a[1],a[1],a[2]) # 而非预期的(a[1],a[0],a[2]) 这是因为引用赋值导致的,在交换过程,如下所示,当b的值赋值与a的时候,因为tmp指针与a是同一变量的不同名,故而tmp的内容也会变为b. # 交换a,b a,b = b,a # 等价于 tmp = a a =

  • 查看postgresql数据库用户系统权限、对象权限的方法

    PostgreSQL简介 PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的对象关系型数据库管理系统.POSTGRES的许多领先概念只是在比较迟的时候才出现在商业网站数据库中.PostgreSQL支持大部分的SQL标准并且提供了很多其他现代特性,如复杂查询.外键.触发器.视图.事务完整性.多版本并发控制等.同样,PostgreSQL也可以用许多方法扩展,例如通过增加新的数据类型.函数.操作符

  • postgresql insert into select无法使用并行查询的解决

    本文信息基于PG13.1. 从PG9.6开始支持并行查询.PG11开始支持CREATE TABLE - AS.SELECT INTO以及CREATE MATERIALIZED VIEW的并行查询. 先说结论: 换用create table as 或者select into或者导入导出. 首先跟踪如下查询语句的执行计划: select count(*) from test t1,test1 t2 where t1.id = t2.id ; postgres=# explain analyze se

  • PostgreSQL忘记postgres账号密码的解决方法

    PostgreSQL简介 PostgreSQL是一个功能非常强大的.源代码开放的客户/服务器关系型数据库管理系统(RDBMS).PostgreSQL最初设想于1986年,当时被叫做Berkley Postgres Project.该项目一直到1994年都处于演进和修改中,直到开发人员Andrew Yu和Jolly Chen在Postgres中添加了一个SQL(Structured Query Language,结构化查询语言)翻译程序,该版本叫做Postgres95,在开放源代码社区发放. 下面

  • 详解PostgreSQL提升批量数据导入性能的n种方法

    关键字:批量数据导入,数据加载,大量插入,加快,提升速度 多元化选择时代,人生里很多事物都是如此,凡事都没有一成不变的方式和方法.不管白猫黑猫,能抓老鼠的就是好猫,适合自己的就是最好的. 提升批量数据导入的方法亦是如此,没有何种方法是最优的,应用任何方法前根据自己的实际情况权衡利弊,做出选择. 批量导入数据之前,无论采取何种方式,务必做好相应的备份. 导入完成后亦需对相应对象进行ANALYZE操作,这样查询优化器才会按照最新的统计信息生成正确的执行计划. 下面正式介绍提升批量数据导入性能的n种方

随机推荐