?
Solved

This should be an easy query I think...

Posted on 2004-08-09
13
Medium Priority
?
281 Views
Last Modified: 2008-03-06
...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
0
Comment
Question by:tommoran
[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
  • 7
  • 3
  • 2
  • +1
13 Comments
 
LVL 18

Expert Comment

by:SjoerdVerweij
ID: 11755404
For BOTH and EITHER, do you mean the subscriber and the dependent?
0
 
LVL 1

Author Comment

by:tommoran
ID: 11755420
Yes I do, I should have clarified that.
0
 
LVL 1

Author Comment

by:tommoran
ID: 11755429
ALL and ANY mean the same as well.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:AaronAbend
ID: 11755457
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)

0
 
LVL 10

Expert Comment

by:AaronAbend
ID: 11755492
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)
0
 
LVL 1

Author Comment

by:tommoran
ID: 11755543
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
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 11755578
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
 
LVL 1

Author Comment

by:tommoran
ID: 11758583
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
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 2000 total points
ID: 11761767
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
 
LVL 1

Author Comment

by:tommoran
ID: 11761828
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
 
LVL 1

Author Comment

by:tommoran
ID: 11762075
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
0
 
LVL 1

Author Comment

by:tommoran
ID: 11766730
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
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 11766759
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

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

801 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