SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
issa269
issa269
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
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
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16073 Visits: 19533
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
issa269
issa269
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
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?
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16073 Visits: 19533
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
issa269
issa269
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 3
Thanks alot ChrisM@Work,
My mistake unit cost was 0 on those rows.
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16073 Visits: 19533
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search