Is it possible to create a join on two tables when one of the columns has been derived.
e.g.
Table 1
ID Name Price Qty
1 Sand 12,00 2
Table 2
Week Top_Sales
1 24
e.g.
Select a.ID, a.price, a.qty, a.qty *a.price as "Total Sales"
from Table 1 a
inner join on table 2 b
on
b.top_sales = Total Sales
Is this possible to perform this join without creating a temporary table or a view?
Sure, but you can't use the named column from the SELECT clause because that name hasn't been materialized yet. You will need to do:
Select a.ID, a.price, a.qty, a.qty *a.price as "Total Sales"
from Table 1 a
inner join on table 2 b
on b.top_sales = a.qty *a.price
This seems a very dangerous query, if you ask me though. In your table2, why not include the primary key of the top sales rows? Then you wouldn't have to do this wonky join and you could join on keys. I guess this because you have a fixed amount for the top seller, so you had to have calculated it from the quantity and price somewhere.
No comments:
Post a Comment