SQL 选择多个总和? [英] SQL Selecting multiple sums?

查看:32
本文介绍了SQL 选择多个总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一张桌子:

SELECT  SUM(quantity) AS items_sold_since_date,
        product_ID
FROM    Sales
WHERE order_date >= '01/01/09'
GROUP BY product_ID

这将返回一个产品列表,其中包含自特定日期以来的销售数量.有没有办法不仅选择这个总和,而且还选择没有 where 条件的总和?我想查看自特定日期以来每种产品的销售额以及所有(不受日期限制的)销售额.

This returns a list of products with the quantity sold since a particular date. Is there a way to select not only this sum, but ALSO the sum WITHOUT the where condition? I'd like to see sales since a particular date for each product alongside all (not date limited) sales.

推荐答案

SELECT  SUM(CASE WHEN order_date >= '01/01/09' THEN quantity ELSE 0 END) AS items_sold_since_date,
        SUM(quantity) AS items_sold_total,
        product_ID
FROM    Sales
GROUP BY product_ID

这篇关于SQL 选择多个总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆