Solved

This should be an easy query I think...

Posted on 2004-08-09
13
268 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
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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 500 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Some sers suddenly getting error popup msg 28 86
SQL Server Generate Scripts Fails 5 34
Help with SQL Server Polygon 2 32
SQL Replication question 9 41
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now