使用Group By获得Grand Totals

时间:2012-04-29 10:22:24

标签: sql sql-server-2008

我正在尝试更多地了解SQL,目前正在尝试使用SUMCOUNTAVGGROUP BY执行一些简单的销售报告SQL Server 2008数据库中的函数。我已经设法按行获得每组的总数,计数和平均值。

如何按行获取所有组的总计?

到目前为止

SQL:

SELECT 
    SUM(dbo.tbl_orderitems.mon_orditems_pprice) AS prodTotal,
    AVG(dbo.tbl_orderitems.mon_orditems_pprice) AS avgPrice,
    count(dbo.tbl_orderitems.uid_orditems_prodid) AS prodQty,
    dbo.tbl_orderitems.txt_orditems_pname
FROM 
    dbo.tbl_orderitems
INNER JOIN 
    dbo.tbl_orders 
    ON (dbo.tbl_orderitems.uid_orditems_orderid = dbo.tbl_orders.uid_orders)
WHERE 
    dbo.tbl_orders.uid_order_webid = 
       <cfqueryparam cfsqltype="cf_sql_integer" value="#session.webid#">
    AND dbo.tbl_orders.txt_order_status = 
       <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.sale_status#">
GROUP BY
    dbo.tbl_orderitems.txt_orditems_pname
Product                           Qty      Gross        Avg  

Westbury Climbing Frame           17       8,023.00     471.94
Sandpoint Deluxe Climbing Frame   34       36,146.00    1,063.12
Roseberry Climbing Frame          9        7,441.00     826.78
Ridgeview Texas Climbing Frame    10       6,990.00     699
Selwood Picnic Table              9        489.92       54.44

I need the Totals of qty column and gross column

非常感谢

杰森

3 个答案:

答案 0 :(得分:16)

您正在寻找ROLLUP运算符,它会在结果集的末尾添加一个总行。如果您正在寻找更复杂的聚合总计,请使用带有GROUP BY子句的ROLLUP或CUBE,例如@MartinSmith提供的链接或Aggregation WITH ROLLUP

SELECT 
    SUM(dbo.tbl_orderitems.mon_orditems_pprice) AS prodTotal,
    AVG(dbo.tbl_orderitems.mon_orditems_pprice) AS avgPrice,
    count(dbo.tbl_orderitems.uid_orditems_prodid) AS prodQty,
    dbo.tbl_orderitems.txt_orditems_pname
FROM 
    dbo.tbl_orderitems
INNER JOIN 
    dbo.tbl_orders ON (dbo.tbl_orderitems.uid_orditems_orderid = dbo.tbl_orders.uid_orders)
WHERE 
    dbo.tbl_orders.uid_order_webid = <cfqueryparam cfsqltype="cf_sql_integer" value="#session.webid#">
    AND dbo.tbl_orders.txt_order_status = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.sale_status#">
GROUP BY
    dbo.tbl_orderitems.txt_orditems_pname
WITH ROLLUP

答案 1 :(得分:4)

我知道这是一个老问题,但仅供将来参考 - 您还可以通过使用GROUPING SETS获得对分组过程的更多控制。例如:

SELECT 
    SUM(dbo.tbl_orderitems.mon_orditems_pprice) AS prodTotal,
    AVG(dbo.tbl_orderitems.mon_orditems_pprice) AS avgPrice,
    count(dbo.tbl_orderitems.uid_orditems_prodid) AS prodQty,
    COALESCE(dbo.tbl_orderitems.txt_orditems_pname, 'TOTAL')
FROM 
    dbo.tbl_orderitems
INNER JOIN 
    dbo.tbl_orders 
    ON (dbo.tbl_orderitems.uid_orditems_orderid = dbo.tbl_orders.uid_orders)
WHERE 
    dbo.tbl_orders.uid_order_webid = 
       <cfqueryparam cfsqltype="cf_sql_integer" value="#session.webid#">
    AND dbo.tbl_orders.txt_order_status = 
       <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.sale_status#">
GROUP BY GROUPING SETS (
    (dbo.tbl_orderitems.txt_orditems_pname),
    ()
)

这样,结果将包含按txt_orditems_pname分组的两行,并且根本没有分组。您可以指定更多分组集,即。查询部门和团队的平均工资,部门和整个公司的总数也返回。

答案 2 :(得分:1)

将您的选择包装在另一个选择中并总结列。