在开始下一组之前查询总行数据组?

时间:2012-11-01 23:07:24

标签: sql sql-server sql-server-2008 tsql

我需要在SQL Server中查询以下格式的一些数据:

  • Id Group Price
  • 1 A 10
  • 2 A 20
  • Sum 30

  • 1 B 6

  • 2 B 4
  • 总和10

  • 1 C 100

  • 2 C 200
  • Sum 300

我想在以下几个步骤中做到这一点:

  1. 查询一个小组
  2. 在其他查询中求和
  3. 使用Union运算符组合此结果集
  4. 对所有组执行步骤1-3,最后使用union返回所有子数据集。
  5. 有更好的方法吗?可能正在使用一些开箱即用的功能?请指教。

    修改

    根据建议和代码示例,我尝试了这段代码:

    Select 
    Case 
    when id is null then 'SUM' 
    else CAST(id as Varchar(10)) end as ID,
    
    Case when [group] is null then 'ALL' else CAST([group] as Varchar(50)) end as [group]
    ,Price from
    (
    SELECT Id,  [Group],BGAApplicationID,Section, SUM(PrimaryTotalArea) AS price   
    FROM vwFacilityDetails
    where bgaapplicationid=1102
    GROUP BY Id,  [Group],BGAApplicationID,Section  WITH ROLLUP
    ) a
    

    甚至这个代码也是如此:

    Select Id,  [Group],BGAApplicationID,Section, SUM(PrimaryTotalArea) AS price           
    From   vwFacilityDetails
    Where  Not ([group] Is Null And id Is Null And BGAApplicationId is null and section is null) and BGAApplicationId=1102
    Group By Id,  [Group],BGAApplicationID,Section 
        With Rollup
    

    在结果中,它对数据进行分组,但对于每个记录,它显示3次(在上述两个代码中),如:

    • 2879现有设施全校25.00
    • 2879现有设施全校25.00
    • 2879现有设施全校25.00
    • 2879 ALL 25.00

    我想我的查询中存在一些问题,请在这里指导我。 感谢

3 个答案:

答案 0 :(得分:3)

Select
  id,
  [Group],
  SUM(price) AS price
From
  Test
Group By
  [group],
  id 
With
  Rollup

http://sqlfiddle.com/#!3/080cd/8

答案 1 :(得分:3)

SQL Server引入了GROUPING SETS,这是您应该使用的内容。

SQL Fiddle

MS SQL Server 2008架构设置

Create Table vwFacilityDetails (
  id int not null,
  [group] char(1) not null,
  PrimaryTotalArea int not null,
  Section int,
  bgaapplicationid int
);

Insert Into vwFacilityDetails (id, [group], Section,bgaapplicationid,PrimaryTotalArea) values
  (1, 'A', 1,1102,2),
  (1, 'A', 1,1102,1),
  (1, 'A', 1,1102,7),
  (2, 'A', 1,1102,20),
  (1, 'B', 1,1102,6),
  (2, 'B', 1,1102,4),
  (1, 'C', 1,1102,100),
  (2, 'C', 1,1102,200);

查询1

SELECT CASE WHEN Id is null then 'SUM'
            ELSE Right(Id,10) end Id,
       [Group],BGAApplicationID,Section,
       SUM(PrimaryTotalArea) price   
FROM vwFacilityDetails
where bgaapplicationid=1102
GROUP BY GROUPING SETS (
  (Id,[Group],BGAApplicationID,Section),
  ([Group])
)
ORDER BY [GROUP],
         ID;

<强> Results

|  ID | GROUP | BGAAPPLICATIONID | SECTION | PRICE |
----------------------------------------------------
|   1 |     A |             1102 |       1 |    10 |
|   2 |     A |             1102 |       1 |    20 |
| SUM |     A |           (null) |  (null) |    30 |
|   1 |     B |             1102 |       1 |     6 |
|   2 |     B |             1102 |       1 |     4 |
| SUM |     B |           (null) |  (null) |    10 |
|   1 |     C |             1102 |       1 |   100 |
|   2 |     C |             1102 |       1 |   200 |
| SUM |     C |           (null) |  (null) |   300 |

答案 2 :(得分:1)

Select Case when id is null then 'SUM' else CAST(id as Varchar(10)) end as ID
,Case when [group] is null then 'ALL' else CAST([group] as Varchar(10)) end as [group]
,Price from
(
SELECT id, [group], SUM(price) AS Price
FROM IG
GROUP BY [GROUP],ID WITH ROLLUP
) a