我有一个佣金报告功能,可以抓取员工的所有每日销售信息。 fCommissionINFO
我想要做的是根据输出获取不同的信息并创建佣金报告。例如,该功能可以获取总客户数,特定销售额的描述。我想做的是计算总销售数量,特定销售总数以及特定销售额的百分比。所以例如...如果“Sara”总销售额为“100”......在这100个销售中,她有3个服务“x”(总计3%),5个服务Y(5%),6个服务“z”(6%)等...
我最初的2个存储函数调用是:
Select UserName, SUM(CarCount) AS Cars
FROM dbo.fCommissionINFO(@startdate, @endate, 'ALL', 'ALL', '1', 'ALL', 'Combined', '0') AS fCommissionINFO_1
WHERE (Services > 0)
GROUP BY UserName
和
SELECT COUNT(Description) AS [service x]
FROM dbo.fCommissioninfo(@startdate, @endate, 'ALL', 'ALL', '1', 'ALL', 'Combined', '0') AS fCommissionINFO_1
WHERE (Description = 'service x')
如何组合这些,并将第三个与用户名组合起来?
答案 0 :(得分:1)
您需要此查询
SELECT UserName,
(SUM(COUNT(CASE WHEN Description = 'service x' THEN 1 END)) OVER() * 100) /
SUM(CASE WHEN [Services] > 0 THEN CarCount END) AS percentage
FROM dbo.fCommissioninfo(@startdate, @endate, 'ALL', 'ALL', '1', 'ALL', 'Combined', '0') AS fCommissionINFO_1
GROUP BY UserName
上的简单示例
答案 1 :(得分:0)
Select UserName, SUM(CarCount) AS Cars, COUNT(Description) AS [service x]
FROM dbo.fCommissionINFO(@startdate, @endate, 'ALL', 'ALL', '1', 'ALL', 'Combined', '0') AS fCommissionINFO_1
WHERE (Services > 0)
GROUP BY UserName, Description
答案 2 :(得分:0)
我相信以下应该可以解决问题。请注意将where子句提升到计数内部。这是因为如果有一行服务< = 0并且'service x'it的描述将被顶部查询排除并包含在底部查询中。因此,通过简单地梳理查询,您可以根据数据获得不同的结果。
Select UserName,
SUM(CASE WHEN Services > 0 THEN CarCount ELSE 0 END) AS Cars,
COUNT(CASE Description = 'service x' THEN 1 ELSE 0 END)
FROM dbo.fCommissionINFO(@startdate, @endate, 'ALL', 'ALL', '1', 'ALL', 'Combined', '0') AS fCommissionINFO_1
GROUP BY UserName