- CREATE TABLE [dbo].[GPDATA](
- [AA] [varchar](50) NULL,
- [BB] [varchar](50) NULL,
- [CC] [numeric](18, 0) NULL
- ) ON [PRIMARY]
- GO
- INSERT [dbo].[GPDATA] VALUES('A','A1',1)
- INSERT [dbo].[GPDATA] VALUES('A','A2',2)
- INSERT [dbo].[GPDATA] VALUES('A','A3',3)
- INSERT [dbo].[GPDATA] VALUES('B','B1',4)
- INSERT [dbo].[GPDATA] VALUES('B','B2',5)
- INSERT [dbo].[GPDATA] VALUES('B','B3',6)
- SELECT CASE WHEN GROUPING(AA) = 1 THEN '合计' ELSE AA END --判断指定分组的字段如果要聚合则显示合计否则显示原来的值
- ,SUM(CC) --聚合的字段
- ,GROUPING(AA) --分组字段是否聚合的值
- FROM GPDATA
- WHERE 1 = 1
- GROUP BY AA
- WITH ROLLUP
- SELECT
- CASE WHEN GROUPING(AA) = 1 THEN '合计' ELSE AA END
- ,CASE WHEN GROUPING(BB) = 1 AND GROUPING(AA) = 0 THEN '小计' ELSE AA END
- ,SUM(CC)
- ,GROUPING(AA)
- ,GROUPING(BB)
- FROM GPDATA
- WHERE 1 = 1
- GROUP BY AA,BB
- WITH ROLLUP
复制代码
|
|
hessen