我写了下面的查询,它设计了产品总数乘以数量价格并将其放入新列。我在想什么,有点困惑。那么还有一种方法可以完成这个新专栏。因此,除了获得订购的每一行的总数之外,我还可以获得订单本身的总数。
SELECT Product_Quantity, ProductPrice, (Product_Quantity*ProductPrice) as Total FROM Order_Info WHERE Order_Number = '1'
答案 0 :(得分:2)
这会添加一个总记录。 使用ROLLUP模拟GROUP BY
SELECT
Product_Quantity
, ProductPrice
, (Product_Quantity*ProductPrice) AS Total
FROM
Order_Info
WHERE
Order_Number = '1'
UNION ALL
SELECT
NULL
, NULL
, SUM((Product_Quantity*ProductPrice)) AS Total
FROM
Order_Info
WHERE
Order_Number = '1'
答案 1 :(得分:1)
要获取摘要行,您可以使用sql的union
和sum
聚合函数。试试这个:
SELECT Product_Quantity, ProductPrice, (Product_Quantity*ProductPrice) as Total FROM Order_Info WHERE Order_Number = '1'
union all
SELECT sum(Product_Quantity), sum(ProductPrice), sum(Product_Quantity*ProductPrice) as Total FROM Order_Info WHERE Order_Number = '1'