Hi there,
I have a table of customers and a table of orders. I will clearly have 1 or many orders per customer. I need my select to return 1 row per customer so I planned to sum from orders table rather than join.
However, the field I will sum is dependant on a field on the order, namely the currency, due to the structure of the orders table.
So, if Field X in orders table = field x in customers table. Its the correct customer which will be my sub select joiner. So from the orders table, if field currency = GBP then I need to sum(fieldA) else sum(fieldB) * (Another sub select to obtain exchange rate based on the currency field)
Any idea's how I do this?
I had the following select but get this error Not a single-group group function
(SELECT
CASE
WHEN (orders."currency" = 'GBP'')
THEN
round(COALESCE(SUM(orders."gbp"),0),2)
ELSE
ROUND(COALESCE(SUM(orders."amount"),0) *
(Select exchange."rate"
From "curr_exch" exchange
WHERE exchange."currency" = orders."currency"
and exchange."date" =
(SELECT MAX(exchange."date") as "Date" From "curr_exch" exchange Where exchange."currency" = customer."currency" and exchange."date" <= customer."orderDate"))
/
(Select exchange."rate"
From "curr_exch" exchange
WHERE exchange."valuta" = ''GBP''
and exchange."date" =
(SELECT MAX(exchange."date") as "Date" From "curr_exch" exchange Where exchange."currency" = '"GBP' and exchange."date" <= customer."orderDate")),2)
END
FROM orders WHERE orders."ID" = Customer."OrderID" AND
) AS "IncomeGBP"