简体   繁体   English

有子句中的 sql 总和列未对季度值求和

[英]sql sum column in having clause is not summing quarterly values

i would like to sum (purchase_prd) which is quarterly for customerID where purchase value is >0 and <=500.我想求和(purchase_prd),这是每季度购买价值大于 0 且 <=500 的 customerID。 I have same customer ids in multiple purchase_prd, and would also like to see how many records show for their customerID....how do I query this?我在多个purchase_prd 中有相同的客户ID,并且还想查看他们的customerID 显示了多少条记录......我该如何查询? I have the following我有以下

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

my results show customerids in multiple quarters and the sums of purchase_value exceeds 500, each quarter is separate and not extracting the total of purchase_value for the entire year with the criteria of purchase_value >0 and <=500我的结果显示多个季度的 customerids 并且 purchase_value 的总和超过 500,每个季度都是独立的,并且没有以 purchase_value >0 和 <=500 的标准提取全年的 purchase_value 总数

my results are:我的结果是:

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

I'd like 2017 summed for customerID 714 and selected if sum of purchase_value is >0-<=500我希望 2017 年对 customerID 714 求和,如果 purchase_value 的总和 >0-<=500 则选择

I think you want:我想你想要:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM