Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
| Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 13th, 2007, 05:48 AM
Registered User
 
Join Date: Jun 2007
Location: Varna, , Bulgaria.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ordering price when multi-currency table

I have a table, lets call it products, that includes 2 columns for price and currency. Its dealing with euros, pounds and dollars.

I want to be able to generate the logic that can sort the table on the price, but by taking into account the currency exchange rates.

So, lets say the PRODUCTS table has the following records:

Price Currency

5 £
1 $
7 €

If you were to simply sort ascending on Price, it would return:

1
5
7

But if we want to return the results ASCending, according to lowest price value first (taking exchange rate into account), it would need to return:

1
7
5

I do have a RATES table stored in SQL, that has this structure:

id from to rate
-------------------
1 eur gbp 0.683
etc

Which has all of the conversion rates for all possibilities stored in it.

Does anyone have any ideas of the type of SQL code that could achieve this?

  #2 (permalink)  
Old June 13th, 2007, 06:51 AM
Registered User
 
Join Date: Jun 2007
Location: Varna, , Bulgaria.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I had thought the following code would do the job:

SELECT price,currency,CAST((price*rates.rate) AS UNSIGNED) AS priceval
FROM products, rates
WHERE currency=rates.from_code AND
rates.to_code=0
ORDER BY priceval ASC

Where to_code of 0 means EUROS. And matching currency field (in products) to rates.from_code, would match numeric value that represents each currency:

EUR = 0
USD = 1
GBP = 2

But when I execure this code, it just returns 0 for each of the 3 fields. Anyone got any ideas?


  #3 (permalink)  
Old June 13th, 2007, 07:22 AM
Registered User
 
Join Date: Jun 2007
Location: Varna, , Bulgaria.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If I remove the ORDER BY priceval ASC clause, then I get the result that I want - apart from the fact that its not in the order I need.
But adding this ORDER BY clause, causes the results to be displayed as 0 for all 3 columns in the select..???

  #4 (permalink)  
Old June 13th, 2007, 08:46 AM
Registered User
 
Join Date: Jun 2007
Location: Varna, , Bulgaria.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was being a numpty and reading my results incorrectly. The SQL code I posted does work. Its just that I had many records with 0 price in my test data that I forgot about.

So I worked out the solution as:

SELECT price,currency,CAST((price*rates.rate) AS UNSIGNED) AS priceval
FROM products, rates
WHERE currency=rates.from_code AND
rates.to_code=0
ORDER BY priceval ASC

If this helps anyone else with a similar problem, then great.



Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi table DataGrid melvik C# 1 October 6th, 2007 04:30 PM
stupid question to ordering column in table sal21 BOOK: Access 2003 VBA Programmer's Reference 0 May 9th, 2007 08:00 AM
Unknown table 'char_main' in MULTI DELETE uniacid Beginning PHP 9 December 11th, 2006 04:59 PM
Ordering results EstherMStrom XSLT 6 October 13th, 2004 09:50 AM
alternate ordering polecat XSLT 17 October 2nd, 2003 08:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.