Stored procedure Script to display and add two different currency columns using case for conversions

  • Hi there,

    I have a table cost, with costs of products from one colum(cost). This column has has different currency values e.g dollars, and pounds. i need to show two other columns on the right of column cost. 1st Column with dollars after converting., and 2nd Column with Pound value after converting.

    The conversion for different currencies are in another table called currencyt. each row has currency id,curreny name, currency equivalent to dollars(eg. currency id=2,curreny name=pounds and currency equivalent to dollars=1.8 )

    How can i do that in a storage procedure.

    I tried to use the below storage procedure but display alot of duplicate.

    select t1.Product_Name as Item ,t1.Description as Description ,

    t5.Currency as Currency,t1.stock_quantity as Quantity,t4.unit_price as Unit_Cost,

    t1.stock_quantity*t4.unit_price as Total_Cost, t1.stock_quantity* xx.unit_price_rate

    from Product t1,section t2,Department t3,Entry_New t4,Currency t5,

    (select case

    when t9.id in (3,1) then

    t8.unit_price

    Else

    t8.unit_price * t9.Rate

    End As unit_price_rate

    from Currency t9,Entry_New t8,Product t10

    where t9.id=t8.currency and t10.Product_ID=t8.Product_ID)xx

    where t1.section_ID=t2.section_ID and t2.Department_ID=t3.Department_ID and t1.Product_ID=t4.Product_ID and t5.id=t4.currency

    and t1.stock_quantity>0

    Any help will be much appreciated. Thanks

  • The subquery (inner query) you added isn't correlated to the outer (main) query. Since all of the tables in the inner query are already referenced by the outer query, do you need the subquery at all?

    -- use meaningful table aliases

    -- use proper joins

    SELECT

    p.Product_Name as Item ,

    p.Description as Description ,

    c.Currency as Currency,

    p.stock_quantity as Quantity,

    e.unit_price as Unit_Cost,

    p.stock_quantity * e.unit_price as Total_Cost,

    p.stock_quantity *

    (case when c.id in (3,1) then e.unit_price Else e.unit_price * c.Rate End)

    FROM Product p

    INNER JOIN Section s

    ON p.section_ID = s.section_ID

    INNER JOIN Department d

    ON s.Department_ID = d.Department_ID

    INNER JOIN Entry_New e

    ON p.Product_ID = e.Product_ID

    INNER JOIN Currency c

    ON c.id = e.currency

    /*

    (

    select

    case when ci.id in (3,1) then ei.unit_price Else ei.unit_price * ci.Rate End As unit_price_rate

    from Currency ci,

    Entry_New ei,

    Product pin

    where ci.id = ei.currency

    and pin.Product_ID = ei.Product_ID

    ) xx

    */

    WHERE p.stock_quantity > 0

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Thanks ChrisM@Work for the help, it removed the duplicates as i had asked but the total cost for the added column displayed value for only dollars(id for dollar is 3 and the default one is 1) but for the currencies with id not 1 or 3, their total cost became zero, any idea why they showed zero?

  • issa269 (4/30/2013)


    Thanks ChrisM@Work for the help, it removed the duplicates as i had asked but the total cost for the added column displayed value for only dollars(id for dollar is 3 and the default one is 1) but for the currencies with id not 1 or 3, their total cost became zero, any idea why they showed zero?

    Add c.Rate to the SELECT list.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Thanks alot ChrisM@Work,

    My mistake unit cost was 0 on those rows.

  • issa269 (4/30/2013)


    Thanks alot ChrisM@Work,

    My mistake unit cost was 0 on those rows.

    You're welcome 😉

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply