【问题标题】:How to filter data based using date column?如何使用日期列过滤数据?
【发布时间】:2016-01-01 10:31:45
【问题描述】:

如何从具有最大日期的 3 个表中获取数据

select 
    c.model_key, v.id, v.category_Id, v.Text1, v.Numeric1,
    c.Name, fx.Currency_code, fx.Rate, fx.effective_dt 
from 
    aps.v_m_fxrates fx 
join 
    aps.t_m_value v on fx.Currency_code = v.Text1 
join 
    aps.t_m_category c on v.Category_Id = c.Id 
where 
    c.Name = 'FXRates' 
    and c.Model_Key = 25 
    and v.Text1 = fx.Currency_code 
    and fx.version = 2

使用上面的查询我得到了结果,但结果显示为所有effective_dt。而不是这个,我只需要选择最新的effective_dt 的记录。在下图中,有 2 条带 AED 的记录,其中最晚日期为 1999-03-31 的记录必须返回。在此之后,我不知道如何继续并进一步过滤结果以实现输出。

我也试过了

select 
    c.model_key, v.id, v.category_Id, v.Numeric1,
    c.Name, fx.Currency_code, fx.Rate, fx.effective_dt 
from 
    aps.v_m_fxrates fx 
join 
    aps.t_m_value v on fx.Currency_code = v.Text1 
join 
    aps.t_m_category c on v.Category_Id = c.Id 
where 
    c.Name = 'FXRates' 
    and c.Model_Key = 25 
    and v.Text1 = fx.Currency_code 
    and fx.version = 2 
    and fx.effective_dt in (select MAX(effective_dt) 
                            from aps.v_m_fxrates)

但没有返回任何内容。

实际输出:

预期输出:

【问题讨论】:

    标签: sql sql-server-2008 join


    【解决方案1】:

    在子查询中使用 row_number() 函数,如下所示:

    select 
         c.model_key, v.id, v.category_Id, v.Text1, v.Numeric1,
         c.Name, fx.Currency_code, fx.Rate, fx.effective_dt 
    from (
        select
             Currency_code,Rate,effective_dt
            ,SeqNo = row_number() over (partition by Currency_code order by effective_dt desc)
        from aps.v_m_fxrates 
        where version = 2
    ) fx 
    join 
        aps.t_m_value v on fx.Currency_code = v.Text1 
    join 
        aps.t_m_category c on v.Category_Id = c.Id 
    where c.Name = 'FXRates' 
      and c.Model_Key = 25 
      and v.Text1 = fx.Currency_code 
      and fx.SeqNo = 1
    

    【讨论】:

      【解决方案2】:

      尝试使用运算符 TOP 1 和相应的 ORDER BY

      例如:

      SELECT TOP 1 C.model_key,
                   V.id,
                   ...
      FROM ...
      ORDER BY FX.effective_dt DESC
      

      您拥有的另一个选项是在 where 子句中使用聚合函数和相关性。

      select C.model_key,
             V.id,
             V.category_Id, 
             V.Text1,
             V.Numeric1,
             C.Name, 
             FX.Currency_code,
             FX.Rate,
             FX.effective_dt 
      FROM aps.v_m_fxrates AS FX
          JOIN aps.t_m_value AS V on FX.Currency_code = V.Text1 
          JOIN aps.t_m_category AS C on V.Category_Id = C.Id 
      WHERE C.Name = 'FXRates' 
                AND C.Model_Key = 25
                AND FX.version = 2
                AND FX.effective_dt = 
                      (SELECT MAX(effective_dt) FROM aps.v_m_fxrates AS FX2
                       WHERE FX2.currency_code = FX.currency_code)
      

      【讨论】:

      • 您的第二个选项似乎是正确的。我的任务需要与相关子查询相关的东西。但是这对我有用。外部查询的整个结果必须进入内部查询以获取所需的结果。但是这个内部查询只会比较单个记录吗?
      • 在内部查询中,您为外部查询中的货币代码选择 max Effective_dt。因此,每个货币代码的最大有效 dt 记录将进入结果表。我认为您的问题可能出在可以过滤行的其他连接运算符中。所以尝试向内部查询添加两个表连接。
      【解决方案3】:
      select 
          c.model_key, v.id,v.category_Id, v.Numeric1,
          c.Name, fx.Currency_code, fx.Rate, fx.effective_dt 
      from 
          aps.v_m_fxrates fx 
      join 
          aps.t_m_value v on fx.Currency_code = v.Text1 
      join 
         aps.t_m_category c on v.Category_Id = c.Id 
      where 
         c.Name = 'FXRates' 
         and c.Model_Key = 25 
         and v.Text1 = fx.Currency_code 
         and fx.version = 2 
         and fx.effective_dt = (select MAX(effective_dt) from aps.v_m_fxrates)
      

      试试这个;您使用了fx.effective_dt in,但您必须使用=,因为您想要最大日期记录。使用= 后只会返回一行。

      【讨论】:

        猜你喜欢
        • 2019-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-18
        • 1970-01-01
        • 1970-01-01
        • 2018-06-17
        • 1970-01-01
        相关资源
        最近更新 更多