max max - 7 months ago 21
SQL Question

How to count occurrences of a computed column in SQL?

In ms sql, I try to count occurence of a computed column .

With a normal classic, no worries:

SELECT ID, COUNT(*)
FROM User
GROUP BY ID


But with calculated column it display an Error

SELECT CONVERT(INT, (ID * PI())) AS TOTO, COUNT(*)
FROM User
GROUP BY TOTO


Do you know if there is a way to do it?

Answer

Use this...you want to group by the same computed expression to get the count grouped by that expression

SELECT CONVERT(INT, (ID * PI()))  AS TOTO,  COUNT(*) 
FROM User
GROUP BY CONVERT(INT, (ID * PI()))
Comments