# This should be an easy query I think...

...but I can't get it to save my life.  Check this out.

I've got 3 tables they have more fields in them but these are the ones I need to calculate the query.  I tried to articulate the relations in the diagram as well.

tbl_subscriber
subid----------v
subnumber---|-----v
dob               |      |
tbl_depdendents  |      |
subid----------^      |
dob                       |
tlb_renewals               |
subnumber---------^
renewaldate

I need to:
Enter a date range and it returns
1. total number of renewal within the range (I got this one already)
2. total number of renewals that have only 1 dependent in the tbl_dependents and BOTH of them are under 60 years old
3. total number of renewals that have only 1 dependent in the tbl_dependents and EITHER ONE is 60 or over
4. total number of renewals that have more than 1 dependent in the tbl_dependents and ALL of them are under 60 years old
5. total number of renewals that have more than 1 dependent in the tbl_dependents and ANY one is 60 or over

Sounds simple but I'm coming up with duplicates and my querries keep counting records that have mixed criteria such as if a record has someone who is over 60 and under 60 it counts them in both the under and over query.

Tom
LVL 1
###### Who is Participating?

Commented:
ok try this,

basic problem was i needed to use yy in the datediff   for the years test
and i've changed the Case when slightly

I've joined the tables as you question suggested...
I've used a suquery for the subscriber table as we need to determine the subscribers age
and another subquery for the dependents table which calculates the numbers of dependents
and wether they are under or over 60 by the subid..

the select then gives the answers to you 5 conditions...

either use the @start/@end variables for the dates.
or plug in you required date range...

Select count(*) as Renewals
,sum(case when d.num = 1 and d.u60=1 and subage<60 then 1 else null end) as c2
,sum(case when d.num = 1 and (d.o60=1 or subage>=60) then 1 else null end) as c3
,sum(case when d.num > 1 and d.o60=0 then 1 else null end) as c4
,sum(case when d.num > 1 and d.o60>0 then 1 else null end) as c5
From
tbl_renewals as r
Inner Join
(select a.*,datediff(yy,dob,getdate()) as subage from Tbl_subscriber) as S
on R.subnumber=s.subnumber
left outer join
(
select subid
,sum(case when datediff(yy,dob,getdate()) < 60 then 1 else null end) as u60
,sum(case when datediff(yy,dob,getdate()) < 60 then null else 1 end) as O60
,count(*) as Num
from tbl_depdendents
group by subid
) as D
on S.Subid=D.Subid
Where renewaldate between @Start and @end
0

Commented:
For BOTH and EITHER, do you mean the subscriber and the dependent?
0

Author Commented:
Yes I do, I should have clarified that.
0

Author Commented:
ALL and ANY mean the same as well.
0

Commented:
I think this is number 2...
select count(*) from tbl_renewals
where subnumber in
(select subnumber from tbl_subscriber
and subnumber in
(
select s.subnumber
from       tbl_subscriber s
group by s.subnumber having count(*)=1
)
and s.subid in
(
select subid
from tbl_dependents d
group by subid having count(*)=1
)
group by s.subnumber having count(*)=1)

0

Commented:
Oops - 2 changes: y should be yy for year, and the condition should be reversed (> instead of <):

I think this is number 2...
select count(*) from tbl_renewals
where subnumber in
(select subnumber from tbl_subscriber
and subnumber in
(
select s.subnumber
from      tbl_subscriber s
group by s.subnumber having count(*)=1
)
and s.subid in
(
select subid
from tbl_dependents d
group by subid having count(*)=1
)
group by s.subnumber having count(*)=1)
0

Author Commented:
I keep getting this, but the alias' look good to me?

Server: Msg 107, Level 16, State 2, Line 1
The column prefix 's' does not match with a table name or alias name used in the query.
0

Commented:
try this

Select count(*) as Renewals
,sum(case d.num when 1 and d.u60=1 and subage<60 then 1 else null end) as c2
,sum(case d.num when 1 and (d.o60=1 or subage>=60) then 1 else null end) as c3
,sum(case when d.num > 1 and d.o60=0 then 1 else null end) as c4
,sum(case when d.num > 1 and d.o60>0 then 1 else null end) as c5
From
tbl_renewals as r
Inner Join
(select a.*,datediff(y,dob,getdate()) as subage from Tbl_subscriber) as S
on R.subnumber=s.subnumber
left outer join
(
select subid
,sum(case when datediff(y,dob,getdate()) < 60 then 1 else null end) as u60
,sum(case when datediff(y,dob,getdate()) < 60 then null else 1 end) as O60
,count(*) as Num
from tbl_depdendents
group by subid
) as D
on S.Subid=D.Subid
Where renewaldate between @Start and @end
0

Author Commented:
I think maybe there are parenthesis missing, but this code is so far above my head I wouldn't know where?  I keep getting incorrect syntax errors.  I've put and ! where the syntax is going wrong. 3 places it says the syntax is incorrect.

Select count(*) as Renewals
,sum(case d.num when 1 !and d.u60=1 and subage<60 then 1 else null end) as c2
,sum(case d.num when 1 and (d.o60=1 or subage>=60) then 1 else null end) as c3
,sum(case when d.num > 1 and d.o60=0 then 1 else null end) as c4
,sum(case when d.num > 1 and d.o60>0 then 1 else null end) as c5
From
tbl_renewals as r
Inner Join
(select a.*,datediff(y,dob,getdate()) !as subage from Tbl_subscriber) as S
on R.subnumber=s.subnumber
left outer join
(
select subid
,sum(case when datediff(y,dob,getdate()) < 60 then 1 else null end) as u60
,sum(case when datediff(y,dob,getdate()) < 60 then null else 1 end) as O60
,count(*) as Num
from tbl_depdendents
group by subid
) !as D
on S.Subid=D.Subid
Where renewaldate between @Start and @end
0

Author Commented:
Wow, I have a whole lot to learn :)

What does this line do?
(select a.*,datediff(yy,dob,getdate()) as subage from Tbl_subscriber) as S

SQL says the column 'a' does not match with a column name or table name used in the query?
0

Author Commented:
I just changed the line to read like this and it worked!  But I wanted to make sure that I didn't mess it up by changing this line.

(select a.*,datediff(yy,dob,getdate()) as subage from Tbl_subscriber) as S

CHANGED TO
(select *,datediff(yy,dob,getdate()) as subage from Tbl_subscriber) as S
0

Author Commented:
If I want to replace the getdate() with another field from the table can I do that?  I tried replacing the getdate() with effectivedate to do the datediff function and it says effectivedate is not a valid column name but it is a valid column.
0

Commented:
(select a.*,datediff(yy,dob,getdate()) as subage from Tbl_subscriber) as S

CHANGED TO
(select *,datediff(yy,dob,getdate()) as subage from Tbl_subscriber) as S

fine ... sorry i was being lazy using the asterisk and was just trying to alias the TableName (as A)

I work with many different Database systems most  (it seems) will not accept any additional columns
on the select list if an asterisk is specified but will work if its preceded  by the table name..

The line just
(select *,datediff(yy,dob,getdate()) as subage from Tbl_subscriber) as S

gets SQL to work out the Subscribers Age at the same time as retrieving all the subscribers data
which then "makes" it easier to do the calculations required "at " a higher level.

hth

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.