Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group by two columns and display grand total in every row

Below are the list data.

Code   ItemCount   Type      Amount 
----------------------------------------
B001    1          Dell         10.00
B001    1          Dell         10.00
B001    1          Apple        10.00
B001    2          Apple        20.00
B001    2          Apple        20.00
B114    1          Apple        30.50
B114    1          Apple        10.00

I need a result to group by code and by type and total the ItemCount and get the grand total of the Amount in every row.

Is this possible?

Code   ItemCount    Type      Amount 
----------------------------------------
B001    2          Dell          20.00
B001    5          Apple         50.00
B114    2          Apple         40.50 
like image 867
user2617053 Avatar asked Jul 25 '13 04:07

user2617053


People also ask

Can we use count and GROUP BY together?

The use of COUNT() function in conjunction with GROUP BY is useful for characterizing our data under various groupings. A combination of same values (on a column) will be treated as an individual group.

Can we do GROUP BY on 2 columns in SQL?

We can use the group by multiple-column technique to group multiple records into a single record. All the records with the same values for the respective columns mentioned in the grouping criteria can be grouped as a single column using the group by multiple-column technique.

Can you GROUP BY multiple columns at once?

We can group the resultset in SQL on multiple column values. When we define the grouping criteria on more than one column, all the records having the same value for the columns defined in the group by clause are collectively represented using a single record in the query output.

How do I SUM all rows in SQL?

If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the basic syntax: SELECT SUM(column_name) FROM table_name; If you need to arrange the data into groups, then you can use the GROUP BY clause.


2 Answers

Please try:

SELECT
    Code,
    SUM(ItemCount) ItemCount,
    Type,
    SUM(Amount) Amount
FROM
    YourTable
GROUP BY Code, Type
ORDER BY Code
like image 91
TechDo Avatar answered Sep 21 '22 19:09

TechDo


This looks like homework.

(I swear I thought this was tagged as MySQL when I first looked at the question, but the title clearly shows MS SQL)

For MySQL, this query will return the specified resultset:

SELECT t.Code
     , SUM(t.ItemCount) AS ItemCount
     , t.Type
     , s.Amount AS Amount
  FROM mytable t
 CROSS
  JOIN ( SELECT SUM(r.Amount) AS Amount
           FROM mytable r
       ) s 
 GROUP
    BY t.Code
     , t.Type
 ORDER BY t.Code ASC, t.Type DESC

For other databases, remove For MySQL the backticks from around the column aliases.

If you need to preserve case, for Oracle, identifiers are enclosed in doublequotes. For SQL Server, identifiers are enclosed in square brackets. For MySQL identifiers are enclosed in backticks.

like image 29
spencer7593 Avatar answered Sep 19 '22 19:09

spencer7593