Question

If it is possible, can we create a range for the age and show the count for the same?

Let's say, the range is between 22 and 25 count: 3, range between 26 and 30 count : 5.

Below is the persons table for which I am trying to do this:

Age: 26 to 27, comments " post graduate"
Age: 28 to 30, comments "working and single"
Age: 31 to 33, comments " middle level manager and married"

Table name: persons

personid  lastname  firstname   age comments
1         Cardinal  Tom         22
2         prabhu    priya       33
3         bhandari  abhijeet    24
4         Harry     Bob         25
5         krishna   anand       29
6         hari      hara        31
7         ram       hara        27
8         kulkarni  manoj       35
9         joshi     santosh     28
Was it helpful?

Solution

SELECT COUNT(CASE WHEN AGE >= 26 AND AGE <= 27 THEN 1 ELSE NULL END) AS [total26To27]
      ,  COUNT(CASE WHEN AGE >= 28 AND AGE <= 30 THEN 1 ELSE NULL END) AS [total28T30]
      ,  COUNT(CASE WHEN AGE >= 31 AND AGE <= 33 THEN 1 ELSE NULL END) AS [total31T33]
FROM Persons
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top