Can the following be done without a view(s) or do I need a view?
We have a subscriptions table. We want to report on subscriptions that haven't expired yet but will expire in the date range the user specifies. Of those that will expire, we want to report on those subscriptions that have been renewed and those that have not been renewed. To get this out of our database, I need to join a table to itself and compare the subscription start dates to the subscription expiration dates. Following are the different SQL queries I am using to get Renewed, Not Renewed, and General.
--NOT RENEWED
SELECT s1.startdate,add_months(s1.startdate,s1.term), s1.subkey,s1.arckey,s1.subno,s1.status,s1.subtype,s1.subqty
FROM sbsubs s1,invhead inv, arcust arc
WHERE s1.arckey = arc.arckey and
s1.invkey = inv.invkey and
arc.slsvol is null and
s1.status in('A','F') and
inv.item = '2455'and
add_months(s1.startdate,s1.term) >= to_date('05/01/2000', 'mm/dd/yyyy') and
add_months(s1.startdate,s1.term) <= to_date('11/02/2000', 'mm/dd/yyyy') and
NOT EXISTS (select * from sbsubs s2 where s2.arckey=s1.arckey and
s2.subno=s1.subno and
s2.startdate = add_months(s1.startdate,s1.term) and
s2.startdate<>s1.startdate)
ORDER BY s1.ARCKEY
-- RENEWED
SELECT s1.startdate,add_months(s1.startdate,s1.term), s1.subkey,s1.arckey,s1.subno,s1.status,s1.subtype,s1.subqty
FROM sbsubs s1,invhead inv, arcust arc
WHERE s1.arckey = arc.arckey and
s1.invkey = inv.invkey and
arc.slsvol is null and
s1.status in('A','F') and
inv.item = '2455'and
add_months(s1.startdate,s1.term) >= to_date('05/01/2000', 'mm/dd/yyyy') and
add_months(s1.startdate,s1.term) <= to_date('11/02/2000', 'mm/dd/yyyy') and
EXISTS (select * from sbsubs s2 where s2.arckey=s1.arckey and
s2.subno=s1.subno and
s2.startdate = add_months(s1.startdate,s1.term) and
s2.startdate<>s1.startdate)
ORDER BY s1.ARCKEY
-- GENERAL
SELECT s1.startdate,add_months(s1.startdate,s1.term), s1.subkey,s1.arckey,s1.subno,s1.status,s1.subtype,s1.subqty
FROM sbsubs s1,invhead inv, arcust arc
WHERE s1.arckey = arc.arckey and
s1.invkey = inv.invkey and
arc.slsvol is null and
s1.status in('A','F') and
inv.item = '2455'and
add_months(s1.startdate,s1.term) >= to_date('05/01/2000', 'mm/dd/yyyy') and
add_months(s1.startdate,s1.term) <= to_date('11/02/2000', 'mm/dd/yyyy')
ORDER BY s1.ARCKEY
Brutal eh. Thanks for any thoughts on the subject.
We have a subscriptions table. We want to report on subscriptions that haven't expired yet but will expire in the date range the user specifies. Of those that will expire, we want to report on those subscriptions that have been renewed and those that have not been renewed. To get this out of our database, I need to join a table to itself and compare the subscription start dates to the subscription expiration dates. Following are the different SQL queries I am using to get Renewed, Not Renewed, and General.
--NOT RENEWED
SELECT s1.startdate,add_months(s1.startdate,s1.term), s1.subkey,s1.arckey,s1.subno,s1.status,s1.subtype,s1.subqty
FROM sbsubs s1,invhead inv, arcust arc
WHERE s1.arckey = arc.arckey and
s1.invkey = inv.invkey and
arc.slsvol is null and
s1.status in('A','F') and
inv.item = '2455'and
add_months(s1.startdate,s1.term) >= to_date('05/01/2000', 'mm/dd/yyyy') and
add_months(s1.startdate,s1.term) <= to_date('11/02/2000', 'mm/dd/yyyy') and
NOT EXISTS (select * from sbsubs s2 where s2.arckey=s1.arckey and
s2.subno=s1.subno and
s2.startdate = add_months(s1.startdate,s1.term) and
s2.startdate<>s1.startdate)
ORDER BY s1.ARCKEY
-- RENEWED
SELECT s1.startdate,add_months(s1.startdate,s1.term), s1.subkey,s1.arckey,s1.subno,s1.status,s1.subtype,s1.subqty
FROM sbsubs s1,invhead inv, arcust arc
WHERE s1.arckey = arc.arckey and
s1.invkey = inv.invkey and
arc.slsvol is null and
s1.status in('A','F') and
inv.item = '2455'and
add_months(s1.startdate,s1.term) >= to_date('05/01/2000', 'mm/dd/yyyy') and
add_months(s1.startdate,s1.term) <= to_date('11/02/2000', 'mm/dd/yyyy') and
EXISTS (select * from sbsubs s2 where s2.arckey=s1.arckey and
s2.subno=s1.subno and
s2.startdate = add_months(s1.startdate,s1.term) and
s2.startdate<>s1.startdate)
ORDER BY s1.ARCKEY
-- GENERAL
SELECT s1.startdate,add_months(s1.startdate,s1.term), s1.subkey,s1.arckey,s1.subno,s1.status,s1.subtype,s1.subqty
FROM sbsubs s1,invhead inv, arcust arc
WHERE s1.arckey = arc.arckey and
s1.invkey = inv.invkey and
arc.slsvol is null and
s1.status in('A','F') and
inv.item = '2455'and
add_months(s1.startdate,s1.term) >= to_date('05/01/2000', 'mm/dd/yyyy') and
add_months(s1.startdate,s1.term) <= to_date('11/02/2000', 'mm/dd/yyyy')
ORDER BY s1.ARCKEY
Brutal eh. Thanks for any thoughts on the subject.