0

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

  • 标识组价格
  • 1个10
  • 2个20
  • 总和 30

  • 1 B 6

  • 2 乙 4
  • 总和 10

  • 1 C 100

  • 2 C 200
  • 总和 300

我正在考虑按照以下步骤进行操作:

  1. 查询一组
  2. 在其他查询中求和
  3. 使用联合运算符组合此结果集
  4. 对所有组执行步骤 1-3,最后使用联合返回所有子数据集。

有一个更好的方法吗 ?可能正在使用一些开箱即用的功能?请指教。

编辑:

根据建议和代码示例,我尝试了以下代码:

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 全部 25.00

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

4

3 回答 3

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

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

于 2012-11-01T23:33:34.777 回答
3

SQL Server 引入了您应该使用的GROUPING SETS 。

SQL小提琴

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;

结果

|  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 |
于 2012-11-02T02:10:35.407 回答
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
于 2012-11-01T23:35:37.743 回答