Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL存储过程-尝试按日期和if语句进行区分_Sql_Sql Server 2005_Stored Procedures_If Statement_Datediff - Fatal编程技术网

SQL存储过程-尝试按日期和if语句进行区分

SQL存储过程-尝试按日期和if语句进行区分,sql,sql-server-2005,stored-procedures,if-statement,datediff,Sql,Sql Server 2005,Stored Procedures,If Statement,Datediff,我有一个按货币分类的汇率列表,我需要以某种方式在上面公布价值差异 例如: 英镑兑美元|捕获日期:2012年2月23日|价值:5 英镑兑美元|捕获日期:2012年2月22日|价值:3 英镑兑美元|捕获日期: 2012年2月21日|价值:3 我想发生的是;当查询运行时,它会自动计算出最近的日期,将其与上一个日期进行比较,并回发一个值,说明该日期是否增加,即如果增加,则^,如果减少v,如果相同 我当前的查询可以提取最新的日期,但我仍然需要执行子查询以提取第二个最新的日期和值,然后发布if语句 我的方法

我有一个按货币分类的汇率列表,我需要以某种方式在上面公布价值差异

例如:

英镑兑美元|捕获日期:2012年2月23日|价值:5 英镑兑美元|捕获日期:2012年2月22日|价值:3 英镑兑美元|捕获日期: 2012年2月21日|价值: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

或者,您可以尝试对上一个问题的已接受答案进行修改:

select source_currency_code, 
       target_currency_code,
       max(source_currency_name) source_currency_name, 
       max(target_currency_name) target_currency_name,
       max(case when rn = 1 then value_amount end) value_amount,
       max(case when rn = 1 then uplift end) uplift,
       max(case when rn = 1 then date_loaded end) date_loaded,
       case sign(max(case when rn = 1 then value_amount end) - 
                 max(case when rn = 2 then value_amount end))
           when 1 then '^'
           when 0 then '<->'
           when -1 then 'v'
       end change_over_previous
from 
(select t.source_currency_code, 
        t.target_currency_code,
        '('+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,
        rank() over (partition by t.source_currency_code, 
                                  t.target_currency_code 
                     order by t.date_loaded desc) rn
 from texchange_rate t
 join tcurrency s on s.currency_code = t.source_currency_code 
 join tcurrency x on x.currency_code = t.target_currency_code) v
where rn in (1, 2)
group by source_currency_code, target_currency_code
order by source_currency_code, target_currency_code

不必使用WHERE a.RowNum=1,否则它将返回所有记录,并在记录旁边加上以前的值。

简单地说,是的。您需要获取最新的值,并连接回最大日期!=最新值的日期。或者,您可以递归地找到所有差异对,然后取最后一个。从每个表中获得一些示例数据,或者至少每个表的定义,这将是非常有帮助的。嗯,好的。我在我的第一篇文章中编辑了一些样本数据,因为我不能在回复框中清楚地完成。先生,你是个天才。你怎么这么擅长这个?!我已经返回到您的方法,但我尝试的解决方案是将结果发布到一个新表中,该表包含单独的字段,然后使用and if语句计算结果差异。再次感谢,非常感谢!嘘!很高兴我能帮忙
select source_currency_code, 
       target_currency_code,
       max(source_currency_name) source_currency_name, 
       max(target_currency_name) target_currency_name,
       max(case when rn = 1 then value_amount end) value_amount,
       max(case when rn = 1 then uplift end) uplift,
       max(case when rn = 1 then date_loaded end) date_loaded,
       case sign(max(case when rn = 1 then value_amount end) - 
                 max(case when rn = 2 then value_amount end))
           when 1 then '^'
           when 0 then '<->'
           when -1 then 'v'
       end change_over_previous
from 
(select t.source_currency_code, 
        t.target_currency_code,
        '('+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,
        rank() over (partition by t.source_currency_code, 
                                  t.target_currency_code 
                     order by t.date_loaded desc) rn
 from texchange_rate t
 join tcurrency s on s.currency_code = t.source_currency_code 
 join tcurrency x on x.currency_code = t.target_currency_code) v
where rn in (1, 2)
group by source_currency_code, target_currency_code
order by source_currency_code, target_currency_code
;WITH ExchangeCTE AS
(   SELECT  *, ROW_NUMBER() OVER(PARTITION BY source_currency_code, target_currency_code ORDER BY date_loaded DESC) [RowNum]
    FROM    texchange_rate
)
SELECT  a.source_currency_code, 
        a.target_currency_code,
        a.entry_no AS entry_no,
        '(' + a.source_currency_code + ') '  + s.currency_name AS source_currency_name, 
        '(' + a.target_currency_code + ') '  + t.currency_name AS target_currency_name,
        a.value_amount AS value_amount,
        a.uplift_percent AS uplift,
        a.date_loaded AS date_loaded2,
        b.value_amount AS Previous_Value_Amount,
        CASE WHEN a.value_amount > b.value_amount THEN '^'
            WHEN a.value_amount = b.value_amount THEN '<->'
            ELSE 'v'
        END AS [Symbol]
FROM    ExchangeCTE a
        INNER JOIN ExchangeCTE b
            ON a.source_currency_code = b.source_currency_code 
            AND a.target_currency_code = b.target_currency_code
            AND a.RowNum = b.RowNum - 1 -- PREVIOUS RECORD
        INNER JOIN tcurrency s
            ON a.source_currency_code = S.currency_code 
        INNER JOIN tcurrency t
            ON a.target_currency_code = S.currency_code  
WHERE   a.RowNum = 1 -- LATEST RECORD