使用count distinct时mssql无效的列名

时间:2015-05-06 23:23:13

标签: sql sql-server

我很难过......当我将count distinct和group by语句添加到我的查询中时,我收到一条错误,告诉我CallTime和Ticketday是无效的列名。如何在不必先将这些数据写入表中然后查询它的情况下完成此操作?

select datename(weekday, calllog.RecvdDate) as Ticketday,
CASE WHEN calllog.RecvdTime between '00:00:00' and '01:00:00' THEN '00:00 to 01:00' 
    WHEN calllog.RecvdTime between '01:00:00' and '02:00:00' THEN '01:00 to 02:00' 
    WHEN calllog.RecvdTime between '02:00:00' and '03:00:00' THEN '02:00 to 03:00'
    WHEN calllog.RecvdTime between '03:00:00' and '04:00:00' THEN '03:00 to 04:00'
    WHEN calllog.RecvdTime between '04:00:00' and '05:00:00' THEN '04:00 to 05:00'
    WHEN calllog.RecvdTime between '05:00:00' and '06:00:00' THEN '05:00 to 06:00'
    WHEN calllog.RecvdTime between '06:00:00' and '07:00:00' THEN '06:00 to 07:00'
    WHEN calllog.RecvdTime between '07:00:00' and '08:00:00' THEN '07:00 to 08:00'
    WHEN calllog.RecvdTime between '08:00:00' and '09:00:00' THEN '08:00 to 09:00'
    WHEN calllog.RecvdTime between '09:00:00' and '10:00:00' THEN '09:00 to 10:00'
    WHEN calllog.RecvdTime between '10:00:00' and '11:00:00' THEN '10:00 to 11:00'
    WHEN calllog.RecvdTime between '11:00:00' and '12:00:00' THEN '11:00 to 12:00'
    WHEN calllog.RecvdTime between '12:00:00' and '13:00:00' THEN '12:00 to 13:00'
    WHEN calllog.RecvdTime between '13:00:00' and '14:00:00' THEN '13:00 to 14:00'
    WHEN calllog.RecvdTime between '14:00:00' and '15:00:00' THEN '14:00 to 15:00'
    WHEN calllog.RecvdTime between '15:00:00' and '16:00:00' THEN '15:00 to 16:00'
    WHEN calllog.RecvdTime between '16:00:00' and '17:00:00' THEN '16:00 to 17:00'
    WHEN calllog.RecvdTime between '17:00:00' and '18:00:00' THEN '17:00 to 18:00'
    WHEN calllog.RecvdTime between '18:00:00' and '19:00:00' THEN '18:00 to 19:00'
    WHEN calllog.RecvdTime between '19:00:00' and '20:00:00' THEN '19:00 to 20:00'
    WHEN calllog.RecvdTime between '20:00:00' and '21:00:00' THEN '20:00 to 21:00'
    WHEN calllog.RecvdTime between '21:00:00' and '22:00:00' THEN '21:00 to 22:00'
    WHEN calllog.RecvdTime between '22:00:00' and '23:00:00' THEN '22:00 to 23:00'
    WHEN calllog.RecvdTime between '23:00:00' and '23:59:59' THEN '23:00 to 00:00'
    END as CallTime, count(distinct(CallTime)) as CallCount
from SERVER.xxxx.dbo.view_calllog calllog
where calllog.RecvdDate > '2015-01-01'
and calllog.NameRecvdBy = 'IVR User Account'
and calllog.CallDesc LIKE '%string%'
group by Ticketday, CallTime
order by Ticketday, CallTime

2 个答案:

答案 0 :(得分:1)

问题在于,在解析查询时,当您尝试在select语句中使用别名时,尚未定义别名CallTime

一种解决方案是将查询包装在公共表表达式中(或将其用作派生表),如下所示:

;WITH cte (Ticketday, CallTime) AS (
    SELECT 
       DATENAME(weekday, calllog.RecvdDate) AS Ticketday,
       CASE 
          WHEN calllog.RecvdTime BETWEEN '00:00:00' AND '01:00:00' THEN '00:00 to 01:00' 
          WHEN calllog.RecvdTime BETWEEN '01:00:00' AND '02:00:00' THEN '01:00 to 02:00' 
          WHEN calllog.RecvdTime BETWEEN '02:00:00' AND '03:00:00' THEN '02:00 to 03:00'
          WHEN calllog.RecvdTime BETWEEN '03:00:00' AND '04:00:00' THEN '03:00 to 04:00'
          WHEN calllog.RecvdTime BETWEEN '04:00:00' AND '05:00:00' THEN '04:00 to 05:00'
          WHEN calllog.RecvdTime BETWEEN '05:00:00' AND '06:00:00' THEN '05:00 to 06:00'
          WHEN calllog.RecvdTime BETWEEN '06:00:00' AND '07:00:00' THEN '06:00 to 07:00'
          WHEN calllog.RecvdTime BETWEEN '07:00:00' AND '08:00:00' THEN '07:00 to 08:00'
          WHEN calllog.RecvdTime BETWEEN '08:00:00' AND '09:00:00' THEN '08:00 to 09:00'
          WHEN calllog.RecvdTime BETWEEN '09:00:00' AND '10:00:00' THEN '09:00 to 10:00'
          WHEN calllog.RecvdTime BETWEEN '10:00:00' AND '11:00:00' THEN '10:00 to 11:00'
          WHEN calllog.RecvdTime BETWEEN '11:00:00' AND '12:00:00' THEN '11:00 to 12:00'
          WHEN calllog.RecvdTime BETWEEN '12:00:00' AND '13:00:00' THEN '12:00 to 13:00'
          WHEN calllog.RecvdTime BETWEEN '13:00:00' AND '14:00:00' THEN '13:00 to 14:00'
          WHEN calllog.RecvdTime BETWEEN '14:00:00' AND '15:00:00' THEN '14:00 to 15:00'
          WHEN calllog.RecvdTime BETWEEN '15:00:00' AND '16:00:00' THEN '15:00 to 16:00'
          WHEN calllog.RecvdTime BETWEEN '16:00:00' AND '17:00:00' THEN '16:00 to 17:00'
          WHEN calllog.RecvdTime BETWEEN '17:00:00' AND '18:00:00' THEN '17:00 to 18:00'
          WHEN calllog.RecvdTime BETWEEN '18:00:00' AND '19:00:00' THEN '18:00 to 19:00'
          WHEN calllog.RecvdTime BETWEEN '19:00:00' AND '20:00:00' THEN '19:00 to 20:00'
          WHEN calllog.RecvdTime BETWEEN '20:00:00' AND '21:00:00' THEN '20:00 to 21:00'
          WHEN calllog.RecvdTime BETWEEN '21:00:00' AND '22:00:00' THEN '21:00 to 22:00'
          WHEN calllog.RecvdTime BETWEEN '22:00:00' AND '23:00:00' THEN '22:00 to 23:00'
          WHEN calllog.RecvdTime BETWEEN '23:00:00' AND '23:59:59' THEN '23:00 to 00:00'
       END AS CallTime
    FROM SERVER.xxxx.dbo.view_calllog calllog
    WHERE calllog.RecvdDate > '2015-01-01'
    AND calllog.NameRecvdBy = 'IVR User Account'
    AND calllog.CallDesc LIKE '%string%'
)
SELECT Ticketday, COUNT(DISTINCT CallTime) AS CallCount 
FROM cte
GROUP BY Ticketday, CallTime
ORDER BY Ticketday, CallTime

此外,distinct不是函数。正确的语法是count(distinct CallTime)

答案 1 :(得分:0)

您不能将列别名放在您的组中;但是,你可以将表达式放在你的组中。在这种情况下,虽然看起来很丑陋。类似的东西:

select datename(weekday, calllog.RecvdDate) as Ticketday,
    <<BIG CASE STATEMENT HERE>> as CallTime, 
    count(distinct(CallTime)) as CallCount
from SERVER.xxxx.dbo.view_calllog calllog
where calllog.RecvdDate > '2015-01-01'
    and calllog.NameRecvdBy = 'IVR User Account'
    and calllog.CallDesc LIKE '%string%'
group by datename(weekday, calllog.RecvdDate), <<BIG CASE STATEMENT HERE>>
order by Ticketday, CallTime

另一种方法是使用子查询,这样你就不必两次查看那个大案例陈述。 :)

select Ticketday, 
    CallTime, 
    count(distinct(CallTime)) as CallCount
from
    (
    select datename(weekday, calllog.RecvdDate) as Ticketday,
    CASE WHEN calllog.RecvdTime between '00:00:00' and '01:00:00' THEN '00:00 to 01:00' 
        WHEN calllog.RecvdTime between '01:00:00' and '02:00:00' THEN '01:00 to 02:00' 
        WHEN calllog.RecvdTime between '02:00:00' and '03:00:00' THEN '02:00 to 03:00'
        WHEN calllog.RecvdTime between '03:00:00' and '04:00:00' THEN '03:00 to 04:00'
        WHEN calllog.RecvdTime between '04:00:00' and '05:00:00' THEN '04:00 to 05:00'
        WHEN calllog.RecvdTime between '05:00:00' and '06:00:00' THEN '05:00 to 06:00'
        WHEN calllog.RecvdTime between '06:00:00' and '07:00:00' THEN '06:00 to 07:00'
        WHEN calllog.RecvdTime between '07:00:00' and '08:00:00' THEN '07:00 to 08:00'
        WHEN calllog.RecvdTime between '08:00:00' and '09:00:00' THEN '08:00 to 09:00'
        WHEN calllog.RecvdTime between '09:00:00' and '10:00:00' THEN '09:00 to 10:00'
        WHEN calllog.RecvdTime between '10:00:00' and '11:00:00' THEN '10:00 to 11:00'
        WHEN calllog.RecvdTime between '11:00:00' and '12:00:00' THEN '11:00 to 12:00'
        WHEN calllog.RecvdTime between '12:00:00' and '13:00:00' THEN '12:00 to 13:00'
        WHEN calllog.RecvdTime between '13:00:00' and '14:00:00' THEN '13:00 to 14:00'
        WHEN calllog.RecvdTime between '14:00:00' and '15:00:00' THEN '14:00 to 15:00'
        WHEN calllog.RecvdTime between '15:00:00' and '16:00:00' THEN '15:00 to 16:00'
        WHEN calllog.RecvdTime between '16:00:00' and '17:00:00' THEN '16:00 to 17:00'
        WHEN calllog.RecvdTime between '17:00:00' and '18:00:00' THEN '17:00 to 18:00'
        WHEN calllog.RecvdTime between '18:00:00' and '19:00:00' THEN '18:00 to 19:00'
        WHEN calllog.RecvdTime between '19:00:00' and '20:00:00' THEN '19:00 to 20:00'
        WHEN calllog.RecvdTime between '20:00:00' and '21:00:00' THEN '20:00 to 21:00'
        WHEN calllog.RecvdTime between '21:00:00' and '22:00:00' THEN '21:00 to 22:00'
        WHEN calllog.RecvdTime between '22:00:00' and '23:00:00' THEN '22:00 to 23:00'
        WHEN calllog.RecvdTime between '23:00:00' and '23:59:59' THEN '23:00 to 00:00'
        END as CallTime
    from SERVER.xxxx.dbo.view_calllog calllog
    where calllog.RecvdDate > '2015-01-01'
    and calllog.NameRecvdBy = 'IVR User Account'
    and calllog.CallDesc LIKE '%string%'
    ) CallData
group by Ticketday, CallTime
order by Ticketday, CallTime