title: Invalid column name when using count distinct
body: <p>I'm stumped... When I added the count distinct and group by statements to my query I get an error telling me that CallTime and Ticketday are invalid column names. How can I accomplish this without having to write this data to a table first and then query it?</p>
  
  <pre><code>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 &gt; '2015-01-01'
  and calllog.NameRecvdBy = 'IVR User Account'
  and calllog.CallDesc LIKE '%string%'
  group by Ticketday, CallTime
  order by Ticketday, CallTime
  </code></pre>