添加分组后的计数小计

时间:2017-06-14 03:07:07

标签: sql sql-server tsql

我知道我的问题似乎很熟悉,但我仍然坚持这一点。我试图按组计算,我有这样的查询:

declare @current varchar(4)
set @current = left(dbo.fdatetotext(getdate()),4)

SELECT (Case when GROUPING(NAME) = 0 and
                  GROUPING(CustID) = 1 and
                  GROUPING(Cust_Name) = 1 and
                  GROUPING(Cust_Type) =1
             then 'Total ' + NAME
             when GROUPING(NAME) = 1 and
                  GROUPING(CustID) = 1 and
                  GROUPING(Cust_Name) =1 and
                  GROUPING(Cust_Type) =1
                  then 'Total'
             ELSE NAME
        END) as Name, 
        CustID, 
        (Case when GROUPING(CNAME) = 0 and
                  GROUPING(CustID) = 1 and
                  GROUPING(Cust_Name) = 1 and
                  GROUPING(Cust_Type) =1
            then CONVERT(varchar(10), COUNT(Distinct Cust_Name ))
            when GROUPING(CNAME) = 1 and
                  GROUPING(CustID) = 1 and
                  GROUPING(Cust_Name) = 1 and
                  GROUPING(Cust_Type) =1
            then CONVERT(varchar(10), COUNT(Distinct Cust_Name ))
            ELSE Cust_Name
        END) as Cust_Name, 
        Cust_Type,
        SUM(Case when LEFT(DATE,6) = @current + '01' then Price/1000000 ELSE 0 END) AS 'Jan',
        SUM(Case when LEFT(DATE,6) = @current + '02' then Price/1000000 ELSE 0 END) AS 'Feb'
FROM A JOIN B ON A.id = B.id
WHERE LEFT(DATE,4) = '2017'
GROUP BY GROUPING SETS((NAME, CustID, Cust_Name, Cust_Type),
                       (NAME),
                       ()
                      );

但我得到的结果是错误的。子总数显示在最新数据显示之后,我现在拥有的是这样的:

table of actual results

我希望名字后面的子总数。如何在计数的名称分组后添加小计? (在我的img中圈出来)

例如:table of wanted results

1 个答案:

答案 0 :(得分:0)

尝试此解决方案,直接浏览摘要表
使用Union和Group by使用LEFT功能的名称排序模式

    declare @mytable table ([name] varchar(10),custid int, cust_name varchar(10),custype varchar(5), Jan decimal(8,2), Feb decimal (8,2))

    insert into @mytable
    values 
    ('BoB',1,'AA','X1',1000,2000),
    ('BoB',2,'BB','X5',200,100),
    ('Carl',1,'AA','X1',2000,1000),
    ('Tim',2,'BB','X2',1000,2000),
    ('Tim',3,'BB','X3',2000,1000),
    ('Doom',4,'DD','X4',2000,1000)


declare @mytable table ([name] varchar(10),custid int, cust_name varchar(10),custype varchar(5), Jan decimal(8,2), Feb decimal (8,2))

insert into @mytable
values 
('BoB',1,'AA','X1',1000,2000),
('BoB',2,'BB','X5',200,100),
('Bonnie',2,'BB','X5',200,100),
('Carl',1,'AA','X1',2000,1000),
('Tim',2,'BB','X2',1000,2000),
('Tim',3,'BB','X3',2000,1000),
('Doom',4,'DD','X4',2000,1000)
select name,custid,cust_name,custype,jan,feb from (
   select *,'1' + name nameord from @mytable
   union 
   select 'SubTotal ' + name, null,cast(count(1) as varchar(2)),null,sum(jan) sumofJan, sum(feb) sumofFeb, '1' + name + 'xx'  from @mytable
   group by
   name
   union
   select 'Total ', null,null,null,sum(jan) sumofJan, sum(feb) sumofFeb, '3'  from @mytable
  ) x
  order by x.nameOrd

结果

name                custid      cust_name  custype jan                                     feb
------------------- ----------- ---------- ------- --------------------------------------- ---------------------------------------
BoB                 1           AA         X1      1000.00                                 2000.00
BoB                 2           BB         X5      200.00                                  100.00
SubTotal BoB        NULL        2          NULL    1200.00                                 2100.00
Carl                1           AA         X1      2000.00                                 1000.00
SubTotal Carl       NULL        1          NULL    2000.00                                 1000.00
Doom                4           DD         X4      2000.00                                 1000.00
SubTotal Doom       NULL        1          NULL    2000.00                                 1000.00
Tim                 2           BB         X2      1000.00                                 2000.00
Tim                 3           BB         X3      2000.00                                 1000.00
SubTotal Tim        NULL        2          NULL    3000.00                                 3000.00
Total               NULL        NULL       NULL    8200.00                                 7100.00
  

将策略排序为字母数字,其中详细信息以1 +名称开头   小计1 +名称+ xx和总计3或1之后的任何数字

nameord       name                custid      cust_name  custype Jan                                     Feb
------------- ------------------- ----------- ---------- ------- --------------------------------------- ---------------------------------------
1BoB          BoB                 1           AA         X1      1000.00                                 2000.00
1BoB          BoB                 2           BB         X5      200.00                                  100.00
1BoBxx        SubTotal BoB        NULL        2          NULL    1200.00                                 2100.00
1Carl         Carl                1           AA         X1      2000.00                                 1000.00
1Carlxx       SubTotal Carl       NULL        1          NULL    2000.00                                 1000.00
1Doom         Doom                4           DD         X4      2000.00                                 1000.00
1Doomxx       SubTotal Doom       NULL        1          NULL    2000.00                                 1000.00
1Tim          Tim                 2           BB         X2      1000.00                                 2000.00
1Tim          Tim                 3           BB         X3      2000.00                                 1000.00
1Timxx        SubTotal Tim        NULL        2          NULL    3000.00                                 3000.00
3             Total               NULL        NULL       NULL    8400.00                                 7200.00