日期时间条款

时间:2015-06-16 22:00:00

标签: mysql sql sql-server

我正在运行此查询:

SELECT
    sm.UPDATE_DATE
FROM
    EAT_Tickets
    LEFT JOIN
        MAS_Users
        ON EAT_Tickets.tic_assignedTo = MAS_Users.use_userKey
    INNER JOIN
        EAT_TicketStatusesLK
        ON EAT_Tickets.tic_ticketStatusKey = EAT_TicketStatusesLK.tis_ticketStatusKey
    INNER JOIN
        [HSHPSQL03].[ServiceManager].dbo.[OCMLM1] sm
        ON sm.NUMBER = EAT_Tickets.tic_oarID
WHERE
        sm.UCD_Status <> 'Closed'
    AND sm.UCD_Status <> 'denied'
    AND sm.NUMBER = EAT_Tickets.tic_oarID
    AND sm.UCD_BRIEF_DESC NOT LIKE '%Access termination%'
    AND MAS_Users.use_ADLogin = 'User1'

它显然会返回日期时间,例如2015-06-15 06:48:12:000

但是,我想添加一些外壳,因为我想看看有多少是在3天内更新的。例如,如果我有5个结果,其中4个是在今天的3天之内,我希望结果集显示&#34; 4&#34;。我不需要看日期/时间。我希望套管比较3天内的日期并加上计数。这可能吗?

1 个答案:

答案 0 :(得分:1)

我认为这应该适合你。总和将在3天内累计任何日期。

SELECT
  SUM(CASE
    WHEN DATEDIFF(dd, sm.UPDATE_DATE, GETDATE()) <= 3 THEN 1
    ELSE 0
  END) 'DaysWithin3'
FROM EAT_Tickets
LEFT JOIN MAS_Users
  ON EAT_Tickets.tic_assignedTo = MAS_Users.use_userKey
INNER JOIN EAT_TicketStatusesLK
  ON EAT_Tickets.tic_ticketStatusKey = EAT_TicketStatusesLK.tis_ticketStatusKey
INNER JOIN [HSHPSQL03].[ServiceManager].dbo.[OCMLM1] sm
  ON sm.NUMBER = EAT_Tickets.tic_oarID
WHERE sm.UCD_Status <> 'Closed'
AND sm.UCD_Status <> 'denied'
AND sm.NUMBER = EAT_Tickets.tic_oarID
AND sm.UCD_BRIEF_DESC NOT LIKE '%Access termination%'
AND MAS_Users.use_ADLogin = 'User1'