-6

查询 Q 返回两列 -DateOfWork一个是 a datetimeCallsTakenOnDate一个是一个int

也就是说,我们得到特定电话交换机在给定日期接听的电话数量。

如何创建一个函数,该函数将CallsTakenOnDate在 n 天内返回特定电话交换的列总和?

表结构 -

DateOfWork  | CallsTakenOnDate
-------------------------------
2013-10-5   | 200
2013-10-6   | 100
2013-10-7   | 700        

等等

我想总结一下CallsTakenOnDate

4

2 回答 2

3

试试这个——

CREATE FUNCTION dbo.udf_getCountCallOnDate
(
      @DateFrom DATETIME
    , @DateTo DATETIME
)
RETURNS INT
AS
BEGIN

    RETURN (
        SELECT SUM(CallsTakenOnDate) 
        FROM dbo.PhoneCalls
        WHERE DateOfWork BETWEEN @DateFrom AND @DateTo
    )

END

SELECT 
  DateOfWork = CONVERT(VARCHAR(10), t.DateOfWork, 111)
, [Count] = SUM(t.CallsTakenOnDate) 
FROM dbo.PhoneCalls t
WHERE DateOfWork BETWEEN @DateFrom AND @DateTo
ORDER BY t.DateOfWork
于 2013-04-11T10:34:18.553 回答
0

我今天刚刚遇到了同样的问题:

select
  sum(CallsTakenOnDate) as count,
  DATE_FORMAT(DateOfWork, "%Y-%m-%d %H:00:00") AS date
from
  raw_visits
group by hour(DateOfWork),day(DateOfWork),month(DateOfWork),year(DateOfWork);

该组每天进行,但您可以通过组声明对其进行调整。

于 2013-04-11T10:27:54.803 回答