與 SQL 服務器匯總匯總 - 但只有最後一個摘要? (Sum with SQL server RollUP - but only last summary?)


問題描述

與 SQL 服務器匯總匯總 ‑ 但只有最後一個摘要? (Sum with SQL server RollUP ‑ but only last summary?)

I have this query: 

DECLARE @t TABLE(NAME NVARCHAR(MAX),datee date,val money)

insert INTO @t SELECT 'a','2012‑01‑02',100
insert INTO @t SELECT 'a','2012‑01‑02',100
insert INTO @t SELECT 'a','2012‑01‑03',100
insert INTO @t SELECT 'a','2012‑01‑05',100
insert INTO @t SELECT 'b','2012‑01‑06',200
insert INTO @t SELECT 'b','2012‑01‑07',200
insert INTO @t SELECT 'd','2012‑01‑07',400
insert INTO @t SELECT 'e','2012‑01‑09',500
insert INTO @t SELECT 'f','2012‑01‑12',600

SELECT  Name,datee,SUM (val) 
from @t GROUP BY NAME ,datee 

currently the result is: 

BUT I need to add sum at the end. So I tried with rollup: 

 SELECT  Name,datee,SUM (val) 
    from @t GROUP BY NAME ,datee  with ROLLUP

BUT I only need the last sum total line.  I don't need the in‑report sum's

So how can get the desire result?

(I cant change the  group by clause  cause others need it also , I just want to add sum at the end with/without rollup).

sql online is here 

‑‑‑‑‑

參考解法

方法 1:

It's possible with GROUPING SETS, try this: 

SELECT  Name,datee,SUM (val) 
FROM    @t 
GROUP BY 
        GROUPING SETS((NAME ,datee), ())

SQL Fiddle

方法 2:

It is also possible with ROLLUP():

SELECT
  Name,
  datee,
  SUM (val) 
FROM @t 
GROUP BY 
  ROLLUP((NAME, datee))
;

WITH ROLLUP, as well as WITH CUBE, are non‑standard and deprecated. (See Non‑ISO Compliant Syntax in the GROUP BY manual.)

It should be noted that ROLLUP() isn't supported in compatibility level under 90 in SQL Server 2005 or under 100 in SQL Server 2008+, while GROUPING SETS() is.

方法 3:

If you just want the final total, can't you just use a UNION ALL:

SELECT  Name,datee,SUM (val) 
from @t 
GROUP BY NAME ,datee 
union all
SELECT  null,null,SUM (val) 
from @t

See SQL Fiddle with Demo

Or you can use a WHERE clause to filter the rows with the null values:

select name, 
  datee, 
  total
from
(
  SELECT  Name,datee,SUM (val) total
  from @t 
  GROUP BY NAME, datee with rollup
) src
where datee is not null
or
(
  name is null 
  and datee is null
)

See SQL Fiddle with Demo

The result is:

|   NAME |      DATEE | COLUMN_2 |
‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
|      a | 2012‑01‑02 |      200 |
|      a | 2012‑01‑03 |      100 |
|      a | 2012‑01‑05 |      100 |
|      b | 2012‑01‑06 |      200 |
|      b | 2012‑01‑07 |      200 |
|      d | 2012‑01‑07 |      400 |
|      e | 2012‑01‑09 |      500 |
|      f | 2012‑01‑12 |      600 |
| (null) |     (null) |     2300 |

方法 4:

You can use this query :

SELECT  *
FROM    ( SELECT    Name ,
                    datee ,
                    SUM(val) summ
          FROM      @t
          GROUP BY  NAME ,
                    datee
                    WITH ROLLUP
        ) A
WHERE   ( datee IS NOT NULL
          OR ( datee IS NULL
               AND name IS NULL
             )
        )

(by Royi NamirIvan GolovićAndriy MTarynShyam Prasad)

參考文件

  1. Sum with SQL server RollUP ‑ but only last summary? (CC BY‑SA 3.0/4.0)

#sql-server-2008 #sql-server #sum #rollup






相關問題

基於集合的插入到具有 1 到 0-1 關係的兩個表中 (Set based insert into two tables with 1 to 0-1 relation)

如何使用 CTE 從分層視圖中獲取元素 (How to get elements from a hierarchical view with CTE)

where 子句中使用等於和 IN 的 CASE 語句 (CASE Statement in where clause using equal to and IN)

與 SQL 服務器匯總匯總 - 但只有最後一個摘要? (Sum with SQL server RollUP - but only last summary?)

將範圍分組到範圍 (Group a range towards a range)

在 SQL Server 2008 中運行 WHILE 或 CURSOR 或兩者 (Running WHILE or CURSOR or both in SQL Server 2008)

按組中的最大值排序 (Order by the max value in the group)

使用存儲過程創建搜索函數 (Creating a Search Function, using stored procedure)

在 sql server 2008 中查詢最後費用日期 (Query for Last Fees Date in sql server 2008)

除了 SQL Server Profiler,還有什麼 SQL Server Profile? (What is SQL Server Profile aside from SQL Server Profiler?)

什麼是日期時間2? (What is datetime2?)

可以在這裡使用 Common Table 表達式來提高性能嗎? (Can Common Table expressions be used here for performance?)







留言討論