我有一个包含14列的表。这些列是颜色,类型,英尺,日期,计数等。我需要的是选择id和类型的所有不同记录与最新日期。所以,例如......
颜色------型-----------日期
红色--------工作----------- 01/01/01
红色--------- ----------播放02年2月2日
红色--------- ----------播放03年3月3日
在这种情况下,我想返回红色,工作,01/01/01和红色,播放03/03/03。希望这是有道理的。我尝试了不同的select unique和select distinct和group bys的组合,但我还没有想出任何东西。
这是我正在尝试的SQL语句:
select distinct
chock_id,
roll_type,
max(chock_service_dt),
chock_id_dt,
chock_seq_num,
chock_service_cmnt,
total_rolled_lineal_ft,
total_rolled_tons,
chock_usage_cnt,
chock_insert_dt,
record_modify_dt,
next_chock_service_dt_act,
previous_alarm_value,
upload_complete_yn
from
tp07_chock_summary_row
group by
chock_id,
roll_type,
chock_service_dt,
chock_id_dt,
chock_seq_num,
chock_service_cmnt,
total_rolled_lineal_ft,
total_rolled_tons,
chock_usage_cnt,
chock_insert_dt,
record_modify_dt,
next_chock_service_dt_act,
previous_alarm_value,
upload_complete_yn;
这是截图。就像我在下面的评论中所说,就像在第2行和第4行中一样,我不能拥有多个具有相同chock_id和roll_type的记录。
答案 0 :(得分:0)
select color, type, max(date)
from ...
group by color, type
答案 1 :(得分:0)
select
color,
type,
max(date)
from
yourtable
group by
color,
type
答案 2 :(得分:0)
鉴于您最初未解释的新要求,应该这样做:
select
chock_id,
roll_type,
chock_service_dt,
chock_id_dt,
chock_seq_num,
chock_service_cmnt,
total_rolled_lineal_ft,
total_rolled_tons,
chock_usage_cnt,
chock_insert_dt,
record_modify_dt,
next_chock_service_dt_act,
previous_alarm_value,
upload_complete_yn
from (
select
chock_id,
roll_type,
chock_service_dt,
chock_id_dt,
chock_seq_num,
chock_service_cmnt,
total_rolled_lineal_ft,
total_rolled_tons,
chock_usage_cnt,
chock_insert_dt,
record_modify_dt,
next_chock_service_dt_act,
previous_alarm_value,
upload_complete_yn,
row_number() over (
partition by chock_id, roll_type
order by chock_service_dt desc
) rn
from
tp07_chock_summary_row
) where rn = 1