Select multiple columns from a table, but group by one

后端 未结 11 1442
逝去的感伤
逝去的感伤 2020-12-12 14:54

The table name is \"OrderDetails\" and columns are given below:

OrderDetailID || ProductID || ProductName || OrderQuantity

I\'m trying to s

11条回答
  •  误落风尘
    2020-12-12 15:34

    You can try the below query. I assume you have a single table for all your data.

    SELECT OD.ProductID, OD.ProductName, CalQ.OrderQuantity
    FROM (SELECT DISTINCT ProductID, ProductName
          FROM OrderDetails) OD
    INNER JOIN (SELECT ProductID, OrderQuantity SUM(OrderQuantity)
                FROM OrderDetails
                GROUP BY ProductID) CalQ
    ON CalQ.ProductID = OD.ProductID
    

提交回复
热议问题