Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Grand Totals using Group By

I am trying to teach myself more about SQL at the moment and am currently trying to carry out some simple sales reporting using SUM, COUNT, AVG and GROUP BY functions within a SQL Server 2008 database. I have managed to get the total, count and average of each group by row.

How do I get the grand total of all the group by rows?

SQL so far:

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

Many thanks

Jason

like image 911
Jason Congerton Avatar asked Apr 29 '12 10:04

Jason Congerton


People also ask

How do I sum after GROUP BY in SQL?

SUM is used with a GROUP BY clause. The aggregate functions summarize the table data. Once the rows are divided into groups, the aggregate functions are applied in order to return just one value per group. It is better to identify each summary row by including the GROUP BY clause in the query resulst.

How do I get the grand total in SQL?

In order to calculate a subtotal in SQL query, we can use the ROLLUP extension of the GROUP BY statement. The ROLLUP extension allows us to generate hierarchical subtotal rows according to its input columns and it also adds a grand total row to the result set.

Can we use count with GROUP BY?

The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.


2 Answers

You are looking for the ROLLUP operator which would add a grand total row at the end of the result set. If you are looking for more complex aggregate totals use ROLLUP or CUBE with the GROUP BY clause, such as the link provided by @MartinSmith or 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
like image 174
matthewnreid Avatar answered Sep 17 '22 17:09

matthewnreid


I know this is an old question, but just for future reference - you can also gain more control over the grouping process by using GROUPING SETS. For example:

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),
    ()
)

This way the result will have both rows grouped by the txt_orditems_pname, and with no grouping at all. You can specify more grouping sets, ie. a query for average salary in a department and team, with totals for department and the entire company also returned.

like image 34
Apokralipsa Avatar answered Sep 20 '22 17:09

Apokralipsa