У меня есть список курсов валют, разбитых по валютам, для которых мне нужно как-то опубликовать разницу в стоимости.
Например:
- GBP в USD | дата захвата: 23.02.12 | значение: 5
- GBP в USD | дата захвата: 22.02.12 | значение: 3
- GBP в USD | дата захвата:
21/02/12 | значение: 3
Я хочу, чтобы это случилось; когда запрос выполняется, он автоматически вычисляет самую последнюю дату, которая была взята, сравнивает ее с предыдущей датой и отправляет обратно значение, чтобы сказать, было ли это увеличение, т.е. если увеличение, то «^», если уменьшение «v» , если же "<->".
Мой текущий запрос может извлекать самую последнюю полученную дату, но мне все еще нужно выполнить подзапрос, чтобы извлечь вторую последнюю дату и значение, а затем опубликовать оператор if.
Я лаю правильное дерево с моим методом?
Вот мой код.
SELECT
distinct t.source_currency_code, t.target_currency_code,
t.entry_no as entry_no,
'(' + t.source_currency_code + ') ' + s.currency_name as source_currency_name,
'(' + t.target_currency_code + ') ' + x.currency_name as target_currency_name,
t.value_amount as value_amount,
t.uplift_percent as uplift,
t.date_loaded as date_loaded
from texchange_rate t, tcurrency s, tcurrency x
where
s.currency_code = t.source_currency_code and
x.currency_code = t.target_currency_code and
t.date_loaded in
(
SELECT max(date_loaded) from texchange_rate tt
where t.source_currency_code = tt.source_currency_code
and t.target_currency_code = tt.target_currency_code
)
order by source_currency_code, target_currency_code
SELECT
distinct t.source_currency_code, t.target_currency_code,
t.entry_no as entry_no,
'(' + t.source_currency_code + ') ' + s.currency_name as source_currency_name,
'(' + t.target_currency_code + ') ' + x.currency_name as target_currency_name,
t.value_amount as value_amount,
t.uplift_percent as uplift,
t.date_loaded as date_loaded2
from texchange_rate t, tcurrency s, tcurrency x
where
s.currency_code = t.source_currency_code and
x.currency_code = t.target_currency_code and
t.date_loaded in
(
SELECT max(date_loaded) from texchange_rate tt
where t.source_currency_code = tt.source_currency_code
and t.target_currency_code = tt.target_currency_code
)
and
t.value_amount in
(
SELECT value_amount from texchange_rate tt
where DATEDIFF(day, date_loaded, getdate()) < date_loaded
and t.source_currency_code = tt.source_currency_code
and t.target_currency_code = tt.target_currency_code
)
order by source_currency_code, target_currency_code
Некоторые примеры данных:
4366 GBP USD 15986 23/01/2012 13:42:02
4337 GBP USD 15600 17/10/2011 12:37:58
4312 GBP USD 15500 22/08/2011 14:00:01
4287 GBP USD 15500 21/08/2011 14:00:01