最近当前周,每月和每年的总销售数量-SQL

时间:2018-07-16 15:39:04

标签: sql sql-server select

您如何按每周,每月和每年查找销售人员的总销售数量?

  name         WTD      MTD     YTD

SalesPersnA   2        5       10

例如:-

ID          SalesPerson    NAME                            TaskDate    TaskAge DocumentType                TaskStatus  OverdueCheck

2000378     Willy           Akron FNOL Supervisor Team 1    2015-02-04  1258    Claim.Reassigned.File.Text  Completed   WithinSLA
2000378     Amanda          Akron FNOL Supervisor Team 1    2015-02-04  1258    ClaimLifecycle.Open.RD.Text Completed   WithinSLA
2000388     Amanda          Akron FNOL Supervisor Team 1    2016-08-06  709     ClaimLifecycle.Open.RD.Text Completed   WithinSLA
2000388     Willy           Akron FNOL Supervisor Team 1    2016-08-06  709     Claim.Reassigned.File.Text  Completed   WithinSLA
2000388     Schutz          Akron FNOL Supervisor Team 1    2016-09-21  663     ISO.Failure.Diary.Text:     Completed   WithinSLA
2000388     Stephannie      Akron FNOL Supervisor Team 1    2016-09-26  658     Claim.Reassigned.File.Text  Completed   WithinSLA

样本数据

{{1}}

3 个答案:

答案 0 :(得分:1)

Count忽略了null,因此您可以在最后一个星期和一个月的count表达式顶部使用case调用。年份的条件可以在where子句中表达:

SELECT   SalesPerson,
         COUNT(CASE DATEDIFF(WEEK,  TaskDate, GETDATE()) = 0 THEN 1 END) AS WTD,
         COUNT(CASE DATEDIFF(MONTH, TaskDate, GETDATE()) = 0 THEN 1 END) AS MTD,
         COUNT(*)
FROM     mytable
WHERE    DATEDIFF(YEAR, TaskDate, GETDATE()) = 0
GROUP BY SalesPerson

答案 1 :(得分:0)

如果您希望获得以年,月,周为单位的结果,则可以使用窗口功能:

declare @test table (ID int, SalesPerson nvarchar(100), [Name] nvarchar(100), TaskDate date, TaskAge int, DocumentType nvarchar(max), TaskStatus nvarchar(20), OverdueCheck nvarchar(20))

insert into @test values
  (2000378, 'Willy'     , 'Akron FNOL Supervisor Team 1', '2015-02-04', 1258, 'Claim.Reassigned.File.Text ', 'Completed', 'WithinSLA')
, (2000378, 'Amanda'    , 'Akron FNOL Supervisor Team 1', '2015-02-04', 1258, 'ClaimLifecycle.Open.RD.Text', 'Completed', 'WithinSLA')
, (2000388, 'Amanda'    , 'Akron FNOL Supervisor Team 1', '2016-08-06', 709 , 'ClaimLifecycle.Open.RD.Text', 'Completed', 'WithinSLA')
, (2000388, 'Willy'     , 'Akron FNOL Supervisor Team 1', '2016-08-06', 709 , 'Claim.Reassigned.File.Text ', 'Completed', 'WithinSLA')
, (2000388, 'Schutz'    , 'Akron FNOL Supervisor Team 1', '2016-09-21', 663 , 'ISO.Failure.Diary.Text:    ', 'Completed', 'WithinSLA')
, (2000388, 'Stephannie', 'Akron FNOL Supervisor Team 1', '2016-09-26', 658 , 'Claim.Reassigned.File.Text ', 'Completed', 'WithinSLA')

select SalesPerson
    , Year(TaskDate) as [Year]
    , month(taskdate) as [Month]
    , DATEPART( wk, taskdate) as [Week]
    , count(*) over (Partition by salesperson, Year(taskdate)) as [YearCount]
    , count(*) over (Partition by salesperson, Year(taskdate), Month(taskdate)) as [MonthCount]
    , count(*) over (Partition by salesperson, Year(taskdate), DATEPART( wk, taskdate)) as [WeekCount]
from @test

答案 2 :(得分:0)

这对我有用!谢谢大家的建议和时间。

    select AdjusterName, NAME,
            sum(case when DATEPART( wk,TaskDate)  =  DATEPART ( wk, getdate()) and YEAR(taskDate) = 2018  then 1 else 0 end ) as WTD, 
            sum(case when DATEPART (m, TaskDate ) =  DATEPART ( m, getdate()) and YEAR(taskDate) = 2018  then 1 else 0 end ) as MTD, 
            sum(case when DATEPART (YEAR, TaskDate) =  DATEPART ( YEAR, getdate()) and YEAR(taskDate) = 2018  then 1 else 0 end ) as YTD

            from cte
            group by AdjusterName,NAME