Link to home
Start Free TrialLog in
Avatar of tommoran
tommoran

asked on

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.

HELP!  Ready to hang myself.

Thanks in advance,
Tom
Avatar of SjoerdVerweij
SjoerdVerweij

For BOTH and EITHER, do you mean the subscriber and the dependent?
Avatar of tommoran

ASKER

Yes I do, I should have clarified that.
ALL and ANY mean the same as well.
I think this is number 2...
select count(*) from tbl_renewals
where subnumber in
      (select subnumber from tbl_subscriber
      where dob<dateadd(y, -60, getdate() )
      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
            where dob<dateadd(y, -60, getdate())
            group by subid having count(*)=1
            )
      group by s.subnumber having count(*)=1)

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
     where dob>dateadd(yy, -60, getdate())
     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
          where dob>dateadd(yy, -60, getdate())
          group by subid having count(*)=1
          )
     group by s.subnumber having count(*)=1)
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.
Avatar of Lowfatspread
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
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
ASKER CERTIFIED SOLUTION
Avatar of Lowfatspread
Lowfatspread
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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.

LINE USED TO READ
     (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
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.
LINE USED TO READ
     (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