Derived PL/SQL

Posted on 2004-11-05
Last Modified: 2010-05-18

I want to put logic in my sql statement such that based on the data in one column, I multiply the value in another field by 1 or negative 1.  For example, consider this data

Amount             Credit                  Charge
100                       0                        1
200                       0                        1
250                       1                        0
145                       0                        1

Basically, it boils down to accounting:  if the amount is a charge (meaning the credit is 0 and the charge is 1) then I just take the amount field.  If the amount is a credit (meaning the Credit field is 1 and the charge field is 0), then I take the amount field *-1.  This means that my amounts above would come out as follows:


I know that if the field contained a NULL instead of 1 or 0, I could just use the NVL field to multiply the value by negative one, but I don't know how to do this based on a 0 or 1 being in another column.  Any ideas?  And I'm trying to just do this in one select statement withou having to build a procedure or anything.

Question by:ccorrente

    Author Comment

    Think I got it.  Can use the replace function to replace 0's in the Charge with -1.  

    replace (string1, string_to_replace, [replacement_string] )

    Then multiply the result of the replace function by the amount in my SQL statement.  If the Charge is 0 (meaning it's a credit) then the result of the replace will be -1 and thus multiplying the amount by -1 will be negative.  If the Charge is 1 (meaning it is a charge) then the replace returns a 1 and multiplying it just produces the same positive amount.
    LVL 76

    Accepted Solution


    select amount * decode(charge,1,-1,1) from table;

    Author Comment

    Yeah, I like decode better.  Wasn't aware of that one.  I'll be sure to keep that one in mind in the future!
    LVL 76

    Expert Comment

    by:slightwv (䄆 Netminder)
    It's the old style way of doing an if-then-else in Oracle.

    FYI: for more complex queries, starting in 9i (I think.....I tend to get confused with all the NEW sql) Oracle introduced 'CASE' statements.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

    737 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

    20 Experts available now in Live!

    Get 1:1 Help Now