我希望有人可以帮助我。我对SQL不太好。我看过无数其他帖子,但无法弄明白。我有以下数据,需要获取最新日期。那将是09/01/1993。
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
答案 0 :(得分:2)
你可能想要最高日期,但也想要所有其他列。
您需要一个窗口聚合函数:
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
答案 1 :(得分:0)
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
答案 2 :(得分:0)
对于在同一MAX
内没有相同值的列,您需要使用id_num
函数:
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