sql的sum函数(与groupby,having⼦句混合使⽤)
SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Bush' OR Customer='Adams'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500
Select sum(downloads) as download from factdownloads_new
where date_time='2013-12-11' and storename='anzhi'
结果:3595443702 3253205502 3631413902 3342157901
⽤别名as:
Select sum(downloads) from factdownloads_new
where date_time='2013-12-11' and storename='hiapk'
结果:8937649006 8927173006 8952359006 8978768006
sum函数
SQL GROUP BY 实例
我们拥有下⾯这个 "Orders" 表:
O_Id OrderDate OrderPrice Customer
12008/12/291000Bush
22008/11/231600Carter
32008/10/05700Bush
42008/09/28300Bush
52008/08/062000Adams
62008/07/21100Carter
现在,我们希望查每个客户的总⾦额(总订单)。
我们想要使⽤ GROUP BY 语句对客户进⾏组合。
我们使⽤下列 SQL 语句:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
结果集类似这样:
Customer SUM(OrderPrice)
Bush2000
Carter1700
Adams2000
sum与having⼀起使⽤
过滤数据结果:HAVING ⼦句筛选满⾜条件的结果集
在 SQL 中增加 HAVING ⼦句原因是,WHERE 关键字⽆法与合计函数⼀起使⽤
这个 "Orders" 表:
O_Id OrderDate OrderPrice Customer
groupby是什么函数12008/12/291000Bush
22008/11/231600Carter
32008/10/05700Bush
42008/09/28300Bush
52008/08/062000Adams
62008/07/21100Carter
现在,我们希望查订单总⾦额少于 2000 的客户。
我们使⽤如下 SQL 语句:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000
结果集类似:
Customer SUM(OrderPrice)
Carter1700
现在我们希望查客户 "Bush" 或 "Adams" 拥有超过 1500 的订单总⾦额。
我们在 SQL 语句中增加了⼀个普通的 WHERE ⼦句:
SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Bush' OR Customer='Adams'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500
结果集:
Customer SUM(OrderPrice)
Bush2000
Adams2000
更多
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论