Groups | Blog | Home
all groups > sql server programming > november 2003 >

sql server programming : excel like statistics


Steve Kass
11/9/2003 4:44:09 PM
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]
Ray Higdon
11/9/2003 4:47:10 PM
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
11/9/2003 10:51:28 PM
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

Pavel S.Vorontsov
11/10/2003 10:28:08 AM
Hi!

set nocount on
declare @age table (id int not null identity(1,1) primary key,
age int not null)
declare @age_groups table (id int not null identity(1,1) primary key,
age_from int not null,
age_to int not null)

insert into @age_groups (age_from, age_to)
select 0,5 union all
select 6,15 union all
select 16,32 union all
select 33,60 union all
select 61,72

declare @i int
set @i = 0
while @i < 1000
begin
insert into @age (age)
select cast(rand()*100 as int)
set @i = @i + 1
end

select g.id, g.age_from, g.age_to, count(a.id)
from @age a inner join @age_groups g on
a.age between g.age_from and g.age_to
group by g.id, g.age_from, g.age_to

Regards,
Pavel S.Vorontsov
[quoted text, click to view]

AddThis Social Bookmark Button