having子句中的sql sum列不是按季度值求和

fzwojiic  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(238)

我想求和(purchase\u prd),这是按季度计算的customerid,其中purchase value>0且<=500。我在多个采购项目中有相同的客户ID,还想看看他们的客户ID显示了多少条记录…我该如何查询?我有以下几点

select purchase_prd, count(*), customerID, sum(purchase_value)
from table a
where purcahse_prd between 201700 and 201712 /*data is quarterly, so 201700, 201703, 201706,201709, 201712*/
group by customerid, purchase_value
having purchase_value >0 and purchase_value<=500

我的结果显示,客户在多个季度中的购买价值总和超过500,每个季度是独立的,不提取全年的购买价值总和,购买价值>0且<=500
我的结果是:

purchase_prd            customer ID         purchase_value
201700                          714              776
201703                           714              120
201706                           714              50
201709                            714             20
201712                           714              100

我想为customerid 714计算2017年的总和,如果购买值的总和大于0-<=500,则选择该值

wfveoks0

wfveoks01#

我想你想要:

select customerID, purchase_prd, count(*), sum(purchase_value)
from table a
where purchase_prd between 201700 and 201712 /*data is quarterly, so 201700, 201703, 201706,201709, 201712*/
group by customerid, purchase_prd
having sum(purchase_value) > 0 and sum(purchase_value) <= 500

相关问题