【问题标题】:SQL Get Most Recent DateSQL 获取最近的日期
【发布时间】:2015-05-22 04:58:43
【问题描述】:

我希望有人可以帮助我。我不太擅长 SQL。我看过无数其他帖子,只是无法弄清楚。我有以下数据,需要获取最近的日期。那将是 1993 年 9 月 1 日。

524 | David | NULL | 1991 | 01 | H | 1991-07-01 00:00:00.000

524 | David | NULL | 1992 | 01 | H | 1992-07-01 00:00:00.000

524 | David | NULL | 1993 | 09 | H | 1993-09-01 00:00:00.000

我尝试了以下查询,但没有返回任何结果。谁能告诉我我做错了什么?

SELECT student_crs_hist.id_num,   
     name_format_view.last_first_middle_suf,   
     year_term_table.pesc_session_type,   
     student_crs_hist.yr_cde,   
     student_crs_hist.trm_cde, 
     student_crs_hist.TRANSACTION_STS,
     year_term_table.TRM_BEGIN_DTE
   FROM student_crs_hist,   
     name_format_view,   
     year_term_table
   WHERE  
   student_crs_hist.id_num = name_format_view.id_num
     and student_crs_hist.yr_cde = year_term_table.yr_cde 
     and student_crs_hist.trm_cde = year_term_table.trm_cde 
     and student_crs_hist.TRANSACTION_STS <> 'D' 
     and student_crs_hist.id_num = 524
     and year_term_table.TRM_BEGIN_DTE = (select max(year_term_table.TRM_BEGIN_DTE) from year_term_table)
Group By
     student_crs_hist.id_num,   
     name_format_view.last_first_middle_suf,   
     year_term_table.pesc_session_type,   
     student_crs_hist.yr_cde,   
     student_crs_hist.trm_cde, 
     student_crs_hist.TRANSACTION_STS, 
     year_term_table.TRM_BEGIN_DTE

【问题讨论】:

    标签: sql sql-server max


    【解决方案1】:

    您可能想要最高日期,但也需要所有其他列。

    为此,您需要一个窗口聚合函数:

    SELECT *
    FROM 
     (
       SELECT student_crs_hist.id_num,   
            name_format_view.last_first_middle_suf,   
            year_term_table.pesc_session_type,   
            student_crs_hist.yr_cde,   
            student_crs_hist.trm_cde, 
            student_crs_hist.TRANSACTION_STS,
            year_term_table.TRM_BEGIN_DTE,
        -- group maximum = maximum date per id_num
            MAX(TRM_BEGIN_DTE) OVER (PARTITION BY student_crs_hist.id_num) AS maxDate
          FROM student_crs_hist,   
            name_format_view,   
            year_term_table
          WHERE  
          student_crs_hist.id_num = name_format_view.id_num
            AND student_crs_hist.yr_cde = year_term_table.yr_cde 
            AND student_crs_hist.trm_cde = year_term_table.trm_cde 
            AND student_crs_hist.TRANSACTION_STS <> 'D' 
            AND student_crs_hist.id_num = 524
     ) AS dt
    WHERE TRM_BEGIN_DTE = maxDate  -- only the rows with the maximum date
    

    【讨论】:

      【解决方案2】:
          SELECT student_crs_hist.id_num,   
           name_format_view.last_first_middle_suf,   
           year_term_table.pesc_session_type,   
           student_crs_hist.yr_cde,   
           student_crs_hist.trm_cde, 
           student_crs_hist.TRANSACTION_STS,
           year_term_table.TRM_BEGIN_DTE
         FROM student_crs_hist,   
           name_format_view,   
           year_term_table
         JOIN (
             SELECT student_crs_hist.id_num, MAX(year_term_table.TRM_BEGIN_DTE) AS MaxDate
             FROM student_crs_hist,   
               name_format_view,   
               year_term_table
             WHERE  
             student_crs_hist.id_num = name_format_view.id_num
               and student_crs_hist.yr_cde = year_term_table.yr_cde 
               and student_crs_hist.trm_cde = year_term_table.trm_cde 
               and student_crs_hist.TRANSACTION_STS <> 'D' 
               and student_crs_hist.id_num = 524
               and year_term_table.TRM_BEGIN_DTE = (select max(year_term_table.TRM_BEGIN_DTE) from year_term_table)
             GROUP BY student_crs_hist.id_num
         ) MaxDateJoin ON student_crs_hist.id_num = MaxDateJoin.id_num AND year_term_table.TRM_BEGIN_DT = MaxDateJoin.MaxDate
         WHERE  
         student_crs_hist.id_num = name_format_view.id_num
           and student_crs_hist.yr_cde = year_term_table.yr_cde 
           and student_crs_hist.trm_cde = year_term_table.trm_cde 
           and student_crs_hist.TRANSACTION_STS <> 'D' 
           and student_crs_hist.id_num = 524
           and year_term_table.TRM_BEGIN_DTE = (select max(year_term_table.TRM_BEGIN_DTE) from year_term_table)
      Group By
           student_crs_hist.id_num,   
           name_format_view.last_first_middle_suf,   
           year_term_table.pesc_session_type,   
           student_crs_hist.yr_cde,   
           student_crs_hist.trm_cde, 
           student_crs_hist.TRANSACTION_STS, 
           year_term_table.TRM_BEGIN_DTE
      

      【讨论】:

      • 我试图通过在 select 语句中执行 max(year_term_table.TRM_BEGIN_DTE) 将其合并到我的查询中,但它会带回所有记录。我的其他 SQL 是否有问题?
      • 我认为问题是您按 year_term_table.TRM_BEGIN_DTE 分组,因此它将返回每个日期的记录。尝试更新版本,它应该返回每个 id_num 的一条记录和最大日期。
      【解决方案3】:

      对于在同一 id_num 中没有相同值的列,您需要使用 MAX 函数:

      SELECT student_crs_hist.id_num
          ,name_format_view.last_first_middle_suf
          ,year_term_table.pesc_session_type
          ,max(student_crs_hist.yr_cde)
          ,max(student_crs_hist.trm_cde)
          ,student_crs_hist.TRANSACTION_STS
          ,max(year_term_table.TRM_BEGIN_DTE)
      FROM student_crs_hist
          ,name_format_view
          ,year_term_table
      WHERE student_crs_hist.id_num = name_format_view.id_num
          AND student_crs_hist.yr_cde = year_term_table.yr_cde
          AND student_crs_hist.trm_cde = year_term_table.trm_cde
          AND student_crs_hist.TRANSACTION_STS <> 'D'
          AND student_crs_hist.id_num = 524
          AND year_term_table.TRM_BEGIN_DTE = (
              SELECT max(year_term_table.TRM_BEGIN_DTE)
              FROM year_term_table
              )
      GROUP BY student_crs_hist.id_num
          ,name_format_view.last_first_middle_suf
          ,year_term_table.pesc_session_type
          ,student_crs_hist.TRANSACTION_STS
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-22
        • 1970-01-01
        相关资源
        最近更新 更多