我想创建一个存储过程,可以过滤日期范围。 我想显示分组,没有日期我的数据。在某些时期,SUM每个员工的前景如何访问。
现在我有了这个,但还不够好。我应该删除av.Date,但没有av.Date,我无法过滤我的日期范围!
Select emp.Name+' '+emp.Surname as TaskOwner,av.Date,et.Event,COUNT(av.EventTypeID) as something
from TableT av
left join Employee emp on av.CreatedBy = emp.EmployeeD
left join EventType et on av.EventTypeID = et.EventTypeID
Group By av.Date, et.Event, emp.Name+' '+emp.Surname
Order By TaskOwner desc
我当前的输出就像
John Doe 2016-03-02 PROPOSALS ACCEPTED 1
John Doe 2016-03-04 PROSPECTS VISITS 2
John Doe 2016-03-07 COMMERCIAL VISITS 1
John Doe 2016-03-08 PROPOSALS MADE 1
John Doe 2016-03-13 PROPOSALS ACCEPTED 1
Someone Else 2016-01-04 PROPOSALS MADE 1
Someone Else 2016-03-03 COMMERCIAL VISITS 1
Someone Else 2016-03-04 PROPOSALS ACCEPTED 1
Someone Else 2016-03-06 PROPOSALS ACCEPTED 1
我需要的是这样的事情:
John Doe PROPOSALS ACCEPTED 2
John Doe PROSPECTS VISITS 2
John Doe COMMERCIAL VISITS 1
John Doe PROPOSALS MADE 1
Someone Else PROPOSALS MADE 1
Someone Else COMMERCIAL VISITS 1
Someone Else PROPOSALS ACCEPTED 2
但是能够过滤日期范围。
答案 0 :(得分:1)
您可以尝试以下查询
Select
emp.Name+' '+emp.Surname as TaskOwner,
et.Event,
COUNT(av.EventTypeID) as something
from TableT av
left join Employee emp on av.CreatedBy = emp.EmployeeD
left join EventType et on av.EventTypeID = et.EventTypeID
Where
(ISNULL (@lowerdate) OR av.Date >= ISNULL(CAST(@lowerdate as datetime))
AND
(ISNULL (@higherdate ) OR av.Date <= CAST(@higherdate as datetime) )
-- this is how we filter, assuming you get two dates always and also taking care of NULL
Group By et.Event, emp.Name+' '+emp.Surname
Order By TaskOwner desc