Using the Case clause with Model clause

Hello PL SQL gurus
I've used some scripts I've found on these forums to create a mortgage amortization statement. What I am trying to accomplish is getting the script to run a calculation or use a value within a table based upon the value in that table.
Here are the two tables:
CREATE TABLE mortgage_facts (customer VARCHAR2(20), fact VARCHAR2(20),
amount NUMBER(10,3));
INSERT INTO mortgage_facts VALUES ('Smith', 'Loan', 131828.81);
INSERT INTO mortgage_facts VALUES ('Smith', 'Annual_Interest', 3.348);
INSERT INTO mortgage_facts VALUES ('Smith', 'Payments', 72);
INSERT INTO mortgage_facts VALUES ('Smith', 'PaymentAmt', 0);
CREATE TABLE mortgage (customer VARCHAR2(20), pmt_num NUMBER(4), principalp NUMBER(10,3), interestp NUMBER(10,3), mort_balance NUMBER(10,3));
INSERT INTO mortgage VALUES ('Smith',0, 0, 0, 131828.81);
If the value within the table mortgage_facts is zero, I want the script to run a calculation to be used in a MODEL clause. If it is not zero, I would like to use that value instead of the calculation. Below is the script that I am getting an error on (I have bolded the portion in question):
SELECT c, p, to_char(round(m,2),'fm$9999999.00') principal_balance,
to_char(round(pp,2),'fm$9999999.00') towards_principal,
to_char(round(ip,2),'fm$9999999.00') towards_interest,
to_char(round(mp,2),'fm$9999999.00') monthly_payment
FROM MORTGAGE
MODEL --See 1
IGNORE NAV
REFERENCE R ON
*(SELECT customer, fact, amt --See 2*
FROM mortgage_facts
*MODEL DIMENSION BY (customer, fact) MEASURES (amount amt)          --See 3*
RULES SEQUENTIAL ORDER
CASE WHEN mortgage_facts.fact = 'PaymentAmt' AND mortage_facts.amt = 0 THEN
*amt[ANY, 'PaymentAmt'] = mortgage_facts.amt*
ELSE
*amt[any, 'PaymentAmt']= (amt[CV(),'Loan']**
*Power(1+ (amt[CV(),'Annual_Interest']/100/12),*
*amt[CV(),'Payments']) **
*(amt[CV(),'Annual_Interest']/100/12)) /*
*(Power(1+(amt[CV(),'Annual_Interest']/100/12),*
*amt[CV(),'Payments']) - 1)*
END
DIMENSION BY (customer cust, fact) measures (amt)
MAIN amortization
PARTITION BY (customer c)
DIMENSION BY (0 p)
MEASURES (principalp pp, interestp ip, mort_balance m, customer mc, 0 mp )
RULES SEQUENTIAL ORDER
ITERATE(1000) UNTIL (ITERATION_NUMBER+1 =
r.amt[mc[0],'Payments'])
(ip[ITERATION_NUMBER+1] = m[CV()-1] *
r.amt[mc[0], 'Annual_Interest']/1200,
mp[ITERATION_NUMBER+1] = r.amt[mc[0], 'PaymentAmt'],
pp[ITERATION_NUMBER+1] = r.amt[mc[0], 'PaymentAmt'] - ip[CV()],
m[ITERATION_NUMBER+1] = m[CV()-1] - pp[CV()]
ORDER BY c, p
Any help is much appreciated. Thank you!!

Ok, here we go, one way with iterative model:
select *
from mortgage_facts
model
partition by (Customer)
dimension by (1 p)
measures(loan, payments, INTEREST, PAYMENTAMT, INTERESTPMT, PRINCIPALPMT, balance)
rules iterate(1e9) until (iteration_number+2 >= payments[1])
(loan[iteration_number+2]=loan[1]
,payments[iteration_number+2]=cv(p)-1
,interest[iteration_number+2]=interest[1]
,paymentamt[iteration_number+2]=ROUND(
  (LOAN[1] * (INTEREST[1]/12/100)*Power((1+INTEREST[1]/12/100), PAYMENTS[1])/(Power((1+INTEREST[1]/12/100),PAYMENTS[1])-1)), 2)
,INTERESTPMT[iteration_number+2]=round(balance[cv(p)-1]*interest[1]/1200, 2)
,PRINCIPALPMT[iteration_number+2]=paymentamt[cv()]-INTERESTPMT[cv()]
,balance[iteration_number+2]=balance[cv()-1]-PRINCIPALPMT[cv()]
CUSTOMER     P     LOAN     PAYMENTS     INTEREST     PAYMENTAMT     INTERESTPMT     PRINCIPALPMT     BALANCE
Smith     1     131828.81     72     3.348     0     0     0     131828.81
Smith     2     131828.81     1     3.348     2023.55     367.8     1655.75     130173.06
Smith     3     131828.81     2     3.348     2023.55     363.18     1660.37     128512.69
Smith     4     131828.81     3     3.348     2023.55     358.55     1665     126847.69
Smith     5     131828.81     4     3.348     2023.55     353.91     1669.64     125178.05
Smith     6     131828.81     5     3.348     2023.55     349.25     1674.3     123503.75
Smith     7     131828.81     6     3.348     2023.55     344.58     1678.97     121824.78
Smith     8     131828.81     7     3.348     2023.55     339.89     1683.66     120141.12
Smith     9     131828.81     8     3.348     2023.55     335.19     1688.36     118452.76
Smith     10     131828.81     9     3.348     2023.55     330.48     1693.07     116759.69
Smith     11     131828.81     10     3.348     2023.55     325.76     1697.79     115061.9
Smith     12     131828.81     11     3.348     2023.55     321.02     1702.53     113359.37
Smith     13     131828.81     12     3.348     2023.55     316.27     1707.28     111652.09
Smith     14     131828.81     13     3.348     2023.55     311.51     1712.04     109940.05
....

Similar Messages

  • How do I use the CASE statement  in the where clause?

    Hello Everyone,
    I have 2 queries that do what I need to do but I am trying to learn how to use the CASE statement.
    I have tried to combine these 2 into one query using a case statement but don't get the results I need.
    Could use some help on how to use the case syntax to get the results needed.
    thanks a lot
    select segment_name,
    product_type,
    count (distinct account_id)
    FROM NL_ACCT
    where
    ind = 'N'
    and
    EM_ind = 'N'
    and product_type in ('TAX','PAY')
    and acct_open_dt between (cast('2006-01-17' as date)) and (cast('2006-01-17' as date) + 60)
    GROUP BY 1,2
    order by product_type
    select segment_name,
    product_type,
    count (distinct account_id)
    FROM NL_ACCT
    where
    ind = 'N'
    and
    EM_ind = 'N'
    and product_type not in ('TAX','PAY')
    and acct_open_dt between (cast('2006-01-17' as date)) and (cast('2006-01-17' as date) + 30)
    group by 1,2
    order by product_type

    Something like:
    SELECT segment_name, product_type,
           SUM(CASE WHEN account_id IN ('TAX','PAY') and
                         acct_open_dt BETWEEN TO_DATE('2006-01-17', 'yyyy-mm-dd') and
                               TO_DATE('2006-01-17', 'yyyy-mm-dd') + 60 THEN 1
                    ELSE 0 END) tax_pay,
           SUM(CASE WHEN account_id NOT IN ('TAX','PAY') and
                         acct_open_dt BETWEEN TO_DATE('2006-01-17', 'yyyy-mm-dd') and
                               TO_DATE('2006-01-17', 'yyyy-mm-dd') + 30 THEN 1
                    ELSE 0 END) not_tax_pay
    FROM NL_ACCT
    WHERE ind = 'N' and
          em_ind = 'N' and
          acct_open_dt BETWEEN TO_DATE('2006-01-17', 'yyyy-mm-dd') and
                               TO_DATE('2006-01-17', 'yyyy-mm-dd') + 60
    GROUP BY segment_name, product_type
    ORDER BY product_typeNote: You cannor GROUP BY 1,2, you need to explicitly name the columns to group by.
    HTH
    John

  • How to use Special value set with Column clause to display addtional column

    hi
    can anyone tell me
    How to use Special value set with Column clause to display addtional column
    how can i use FND POPID and with COLUMN clause ?

    Dear Yaseen,
                To get the value in formula cumulative,after you have created the formula for that particular key figure,go to edit and click on tab "calculations".
    In that select overall result as 'count all values not equal to zero' and single result as 'count all values not equal to zero' .
    And mark the cumulative check box underneath.
    I hope you get the desired result and please do me favour .
    Sumit goomber

  • How to use the CASE Expression in Where Cluase?

    Hi All,
    I'm trying to use the CASE Expression in the Where Clause at some trigger on the Form?
    I've tried this Code:
    Declare
    N Number;
    begin
    SELECT COUNT(E.EMP_SID)
         INTO N
         FROM EMPLOYEES E, RANKS R
         WHERE CASE WHEN R.qualification_sid = 1104 AND E.rank_sid = 8 THEN
         (TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.spe_per)+1)
         ELSE
         (TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.mili_yea_per)+1)
         END
         BETWEEN TO_DATE('01-07-2011', 'DD-MM-RR') AND TO_DATE('31-07-2011', 'DD-MM-RR');
    END;
    When I run this code as a normal query at any SQL editor it works successfully, But When I Compile it at some trigger on the Form it gives me this error:
    Encountered the symbol "CASE" when expecting one of the following:
    ( - + mod ......
    Heeey how to specify the previous code to be shown as code in the thread?
    Note: I'm using Forms 6i

    OK I tried it and worked but for one condition:
    WHERE DECODE (E.qualification_sid, 1104,
         (TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.spe_per)+1),
         (TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.mili_yea_per)+1))
         BETWEEN TO_DATE('01-07-2011', 'DD-MM-RR') AND TO_DATE('31-07-2011', 'DD-MM-RR')
    But how to put two conditions for the same Expression:
    WHERE DECODE ((E.qualification_sid, 1104) AND (E.RANK_SID, 8),
         (TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.spe_per)+1),
         (TO_DATE(E.RANK_DATE, 'DD-MM-RR')+(365*M.mili_yea_per)+1))
         BETWEEN TO_DATE('01-07-2011', 'DD-MM-RR') AND TO_DATE('31-07-2011', 'DD-MM-RR')
    The previous code gives me this error: missing right parenthesis

  • Creating a view and use the view in a model

    Hi All,
    I am new to OBIEE and i have a requirement that i have two fields to be added to a subject area. Since we can't add queries directly to a model, i have to create a view and then put the query in it and then use the view in that model.
    I couldn't understand this much, if anybody could help me explaining in detail about the views and adding a query in it?
    Thanks
    Gohan

    Hi,
    Please refer the screen short below link
    http://imgur.com/b9sXS
    Right click any of one physial table(RPD physical layer) -> Select Properties -> from the properties windows choose 'Select' as Table type ->
    Please enable feature 'CREATE_VIEW_SUPPORTED' view just go to 'Features' tab and from the available option select 'CREATE_VIEW_SUPPORTED' is checked or not. If it is not checked then check the option.
    can you explain your current model and scenario?
    for example to you can use to fetch two fields from Subqueries,all trypes of joins(inner,left outer,right outer etc..,) its upto you case
    e.x:fetching two tables columns mapping subquery:
    SELECT column1, column2 FROM table1 WHERE column1 IN (SELECT column1 FROM table1
    INTERSECT SELECT column1 FROM table2 )
    SELECT column1, column2 FROM table1,table2 WHERE table1.column1 = table2.column1
    Alternative there is feature call "mulitple subject area" method will work (Union,Union all etc..,)
    Thanks
    Deva
    Edited by: Devarasu on Jun 6, 2012 4:40 PM

  • Can I use the iPhone 5s unlocked model A1533 for LG U  in South Korea

    Can I use the iPhone 5s unlocked model A1533 for LG U  in South Korea

    you might go here http://www.everymac.com/systems/apple/iphone/index-iphone-specs.html to see if the specs for the US phone are compatible with that required in KR

  • The key could not be obtained. You may need to use the -keystore argument with the specified keystor

    I am getting this error: "The key could not be obtained. You may need to use the -keystore argument with the specified keystore type." when trying to create an apk file using adt. I am following the steps mentioned here: http://blogs.adobe.com/ria/2010/10/28/creating-an-apk-file-using-flashbuilder-4-0/

    Could a self save from premiere cause the problem? By this i don't mean the autosave.
    Autosave is the only kind of 'self save', and is not likely to 'cause' project corruption.
    thnx for your reply. My first question came from a remark of Gakhue:
    "I'm using CS6 and I got that warning, I copied my project file on usb stick and re-opened in a different computer using CS5, it then gave me a few warnings about changing the name and saving it to auto save fault, I clicked away and said yes to each one"
    So i wondered how he did it
    The second question came from the fact that my last save was from 17 minutes after i left my studio. And i am not talking about an autosave. It was the main save of the corrupted videofile. And since i did not do it myself, the only option left is that Premiere did it? Or that premiere does not use the internal Mac clock and is on a different time. Which is not the case (just tested).

  • I everyone! my Ipad was stolen, i had the find my Iphone instaled but he never find the ipad, how can i find him. Apple can find him by the serial number? the case is with the police, but they can´t do anyting...

    I everyone! my Ipad was stolen, i had the find my Iphone instaled but he never find the ipad, how can i find him. Apple can find him by the serial number? the case is with the police, but they can´t do anyting...

    These links may be helpful.
    How to Track and Report Stolen iPad
    http://www.ipadastic.com/tutorials/how-to-track-and-report-stolen-ipad
    Reporting a lost or stolen Apple product
    http://support.apple.com/kb/ht2526
    Report Stolen iPad Tips and iPad Theft Prevention
    http://www.stolen-property.com/report-stolen-ipad.php
    How to recover a lost or stolen iPad
    http://ipadhelp.com/ipad-help/how-to-recover-a-lost-or-stolen-ipad/
    How to Find a Stolen iPad
    http://www.ehow.com/how_7586429_stolen-ipad.html
    Apple Product Lost or Stolen
    http://sites.google.com/site/appleclubfhs/support/advice-and-articles/lost-or-st olen
    Oops! iForgot My New iPad On the Plane; Now What?
    http://online.wsj.com/article/SB10001424052702303459004577362194012634000.html
    If you don't know your lost/stolen iPad's serial number, use the instructions below. The S/N is also on the iPad's box.
    How to Find Your iPad Serial Number
    http://www.ipadastic.com/tutorials/how-to-find-your-ipad-serial-number
     Cheers, Tom

  • Can i use the magick trackpad with the ipad?

    can i use the magick trackpad with the ipad? and the iphone?

    I'm looking for this same answer, I wonder if any new info. I've gone from posts saying why would anyone want to, and I now see that this is in the ATV section, which actually suits me because the setup I'm using it for is when I don't want to be up in my office on my iMac, but rather in the lounge in front of the big screen with my iPad mirrored to the Apple TV and bt keyboard connected. I wouldn't actually suggest anyone go out and buy a Magic Mouse or trackpad, but since I already have them would like to use it to make things easier when using my iPad as a mini computer with my TV as the screen. I can currently use the iPad to scroll no problem, but if I have to click somewhere specific then I must look at the iPad screen vice just moving a mouse or trackpad and having a pointer show up on my screen.  Obviously if my iMac was much closer,  I could mirror that screen and my keyboard and mouse would work just fine. (That is if my iMac was newer, but it's not, its a late 2009 model and that apparently is not new enough to be compatible with airplay mirroring.)

  • How do I use the App store with macbook pro and mac pro

    I currently use the app store with the mac book, but want to buy (share?) apps and music with the mac pro.
    I can't find anything that specifically outlines how this works.
    I use SplashID with the macbook and an iPhone, and want to use it (and sync it) on the mac pro as well.
    If you buy apps for a macbook, can you use them on a second machine, or do you need to buy it a second time?

    Your license from the MAS allows you to download and install the apps on as many Macs as you own or have under your control (institutional or employer supplied Macs.) The Mac should to be running Mac OS X 10.6.8 Snow Leopard or Mac OS X 10.7.2 Lion. Open the MAS app on the Mac Pro and sign into your account to have access to download all of your previous purchases in the Purchased pane.
    Simple apps can also be dragged & dropped over a network connection from the MacBook to the Mac Pro.

  • I have an apple id but i have not been able to use the same id with i tune and app store..what to do

    i have an apple id but i have not been able to use the same id with i tune and app store..what to do???every time i m trying to login it is telling that this id has not been used with i tune and App Store ....reviewing of my id is one of the option provided to me after this...

    Have you logged in and reviewed your account and entered payment details ? Unless the instructions on this page are followed when creating an account : Create an iTunes Store, App Store, or iBooks Store account without a credit card or other payment method - Apple Suppor…
    then credit card details will need to be entered before the account can be used to download any item from the store.
    You could see if this post by mountaingoatgirl lets you review your account without needing to enter credit card details : https://discussions.apple.com/message/24303054#24303054
    If not then you will either have to enter card details (you should be able to remove them after entering them), or create a new account (using the instructions on the above link).

  • I was trying to get the iOS 8.2 on my iPhone 5 but then it stopped and my phone is now on recovery mode I have pictures I need and they did not all fit on iCloud  how can I use the phone again with ALL my pictures and videos without restoring it?

    I was trying to get the iOS 8.2 on my iPhone 5 but then it stopped and my phone is now on recovery mode I have pictures I need and they did not all fit on iCloud  how can I use the phone again with ALL my pictures and videos without restoring it?

    Contacts are designed to be synced to a supported application on the computer or a cloud service.
    Pictures taken with the device are designed to regularly be copied off the device to a computer as would be done with any digital camera.
    If you have failed to use the device as designed it may be too late to recovery anything.
    Is the device regularly backed up to a computer via iTunes?  If so, the most recent backup (when restored to a replacement iOS device) should contain all contacts and pictures as of when the backup was created.

  • Can two people use the same computer with two different ipods?

    I was just wondering if me and my mother can use the same computer with our ipods?

    Glad you asked. Before you get rid of the old one move its iTunes Library to the new one:
    iTunes: How to move your music to a new computer
    http://support.apple.com/kb/HT4527
    "Deauthorize" the old computer too. Go to the iTunes Store menu and select "Deauthorize This Computer..."
    I hope you get a Mac this time

  • It´s possible to use the "Remote" app with Itunes and a Nokia E72 with Joikuspot installed only? Or I´ll need to buy a modem Wifi?

    It´s possible to use the "Remote" app with Itunes and a Nokia E72 with Joikuspot installed only? Or I´ll need to buy a modem Wifi?
    I was using "remote" with a D-link modem, that works fine at my iPad. But now I´m using only the cell phone as modem 3G, and don´t want to intall a router to put new cables at my notebook.

    What?
    You want to control iTunes on your computer using the Remote app on your iPad?
    And you want to connect the iPad and computer through your Nokia?
    No, this will not work.
    Just create a network with your notebook.

  • Has anyone figured a way to use the Magic Trackpad with Windows 7 on a partitioned disc using Bootcamp?

    Has anyone figured a way to use the magic trackpad with Windows 7 on a partitioned disc using Bootcamp?

    Yes. After Windows/Bootcamp discovers  your bluetooth device (Magic Trackpad or Mouse, be sure to make it discoverable), right click, with your wired mouse, of course, on the icon and select Properties.
    Then check the mouse drivers box and your device should work. (I located this solution on another thread)
    It works for both the Magic Mouse and Magic Trackpad.
    Must admit, the solution was not intuitive within the Windows environment. But we all must remember. It IS Windows, after all.
    Another thing. Keep in mind that the gestures on these devices that work just dandy in OSX do not carry over into Windows.
    It's Windows.

Maybe you are looking for

  • My company can't use anything beyond ver.3.6 but, I want the latest version. Can I run both?

    My company's website can only handle ver 3.6 and I want to run the latest version. Can I run both?

  • Merge to HDR Pro Issue

    Hello all, I'm going to give you as much information about this as possible in the first instance so we don't have to play post tag. In using Merge to HDR Pro in CS5, the process will allow me to pick the images I wish to use and being the process fo

  • Need a documentation tool

    hi all we are newly entering in to the production environment. so i want to maintain a all documentations in intranet. can anybody suggest me a documentation tool thanks in advance urs akhil

  • IPad Air operating system not supported.

    Is there a workaround to install drivers for an HP4580 printer on an iPad Air tablet? The operating system is version 7.1 This question was solved. View Solution.

  • Decision between multiple alternatives process type

    Hi All, I want to use this process type "decision between multiple alternatives" in one of my process chains, but i want use this process type if the DELTA infopackage pulls ZERO records, skip the next process and more than ZERO records continue the