Click here to Skip to main content
15,922,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
hi i have a table of students record,
i want to calculate number of students of a specific age , and also for increasing age.
the output will be like
Age No of students
7 12
8 17
9 4
. .
. .
. .
28 21


what will be the query for that, can anybody help e out?
Posted
Comments
[no name] 23-Jun-11 1:42am    
can you provide your table structure from where want to find this

SQL
SELECT
    Age,
    COUNT(StudentName) AS [No of Students]
FROM
    TMP
GROUP BY
    Age
ORDER BY
    Age


Please replace "Age" and "StudentName" with the column name(s) as per your table structure.
 
Share this answer
 
Comments
Db issues 23-Jun-11 2:30am    
Thanks your query is simple and effective
hello ,

if your structure is like
TABLE STUDENTS COLUMNS: StudentID (primary key), Name(varchar), Firstname(varchar), Dateofbirth(datetime)

then use this
SELECT FLOOR(DATEDIFF(day,Dateofbirth , GETDATE()) / 365.25) AS AGE, COUNT(*) AS [No of Student] FROM TblEmployee GROUP BY FLOOR(DATEDIFF(day, Dateofbirth, GETDATE()) / 365.25);

hoping it will work for you.

sanjeev
 
Share this answer
 
v2
For Calculating number of students of specified age the SQL Query is as :

SQL
Select SUM(No_Of_Students) From [table name] Where Age=[Your Specified Age]


And for Calculating number of students below the specified Age :

SQL
Select SUM(No_Of_Students) From [table Name] Where (Age=1 OR Age=2 OR Age=3 ... OR Age=[Your specified Age]


Thanks.
 
Share this answer
 
my database is of foll fields

st id
name
class
gender
do birth
do leaving school
fee paid (till to date)
group (science / arts)

i have to calculate age first and insert it into a column

than i have to find the age distributions


Thanks everybody for helping me out. Highly obliged
 
Share this answer
 
v2
that will make my problem more clear

let it be an employee mgmt system

i have
EID (PK)
Date of birth (datetime)
date of joining (datetime)
date of retirement(datetime)
date of death (date time)


i do not have to calculate age till today but i want to calculate age till date of retirement or date of death(if available)

i want to declare a new column "age" and keep the records of every employee in it

i have a query working for it
SQL
select case when not death is null then
datediff(year, birth, death ) 
else datediff(year , birth, date_of_retirement ) end from Table_3


it works ok, but i want to save the results in "age"
 
Share this answer
 
better if you share your table structure whatever, please try this qry

Select Age, Count(StudentName)
from table1
Group by Age


thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900