SQL获取最近日期

SQL获取最近日期,sql,sql-server,max,Sql,Sql Server,Max,我希望有人能帮助我。我不太擅长SQL。我看过无数其他的帖子,就是想不出来。我有以下数据,需要获得最近的日期。那将是1993年1月9日 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.00

我希望有人能帮助我。我不太擅长SQL。我看过无数其他的帖子,就是想不出来。我有以下数据,需要获得最近的日期。那将是1993年1月9日

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

您可能想要最高的日期,但所有其他列也一样

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

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
对于同一id_num中没有相同值的列,需要使用MAX函数:


我试图通过在select语句中执行maxyear\u term\u table.TRM\u BEGIN\u DTE将其合并到查询中,但它会返回所有记录。我的SQL的其余部分是否有问题?我认为问题在于您是按年份\u term\u table.TRM\u BEGIN\u DTE进行分组的,因此它将返回每个日期的记录。尝试更新的版本,该版本应返回每个id_num的一条记录以及最大日期。
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
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