Board index » delphi » Need SQL Statement

Need SQL Statement

Is SQL Statement able to provide me the follwing information?

Age    Count
1 -10    7
11-20   10
21 -23   15

where i only have the DOB in my table

can anyone help please.
thank you

 

Re:Need SQL Statement


This works on MS SQL 2000 (Northwind database; "employees" table):

select
count(*),
case
     when viewage.age between 30 and 39 then '30-39'
     when viewage.age between 40 and 49 then '40-49'
     when viewage.age between 50 and 59 then '50-59'
     when viewage.age between 60 and 69 then '60-69'
end
as agegroup
from
(
select round((cast((getdate() - birthdate) as float)/365), 0, 1) as age
from employees
)
as viewage

group by
case
     when viewage.age between 30 and 39 then '30-39'
     when viewage.age between 40 and 49 then '40-49'
     when viewage.age between 50 and 59 then '50-59'
     when viewage.age between 60 and 69 then '60-69'
end

This case syntax is native to MSSQL. You would need something different on
another RDBMS (Oracle, Interbase, etc.).

--

Alain Quesnel
cinqsanss...@compuserve.com

Quote
"Coann" <t...@pc.jaring.my> wrote in message news:3a7eeb28_1@dnews...
> Is SQL Statement able to provide me the follwing information?

> Age    Count
> 1 -10    7
> 11-20   10
> 21 -23   15

> where i only have the DOB in my table

> can anyone help please.
> thank you

Re:Need SQL Statement


Quote
Alain Quesnel <cinqsanss...@compuserve.com> wrote in message

news:3a7f5c23$1_1@dnews...
Quote
> select round((cast((getdate() - birthdate) as float)/365), 0, 1) as age

Will the MSSQL round function takes cares of the leap year?

Quote
> This case syntax is native to MSSQL. You would need something different on
> another RDBMS (Oracle, Interbase, etc.).

Anyway, this is the version for Oracle:
Select (age * 10)||' - '||(age * 10 + 9) as AgeGroup, count(*) from
(select trunc(Months_Between(trunc(sysdate), trunc(DOB)) / 120) as age from
table1)
group by age

Ping Kam

Other Threads