views:

237

answers:

5

How can I group by Time? I tried this, but it gives the error "Invalid column name 'Time'.":

select Count(Page) as VisitingCount, CONVERT(VARCHAR(5), Date, 108) as [Time]
from scr_SecuristLog   
where Date between '2009-05-04 00:00:00' and '2009-05-06 14:58'  
and [user] in (select USERNAME from scr_CustomerAuthorities)  
group by [Time] order by [VisitingCount] asc
+8  A: 

Try

GROUP BY CONVERT(VARCHAR(5),Date, 108)

Always make sure you group by everything in your select clause that does not have an aggregate function around it.

Eppz
You're missing a closing brace
Stefan Steinegger
Eppz
+1  A: 
select Count(Page) as VisitingCount,CONVERT(VARCHAR(5),Date, 108) as [Time] from scr_SecuristLog   
where Date between '2009-05-04 00:00:00' and '2009-05-06 14:58'  
and [user] in(select USERNAME             
    from scr_CustomerAuthorities )  
group by CONVERT(VARCHAR(5),Date, 108) order by [VisitingCount] asc

I modified the GROUP BY to include the actual expression for [Time], rather than the column alias (since this can't be used in the GROUP BY, only the ORDER BY)

Adam Robinson
thanks alot!!! you are correct
Phsika
+6  A: 

[Time] is a column alias. Try

SELECT 
      COUNT(Page) AS VisitingCount
    , CONVERT(VARCHAR(5),Date, 108) AS [Time] 
FROM
    scr_SecuristLog   
WHERE
    Date BETWEEN '2009-05-04 00:00:00' AND '2009-05-06 14:58'  
    AND
    [user] IN (
       SELECT 
        USERNAME             
       FROM
        scr_CustomerAuthorities 
       )  
GROUP BY
    CONVERT(VARCHAR(5),Date, 108) 
ORDER BY
    [VisitingCount] ASC
Edward
I need to learn to type faster...
Edward
you are correct! you are ok
Phsika
Guess I need to learn to type slower if identical answers that come later are going to be the ones that get accepted ;)
Adam Robinson
I second this answer. Basically, you have to group by the same expression you use in the SELECT list. The alias won't do.
Rich.Carpenter
Simply put, you cannot group by [Time] because [Time] does not exists at the "group by" level.
T Pops
A: 

It looks like Date is a column here, but it's a keyword and not quoted. Perhaps that's the issue (not tested though):

select Count(Page) as VisitingCount,CONVERT(VARCHAR(5),[Date], 108) as [Time] from scr_SecuristLog   
where [Date] between '2009-05-04 00:00:00' and '2009-05-06 14:58'  
and [user] in(select USERNAME             
    from scr_CustomerAuthorities )  
group by [Time] order by [VisitingCount] asc
Tom Slee
A: 

If you don't want to repeat the date conversion (Sometimes calculation is a bit more intensive than simple conversion), then you can use something like this:

select * 
from ( select Count(page) .., ... As [Date] from ... where ...) UG
group by UG.[Date]

Notice that you have to give the 'UG' name to internal select and that this will be most likely and in most cases less efficient than repeating the conversion expression in original grouping. Also your whole expression is likely to change... But so just you know that it is possible.

Pasi Savolainen