SQL 根据另一列中的各种文本对一列求和
SQL Sum one column based on various text in another
这里是第一个问题,所以不确定如何真正描述我的问题。
假设我有一个非常大的 table 叫做 "soitem"。从此 table,我希望看到行 "soitem.productnum"、"soitem.description" 和 "oitem.qtyfulfilled"。
这将被 "soitem.datelastfullfillment" 过滤,目前看起来是这样的:
SELECT soitem.productnum, SUM(soitem.qtyfulfilled) AS Total_Sold
FROM soitem
WHERE soitem.datelastfulfillment BETWEEN '9/1/15' AND '9/30/15'
GROUP BY soitem.productnum
我想更进一步,让它只在不同的列 "qbclassid" 等于 (2, 3, 9, 12, 14) 时对 qtyfulfilled
求和。
如果您注意到,我的查询也没有显示 soitme.description
,因为它通常会给我一个错误。如果有人也能让它工作,那就更好了!
试试这个:
SELECT soitem.productnum, soitem.description, SUM(soitem.qtyfulfilled) AS Total_Sold
FROM soitem
WHERE soitem.datelastfulfillment BETWEEN '9/1/15' AND '9/30/15'
AND soitem.qbclassid IN (2, 3, 9, 12, 14)
GROUP BY soitem.productnum, soitem.description
这里是第一个问题,所以不确定如何真正描述我的问题。
假设我有一个非常大的 table 叫做 "soitem"。从此 table,我希望看到行 "soitem.productnum"、"soitem.description" 和 "oitem.qtyfulfilled"。
这将被 "soitem.datelastfullfillment" 过滤,目前看起来是这样的:
SELECT soitem.productnum, SUM(soitem.qtyfulfilled) AS Total_Sold
FROM soitem
WHERE soitem.datelastfulfillment BETWEEN '9/1/15' AND '9/30/15'
GROUP BY soitem.productnum
我想更进一步,让它只在不同的列 "qbclassid" 等于 (2, 3, 9, 12, 14) 时对 qtyfulfilled
求和。
如果您注意到,我的查询也没有显示 soitme.description
,因为它通常会给我一个错误。如果有人也能让它工作,那就更好了!
试试这个:
SELECT soitem.productnum, soitem.description, SUM(soitem.qtyfulfilled) AS Total_Sold
FROM soitem
WHERE soitem.datelastfulfillment BETWEEN '9/1/15' AND '9/30/15'
AND soitem.qbclassid IN (2, 3, 9, 12, 14)
GROUP BY soitem.productnum, soitem.description