Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 568
max Avatar asked Apr 28 '16 13:04

max


People also ask

How do I count number of occurrences in SQL?

COUNT(ID) as NumberOfOccurance:- Counting the number of occurrence of a ID. group by – It is necessary in aggregate function like 'count()' otherwise it will give error. having (COUNT(ID)>1) -Select those rows which count of ID value is greater than 1, that's why it shows '0' and '1'.


1 Answers

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()))
like image 194
cableload Avatar answered Nov 15 '22 01:11

cableload