我有一个查询,我一直用来跟踪用户的销售情况。以前他们每个月都需要配额。现在,他们想改变规则,允许他们在任何一个月开始,所以他们可能会从6月到6月或其他什么。他们还希望用户在错过一个月后立即重新开始。这似乎是一个更公平的系统,因为如果他们没有在3月份制定配额,那么他们就无法统计他们在那一个月之后所做的全年。这确实弄乱了我的查询,但我不知道如何修复它。有人有解决方案吗?
这是现有的t-sql。
@Year int
AS
BEGIN
DECLARE @DateStart datetime
DECLARE @DateStop datetime
SELECT @DateStart = CONVERT(DATETIME, CONVERT( char(4), @Year) + '-01- 01')
SELECT @DateStop = CONVERT(DATETIME, CONVERT( char(4), @Year + 1) + '-01- 01')
SET NOCOUNT ON;
SELECT r.riderid,
r.dname,
DATEPART(yyyy, m.ridedate),
SUM(CASE DATEPART(mm, m.datesale) WHEN 1 THEN m.quota ELSE 0 END) AS [jan],
SUM(CASE DATEPART(mm, m.datesale) WHEN 2 THEN m.quota ELSE 0 END) AS [feb],
SUM(CASE DATEPART(mm, m.datesale) WHEN 3 THEN m.quota ELSE 0 END) AS [mar],
SUM(CASE DATEPART(mm, m.datesale) WHEN 4 THEN m.quota ELSE 0 END) AS [apr],
SUM(CASE DATEPART(mm, m.datesale) WHEN 5 THEN m.quota ELSE 0 END) AS [may],
SUM(CASE DATEPART(mm, m.datesale) WHEN 6 THEN m.quota ELSE 0 END) AS [jun],
SUM(CASE DATEPART(mm, m.datesale) WHEN 7 THEN m.quota ELSE 0 END) AS [jul],
SUM(CASE DATEPART(mm, m.datesale) WHEN 8 THEN m.quota ELSE 0 END) AS [aug],
SUM(CASE DATEPART(mm, m.datesale) WHEN 9 THEN m.quota ELSE 0 END) AS [sep],
SUM(CASE DATEPART(mm, m.datesale) WHEN 10 THEN m.quota ELSE 0 END) AS [oct],
SUM(CASE DATEPART(mm, m.datesale) WHEN 11 THEN m.quota ELSE 0 END) AS [nov],
SUM(CASE DATEPART(mm, m.datesale) WHEN 12 THEN m.quota ELSE 0 END) AS [dec],
SUM(m.quota) as [tot]
FROM users u
JOIN mysales m
ON m.riderid = u.riderid
Where m.datesale Between @DateStart AND @DateStop
GROUP BY DATEPART(yyyy, m.datesale), u.userid
ORDER BY DATEPART(yyyy, m.datesale), SUM(m.quota) DESC
END
好的 - 有数据 该表包含用户ID,客户ID,销售量和销售日期 查询以月为单位提取用户的销售总额。用户250在2016年7月制定了配额,但是没有在8月份,所以他应该在7月和9月份进入#quota,但因为他没有在8月份,他必须在9月重新启动;用户#300已经从2016年1月到9月份制定了配额,所以只要他完成12个月,他就有资格获得奖金。用户350已成功完成年度并应获得奖金。此时生产中没有配额表,会简化吗?我需要的是正在运行的用户列表。
--drop table #sales
--drop table #quota
create table #sales
(
--saleid int --PK
userid int -- salesperson FK
, customerid int --FK
, sale_amt decimal
, date_sale datetime
)
insert into #sales values
(300,1301,542.90,'3-2-2016'),
(300,1301,782.70,'3-4-2016'),
(300,1541,600.70,'3-7-2016'),
(300,903,640.71,'3-10-2016'),
(300,1745,900.01,'3-29-2016'),
(300,1440,2040.71,'2-10-2016'),
(300,903,640.71,'2-20-2016'),
(300,414,1489.00,'1-18-2016'),
(300,1645,1322.00,'1-20-2016'),
(300,1200,1156.09,'4-2-2016'),
(300,1204,1456.00,'4-20-2016'),
(250,1140,156.89,'4-12-2016'),
(250,1240,1176.69,'4-14-2016'),
(250,840,480.61,'4-17-2016'),
(250,1940,500.71,'5-17-2016'),
(250,1425,4800.61,'6-1-2016'),
(250,1840,701.32,'6-15-2016'),
(250,1840,701.32,'7-15-2016'),
(250,1840, 2701.32,'8-15-2016'),
(450,8421,2500.61,'7-17-2015'),
(450,8422,2500.1,'8-17-2015'),
(450,843,2500.1,'9-17-2015'),
(450,8431,2500.00,'10-17-2015'),
(450,1431,2500.00,'11-17-2015'),
(450,4311,2500.00,'12-17-2015'),
(450,4310,2500.00,'1-17-2016'),
(450,1310,2500.00,'2-17-2016'),
(450,1310,2500.00,'3-17-2016'),
(450,130,2500.00,'4-17-2016'),
(450,1130,2500.00,'5-17-2016'),
(450,113,2500.00,'6-17-2016')
Select userid
, sum(sale_amt) Sale
, DATEPART(mm,date_sale) as[month]
from #sales
group by userid, DATEPART(mm,date_sale) order by userid
create table #quota
(
qid int --PK
, userid int -- salesperson FK
, quota bit -- awarded when sales => $2500.00
, datesale datetime -- date quota made
)
答案 0 :(得分:0)
只有一种可能的方法来编写一个查询@running_months
个月的查询,以验证在每个用户的窗口期间没有错过任何配额:
select userid from users u
where not exists (
select 1 from #sales s
where s.userid = u.userid
and date_sale > dateadd(month, -@running_months - 1, current_timestamp)
and datediff(month, sales_date, current_timestamp) between 1 and @running_months
group by month(sales_amt)
having sum(sales_amt) < 2500
)
编辑:我后来意识到您可能确实在一个月内没有销售用户,因此您可能需要实际验证所有月份都超过配额的条件而不是没有这些月份都在配额范围内:
select userid from users u
where userid in (
select userid from
(
select userid from #sales s
where s.userid = u.userid
and date_sale > dateadd(month, -@running_months - 1, current_timestamp)
and datediff(month, sales_date, current_timestamp) between 1 and @running_months
group by month(sales_amt)
having sum(sales_amt) >= 2500
) q
group by userid
having count(*) = @running_months
)