我知道我的问题似乎很熟悉,但我坚持这一点。我试图按组来计算,并且我有这样的查询:添加分组后计数小计
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),
()
);
但是我得到的结果是错误的。子总显示最新的数据显示,之后,和我有什么现在的问题是这样的:
而且我想下面的名字后,小计。如何在计数按名称分组后添加小计? (在我的IMG圈子)
附加联合用于总计然后添加一个排序计算值..详细开始可以将您的脚本与详细信息分开,脚本与单个总计以及总计脚本相关联......然后UNION – maSTAShuFu
您使用的是哪种数据库客户端? – SandPiper
@SandPiper sql server bro –