Skip to content
Advertisement

Using Cases for currency conversion in oracle

I have two tables

what I am trying to find is the ‘daily amount GBP Equivalent per day for the last three months’. For example on date ’05/01/2022 ‘ check the given amount in Payments_history table if the currency is GBP add it into total and move on to the next payment, next if the currency is in USD check for its equivalent in GBP rate and convert it into GBP and print the result. If the amount is in some currency whose from_ccy is not GBP skip that transaction

This is what I have done so far

but it’s giving me error

Advertisement

Answer

Your errors include:

  • Using = inside the THEN clause of a CASE expression is invalid syntax.
  • You cannot refer to a column alias inside the sub-query where it is defined (except in an ORDER BY clause) so you cannot use total inside the CASE expression.
  • A CASE expression needs to have the same data type in all of its outputs so you cannot mix numbers and strings.
  • You GROUP BY PH.PAYMENT_DATE but you do not have any aggregation function around the CASE expression.
  • If you do aggregate then 'CURRENCY NOT FOUND' is not something you can total.
  • In Oracle, AS before a table alias is invalid syntax.
  • You have a mix of quoted identifiers HR."from_ccy" and unquoted identifiers HR.from_ccy. While it is possible to have both in a table, one of them is almost certainly wrong (and using quoted identifiers is bad practice) and your DDL statements do not use quotes.
  • Unless you really do intend to get rows from December of any year, do not compare only months. Compare on a range from the start of the month 3-months ago until before the start of the month two-months ago.
  • Your PL/SQL block does not have an END statement.
  • You are using a SELECT statement in PL/SQL without using SELECT ... INTO.
  • You probably don’t want to use PL/SQL.
  • None of your sample data is from 3 months ago (December 2021).

Something like this:

Which, for the sample data:

Outputs:

PAYMENT_DATE TOTAL
06-DEC-21 30
05-DEC-21 24.776

db<>fiddle here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement