【问题标题】:Joining two tables on historical date在历史日期连接两个表
【发布时间】:2015-06-29 03:38:50
【问题描述】:

我有两张桌子。第一个:

col1 | col2 | ColumnOfInterest | DateOfInterest
--------------------------------------------------------
abc  | def  |       ghi        | 2013-02-24 17:48:32.548
.
.
.

第二个:

ColumnOfInterest | DateChanged             | col3 | col4
--------------------------------------------------------
       ghi       | 2012-08-13 06:28:11.092 |  jkl | mno
       ghi       | 2012-10-16 23:54:07.613 |  pqr | stu
       ghi       | 2013-01-29 14:13:18.502 |  vwx | yz1
       ghi       | 2013-10-01 14:17:32.992 |  234 | 567
.
.
.

我要做的是在 ColumnOfInterest 上的两个表之间进行 1:1 连接,以便 DateOfInterest 反映第二个表中的日期。

也就是说,第一个表的行将连接到第二个表的第三行。

你有什么想法吗?

谢谢

【问题讨论】:

  • 所以您试图从 DateChanged 列中为每个不同的 ColumnOfInterest 获取最大日期?
  • 嗯,不是最大日期...您可以看到第二个表是一种历史表,所以我需要一个与第一个表的 DateOfInterest 相关的行...希望有意义
  • 你想要的输出是什么?
  • 基本上两个表中的所有列,没有重复我加入的列
  • 例如,第二个表是价格变化及其生效日期的列表,您希望在第一个表中找到对特定日期有效的价格。或者,换一种说法,对于给定的DateOfInterest,您希望DateChanged 匹配或紧接在它之前,对吧?提示:使用适当的软件(MySQL、Oracle、DB2 等)和版本标记数据库问题很有帮助,例如sql-server-2014。语法和功能的差异通常会影响答案。

标签: tsql join sql-server-2012


【解决方案1】:
select table1.ColumnOfInterest, max(table2.DateChanged) 
  from table1 
  join table2 
    on table1.ColumnOfInterest = table1.ColumnOfInterest 
   and table1.CDateOfInterest >= table2.DateChanged 
 group by table1.ColumnOfInterest

【讨论】:

    【解决方案2】:
    SELECT  'abc' col1,
            'def' col2, 
            'ghi' ColumnOfInterest,
            CAST('2013-02-24 17:48:32.548' AS DATETIME) DateOfInterest
    INTO #DateOfInterest
    
    
    CREATE TABLE #History 
    (
    ColumnOfInterest VARCHAR(5), 
    DateChanged DATETIME,
    col3 VARCHAR(5), 
    col4 VARCHAR(5)
    )
    INSERT INTO #History
    VALUES  ('ghi','2012-08-13 06:28:11.092','jkl','mno'),
            ('ghi','2012-10-16 23:54:07.613','pqr','stu'),
            ('ghi','2013-01-29 14:13:18.502','vwx','yz1'),
            ('ghi','2013-10-01 14:17:32.992','234','567');
    
    ;WITH CTE_Date_Ranges
    AS
    (
    SELECT  ColumnOfInterest,
            DateChanged,
            LAG(DateChanged,1,GETDATE()) OVER (PARTITION BY ColumnOfInterest ORDER BY DateChanged) AS end_date,
            col3,
            col4
    FROM #History
    )
    
    SELECT  B.*,
            A.*
    FROM CTE_Date_Ranges A
    INNER JOIN #DateOfInterest B
    ON B.DateOfInterest > A.DateChanged AND B.DateOfInterest < A.end_date
    

    结果:

    col1 col2 ColumnOfInterest DateOfInterest          ColumnOfInterest DateChanged             end_date                col3  col4
    ---- ---- ---------------- ----------------------- ---------------- ----------------------- ----------------------- ----- -----
    abc  def  ghi              2013-02-24 17:48:32.547 ghi              2012-08-13 06:28:11.093 2015-04-21 18:46:46.967 jkl   mno
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-20
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      相关资源
      最近更新 更多