Solved

This should be an easy query I think...

Posted on 2004-08-09
13
262 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
 
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

708 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

16 Experts available now in Live!

Get 1:1 Help Now