Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Number range query

Posted on 2010-09-01
8
Medium Priority
?
377 Views
Last Modified: 2013-11-10
Hello All,

I need to sum the people based on the min and max age passed and the status (A=Active and B=Non Active) for the care center. Sum all the people fall under those min and max age limit and count the ages which made this sum and exclude the once not in the provided age range. User will be passing the min and max age.

The table looks something like this-

Age    Count      Status
65      30            A
66      12            A
86        8            B
70      10            A
82        9            B
61      28            A
77      30            A
76      12            A
73        8            B

If the user passes minage = 65 and maxage = 82 Then the result should look like
Output should look like this-
Active.AgeCnt       TotalPeople     InActive.AgeCnt         TotalPeople    
          5                          94                          2                        17

Can someone help me with this?

Thanks,
-B
0
Comment
Question by:BrookK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 11

Expert Comment

by:Larissa T
ID: 33578399
Try this
create table #tmp (Age int,  cnt int,      Status char(1))
insert into #tmp
select  65  ,    30,'A'
union select 66,      12,  'A'
union select 86,        8   ,'B'
union select 70 ,     10, 'A'
union select 82 ,       9,'B'
union select 61 ,     28, 'A'
union select 77 ,     30,  'A'
union select 76 ,     12,'A'
union select 73 ,       8,'B'

select sum(case when age between 65 and 82 then 1 else 0 end)
,sum(cnt)
, sum(case when Status='b'  then 1 else 0 end) inactiveAgeCnt
, sum(case when Status='a'  then cnt else 0 end) active
, sum(case when Status='b'  then cnt else 0 end) inactivePeopel
from #tmp
0
 
LVL 25

Accepted Solution

by:
jrb1 earned 1500 total points
ID: 33578978
How about:

select sum(case when age between 65 and 82 and status = 'A' then 1 else 0 end) as Active_Age_Cnt
,sum(case when age bewteen 65 and 82 and status = 'A' then cnt else 0 end) as total_people_a
,sum(case when age between 65 and 82 and status = 'B' then 1 else 0 end) as InActive_Age_cnt
,sum(case when age between 65 and 82 and status = 'B' then cnt else 0 end) as total_people_i
from #tmp
0
 

Author Comment

by:BrookK
ID: 33579001
This is what I get in result which does  not match to my result set in the example.

7      147      3      122      25
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 11

Expert Comment

by:Larissa T
ID: 33579219
please define what each number in you sample outout represent. You column name are ambiguous.



0
 

Author Comment

by:BrookK
ID: 33579454
First two columns are count of ages and total people with active flag and the rest two columns are for the same purpose but for inactive people.
0
 
LVL 11

Expert Comment

by:Larissa T
ID: 33580361
Then what is wrong with this query?

select sum(case when age between 65 and 82 and status = 'A' then 1 else 0 end) as Active_Age_Cnt
,sum(case when age between 65 and 82 and status = 'A' then cnt else 0 end) as total_people_a
,sum(case when age between 65 and 82 and status = 'B' then 1 else 0 end) as InActive_Age_cnt
,sum(case when age between 65 and 82 and status = 'B' then cnt else 0 end) as total_people_i
from #tmp
0
 
LVL 16

Expert Comment

by:vdr1620
ID: 33580765
Try the below Code.. will give you the same results as you have posted
select sum(case when age>= 65 and Age <= 82 and Status = 'A' then 1 else 0 end) as Active_AgeCnt
,sum(case when age>= 65 and Age <= 82 and Status = 'A' then [count] else 0 end) as Active_TotalPeople
,sum(case when age>= 65 and Age <= 82 and Status = 'B' then 1 else 0 end) as InActive_AgeCnt
,sum(case when age>= 65 and Age <= 82 and Status = 'B' then [count] else 0 end) as InActive_TotalPeople
from #tmp

Open in new window

0
 

Author Closing Comment

by:BrookK
ID: 33587423
Worked well
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question