Hi Tom,
I am trying to get YTD in a view. I have below view,
create or replace view billsummary as
select szRegionCode,
szState,
szPartitionCode,
szProduct,
TO_CHAR(dtSnapshot,'YYYY.MM') szMonthYear,
szJioCenter,
dtSnapshot,
szCustomerCategory,
iCycleDay,
fbillamt,
ibillcnt
sum(CRBC.fbillamt) over (partition by szRegionCode, szState, CRBC.szProduct, TO_CHAR(CRBC.dtSnapshot,'YYYY') , CRBC.szCustomerCategory, iCycleDay
order by partition by szRegionCode, szState, CRBC.szProduct, CRBC.dtSnapshot , CRBC.szCustomerCategory, iCycleDay) fytdbillamt
sum(CRBC.ibillcnt) over (partition by szRegionCode, szState, CRBC.szProduct, TO_CHAR(CRBC.dtSnapshot,'YYYY') , CRBC.szCustomerCategory, iCycleDay
order by partition by szRegionCode, szState, CRBC.szProduct, CRBC.dtSnapshot , CRBC.szCustomerCategory, iCycleDay) iytdBillcnt
From BillCollection;
Suppose table BillCollection has data from Jan 2017 to Dec 2017. If I execute below sql for Dec 2017 month, it should give me sum of all bill amount generated in December month(MTD) and sum of all bill amount generated from Jan 2017 to Dec 2017(YTD). But, YTD figure is not matching at all. Can you please guide me how can I code above mentioned view?
Select szMonthYear, szregioncode, sum(fbillamt) MTD , sum (fytdbillamt) YTD
From billsummary
order by szMonthYear
Hmmm..... something wrong the view here before we even get started
sum(CRBC.fbillamt) over (partition by ... order by partition by ...) fytdbillamt
because that is not valid syntax, eg
SQL> with t as ( select 1 x, 2 y, 3 z from dual)
2 select sum(x) over ( partition by y order by partition z )
3 from t;
select sum(x) over ( partition by y order by partition z )
*
ERROR at line 2:
ORA-00907: missing right parenthesis
Without trying to make too many assumptions, its important to realise that a view will respect view *definition* first, so a view that does:
create view V as
select ...,
sum(x) over () as blah
would have column 'blah' being the sum calculated across ALL rows in the table. So even if you then did:
select * From V where name = 'Jones'
then the value that comes back from 'blah' will STILL be the total for ALL rows, not just those rows where name = 'BLAH'