我有这个SQL Server 2005查询
SELECT OI.BILL_NO,OI.ITEM_NAME,ODS.QUANTITY
FROM ORDER_ITEMS OI
INNER JOIN ORDER_ASSIGNMENTS OAS ON OI.BILL_NO=OAS.BILL_NO
INNER JOIN ORDERS ODS ON OI.BILL_NO = ODS.BILL_NO
INNER JOIN ORDERS OD ON OD.BILL_NO = OI.BILL_NO
WHERE OAS.ASSIGNMENT_TYPE= 'TO_STICHING' AND OAS.EMPLOYEE_NAME = 'AMIT'
我得到了这个结果:
现在我的要求是获取每个不同ITEM_NAME的总数量,例如:
CHURIDER 5
CHURI PA 4
LACHA 1
ASTRA 1
ASTRA LACHA DOUBLE 1
像这样,所有不同的ITEM_NAME及其总数量
答案 0 :(得分:2)
这看起来像是一个简单的聚合查询group by
和sum()
:
SELECT OI.ITEM_NAME,sum(ODS.QUANTITY) as Quantity
FROM ORDER_ITEMS OI
INNER JOIN ORDER_ASSIGNMENTS OAS ON OI.BILL_NO=OAS.BILL_NO
INNER JOIN ORDERS ODS ON OI.BILL_NO = ODS.BILL_NO
INNER JOIN ORDERS OD ON OD.BILL_NO = OI.BILL_NO
WHERE OAS.ASSIGNMENT_TYPE= 'TO_STICHING' AND OAS.EMPLOYEE_NAME = 'AMIT'
group by OI.ITEM_NAME
答案 1 :(得分:1)
您可以获取列的总和,然后按名称对其进行分组
SELECT OI.BILL_NO,OI.ITEM_NAME,SUM(ODS.QUANTITY)
FROM ORDER_ITEMS OI
INNER JOIN ORDER_ASSIGNMENTS OAS ON OI.BILL_NO=OAS.BILL_NO
INNER JOIN ORDERS ODS ON OI.BILL_NO = ODS.BILL_NO
INNER JOIN ORDERS OD ON OD.BILL_NO = OI.BILL_NO
WHERE OAS.ASSIGNMENT_TYPE= 'TO_STICHING' AND OAS.EMPLOYEE_NAME = 'AMIT'
GROUP BY OI.BILL_NO,OI.ITEM_NAME
答案 2 :(得分:1)