T
T
ta42016-10-28 15:10:50
SQL
ta4, 2016-10-28 15:10:50

Is it possible to make such a report in pure SQL?

There is such a task:
cbd6ac46e4b8484c940feb8b78340330.png
We solved it like this in MySQL:

SELECT item_id, price * IFNULL(rate, 1) as price_in_byr 
FROM items
LEFT JOIN (SELECT * FROM (SELECT * FROM `rates` ORDER BY date DESC) r group by currency) rmax
ON items.currency = rmax.currency

But according to the assignment, you can not use the features of MySQL, there must be pure SQL

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2016-10-28
@vshvydky

SELECT i.item_id as item_id, (i.price * r.rate) as price_in_byr 
  FROM items i, rates r
  WHERE r.date = max(r.date) AND i.curency = r.curency
ну и если интересует конкретный итем AND i.item_id = 1

up did not see the curency

N
nozzy, 2016-10-28
@nozzy

Syntax highlighting is gone.
Something like this:
select
t1.item_id,
t1.price,
t1.currency,
t2.rate
from items t1
left join (
select
t3.rate,
t4.currency,
t4.date
from rates t3
inner join (
select
currency,
max( date)
from rates
group by currency) t4
on t4.currency = t3.currency
and t4.date = t3.date) t2
on t1.currency = t2.currency
where t1.item_id = 5

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question