This question is about a general technique in SQL, that I can't quite work out how to best achieve with my current understanding of the language.
Note that this will be running against Oracle, in case any vendor-specific functionality would be particularly useful, though I expect there is a nice way of doing this with "pure" SQL.
Short version of the question: If I have an OR condition on a query such that I check that a value in my "main" table A corresponds to a value either in table B or in table C, what is the best way to conditionally modify (e.g. multiply) the statements selectors if the value was found in table C but not in table B?
Long version (with example): Currently I have a query that looks roughly like this (it's a lot more complicated, but this is the basic structure):
select o.value, /* other stuff... */
from orders o,
clients c
where o.client = c.pkey
and c.name = ?
All is fine. However, now imagine that some clients are allowed to act on behalf of others - but because of processing fees or some logic trades carried out this way don't quite have their full value. So there's a table managed_orders
with columns for order
, client
(both primary keys to the respective tables) and another column multiplier
, which is a floating point number between 0.0 and 1.0 representing the proportion of the order value that should be "carried through".
Right - so modifying the above query to show orders that are either owned by the given client or managed by them for someone else, is fairly straightforward. However, I would need to multiply the o.value
selector by the managed order's multiplier
if this is a managed order, but if it's a classic direct order then this isn't needed (or equivalently we could multiply by 1.0).
select o.value * m.multiplier /* Will not work in all cases */, /* other stuff... */
from orders o,
clients c,
managed_orders m
where (o.client = c.pkey or m.order = o.pkey)
and c.name = ?
and m.client = c.pkey
How best to achieve this conditional multiplication?