Marwan,
Here's a script to give you some ideas on one way to
do this kind of thing:
create table RangeChoices (
rangeSetID int not null,
intervalName varchar(20) null,
intervalStart decimal(10,3) not null,
includeStart int not null default 1,
primary key (rangeSetID, includeStart, intervalStart)
)
insert into RangeChoices values (1,null,0,1)
insert into RangeChoices values (1,null,10,0)
insert into RangeChoices values (1,null,25,0)
insert into RangeChoices values (1,null,500,0)
insert into RangeChoices values (2,'Low_1',0,1)
insert into RangeChoices values (2,'Mid_1',25,1)
insert into RangeChoices values (2,'Top_1',10000,0)
go
-- provide standard interval names if not provided
update RangeChoices set
intervalName =
case when includeStart = 0 then '> ' else '>= ' end +
cast(intervalStart as varchar(20))
where intervalName is null
go
-- test data
create table testVals (
f float not null
)
go
insert into testVals
select 0 union all select 1.5 union all select 7.6 union all
select 8.3 union all select 150.5 union all select 25
union all select 1000
go
-- identify which interval each datum is in, using
-- a specified set of intervals
create function valsIntervals (
@rangeSetID int
) returns table as return
select f, intervalName
from testVals, RangeChoices R1
where R1.intervalStart = (
select max(R2.intervalStart)
from RangeChoices R2
where (
R2.intervalStart < testVals.f
or (R2.intervalStart = testVals.f
and R2.includeStart = 1)
) and R2.rangeSetID = @rangeSetID
)
and R1.rangeSetID = @rangeSetID
go
select f, intervalName from valsIntervals(1) order by f
select f, intervalName from valsIntervals(2) order by f
select intervalName, count(f) as valCount
from valsIntervals(1)
group by intervalName
order by min(f)
select intervalName, count(f) as valCount
from valsIntervals(2)
group by intervalName
order by min(f)
go
drop table RangeChoices
drop table testVals
drop function valsIntervals
-- Steve Kass
-- Drew University
-- Ref: 4F8C88C3-C0D5-4085-B39C-9B68FF4D49AF
[quoted text, click to view] marwan hefnawy wrote:
>Suppose we have two tables, col_age in table1 represens the age in years,
>and col_bin in table 2 represents some age groups (0,5,15,32,60,72) say.
>How can I tell how many records in table1.col_age has values between 0 and
>5, and how many ages are between 5 and 15, and so on (according to the
>values in table2.col_bin)
>Thanks in advance
>
>
>
>
Look up cross-tab queries in BOL or pivot tables if you need more
functionality/grouping.
Try this
create table table1 (name varchar(20),age int)
insert into table1 select 'Joe', 50 union all
select 'Bob',12 union all
select 'Fred', 22 union all
select 'Ann', 24 union all
select 'Pam', 5 union all
select 'Ana', 89 union all
select 'Bob',1 union all
select 'Fred', 34 union all
select 'Ann', 65 union all
select 'Pam', 15 union all
select 'Ana', 59
select
sum(case when t.age < 5 then 1 else 0 end) as under5,
sum(case when t.age between 5 and 15 then 1 else 0 end) as '5to14',
sum(case when t.age >= 15 then 1 else 0 end) as Over15
from table1 as t
HTH
--
Ray Higdon MCSE, MCDBA, CCNA
---
[quoted text, click to view] "marwan hefnawy" <marwan_hefnawy@hotmail.com> wrote in message
news:%23MRh2MwpDHA.2000@TK2MSFTNGP12.phx.gbl...
> Suppose we have two tables, col_age in table1 represens the age in years,
> and col_bin in table 2 represents some age groups (0,5,15,32,60,72) say.
> How can I tell how many records in table1.col_age has values between 0 and
> 5, and how many ages are between 5 and 15, and so on (according to the
> values in table2.col_bin)
> Thanks in advance
>
>