Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
having子句中的sql sum列不是按季度值求和_Sql - Fatal编程技术网

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

having子句中的sql sum列不是按季度值求和,sql,Sql,我想对购买价值大于0且的客户的购买金额进行汇总,我想您需要: 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

我想对购买价值大于0且的客户的购买金额进行汇总,我想您需要:

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

样本数据和期望的输出会很有帮助。同时标记数据库名称我的结果是:请澄清问题。你真的很难实现你想要实现的目标。同时标记您正在使用的数据库。样本数据和所需结果将有所帮助。可能希望将purchase_prd保留在SELECT中,即使您已将其正确地放置在group by…:上面的查询不起作用,没有给我总购买值>0的customerID计数-如果在上面的查询中添加countdistinct customer\u id会怎么样??
purchase_prd            customer ID         purchase_value
201700                          714              776
201703                           714              120
201706                           714              50
201709                            714             20
201712                           714              100
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