Group by two columns and display grand total in every row

后端 未结 6 1062
太阳男子
太阳男子 2020-12-06 05:00

Below are the list data.

Code   ItemCount   Type      Amount 
----------------------------------------
B001    1          Dell         10.00
B001    1                


        
6条回答
  •  隐瞒了意图╮
    2020-12-06 05:23

    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.

提交回复
热议问题