select yr_0.loccpcode,yr_0.loccode, yr_0.mnth, cnt_2, cnt_1, cnt_0
-- ======================================================================================
-- Above code formats output rows from 3 years of summary rows from in-line views, below.
-- ======================================================================================
from
-- ======================================================================================
-- Section 1: "Current-year" in-line view follows:
-- ======================================================================================
(select loc.loccpcode, loc.loccode, loc.mnth, count(account) cnt_0
-- ======================================================================================
-- Sub-Section 1a: following in-line view produces 12 months of rows for each
-- "loccpcode" and "loccode" combination:
-- ======================================================================================
from (select loccpcode, loccode, mnth
-- ======================================================================================
-- Sub-Section 1a1: Following in-line view produces 12-row driver for months
-- (Note: Code uses "all_tab_columns" simply as a generic object
-- that we know has at least 12 rows from which to create
-- a 12-row driver object to produce 12 months.)
-- ======================================================================================
from (select rownum mnth from all_tab_columns
where rownum <= 12) mnths
-- ======================================================================================
-- Cartesian join of each "medlocation", below, with the 12-row in-line
-- view, above, creates 12 months of rows for each "medlocation".
-- ======================================================================================
,medlocations l) loc
-- ======================================================================================
-- Sub-Section 1b: In-line view, below, harvests from medcharges, just the rows that
-- fit the business criteria: Last three years of medcharges that
-- are Type 'C' and splitflag is null
-- ======================================================================================
,(select *
-- ======================================================================================
-- Sub-Section 1b1: Following in-line view results in all columns from "medcharges"
-- plus it simplifies the "ServiceDate" into an Oracle DATE,
-- "trans_dt", and simplifies the Month, "mnth" for proper placement
-- in the final output.
-- ======================================================================================
from (select x.*
,to_date(servicedate,'j') trans_dt
,to_char(to_date(servicedate,'j'),'mm') mnth
from medcharges x) c
where type = 'C' and splitflag is null
and to_char(trans_dt,'yyyy')
= to_char(sysdate,'yyyy')) txn -- means "this year's transactions"
-- ======================================================================================
-- Following WHERE outer joins all Location+Month rows with "Medcharges"
-- ======================================================================================
where loc.loccpcode = txn.cpcode(+)
and loc.loccode = txn.docloc(+)
and loc.mnth = txn.mnth(+)
group by loc.loccpcode, loc.loccode, loc.mnth) yr_0
-- ======================================================================================
-- Section 2: Following code matches logic for "Current-year", but for "One Year Ago"
-- ======================================================================================
,(select loc.loccpcode, loc.loccode, loc.mnth, count(account) cnt_1
from (select loccpcode, loccode, mnth
from (select rownum mnth from all_tab_columns
where rownum <= 12) mnths
,medlocations l) loc
,(select *
from (select x.*
,to_date(servicedate,'j') trans_dt
,to_char(to_date(servicedate,'j'),'mm') mnth
from medcharges x) c
where type = 'C' and splitflag is null
and to_char(trans_dt,'yyyy')
= to_char(sysdate,'yyyy')-1) txn -- "-1" means "one year ago"
where loc.loccpcode = txn.cpcode(+)
and loc.loccode = txn.docloc(+)
and loc.mnth = txn.mnth(+)
group by loc.loccpcode, loc.loccode, loc.mnth) yr_1
-- ======================================================================================
-- Section 3: Following code matches logic for "Current-year", but for "Two Years Ago"
-- ======================================================================================
,(select loc.loccpcode, loc.loccode, loc.mnth, count(account) cnt_2
from (select loccpcode, loccode, mnth
from (select rownum mnth from all_tab_columns
where rownum <= 12) mnths
,medlocations l) loc
,(select *
from (select x.*
,to_date(servicedate,'j') trans_dt
,to_char(to_date(servicedate,'j'),'mm') mnth
from medcharges x) c
where type = 'C' and splitflag is null
and to_char(trans_dt,'yyyy')
= to_char(sysdate,'yyyy')-2) txn -- "-2" means "two years ago"
where loc.loccpcode = txn.cpcode(+)
and loc.loccode = txn.docloc(+)
and loc.mnth = txn.mnth(+)
group by loc.loccpcode, loc.loccode, loc.mnth) yr_2
-- ======================================================================================
-- following WHERE joins data from three years onto one row of output,
-- ======================================================================================
where (yr_0.loccpcode = yr_1.loccpcode and
yr_0.loccode = yr_1.loccode and
yr_0.mnth = yr_1.mnth)
and (yr_0.loccpcode = yr_2.loccpcode and
yr_0.loccode = yr_2.loccode and
yr_0.mnth = yr_2.mnth)
/
*****************************************************************************************