Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Stored procedure Script to display and add two different currency columns using case for conversions Expand / Collapse
Author
Message
Posted Tuesday, April 30, 2013 5:55 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 7, 2013 3:04 AM
Points: 3, Visits: 3
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

Post #1447972
Posted Tuesday, April 30, 2013 6:36 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:43 AM
Points: 6,719, Visits: 13,824
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1447989
Posted Tuesday, April 30, 2013 7:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 7, 2013 3:04 AM
Points: 3, Visits: 3
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?
Post #1448006
Posted Tuesday, April 30, 2013 7:18 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:43 AM
Points: 6,719, Visits: 13,824
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1448015
Posted Tuesday, April 30, 2013 7:24 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 7, 2013 3:04 AM
Points: 3, Visits: 3
Thanks alot ChrisM@Work,
My mistake unit cost was 0 on those rows.
Post #1448021
Posted Tuesday, April 30, 2013 8:01 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:43 AM
Points: 6,719, Visits: 13,824
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1448044
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse