0

我有一个有 14 列的表。这些列是颜色、类型、ft、日期、计数等。我需要的是选择所有不同的 id 记录和最近日期的类型。所以,例如...

颜色--------类型------------日期

红色--------工作------------01/01/01

红色---------播放---------02/02/02

红色---------播放---------03/03/03

在这种情况下,我想返回红色,工作,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 的记录。

4

3 回答 3

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
于 2013-06-21T18:37:59.093 回答
0
select color, type, max(date)
from ...
group by color, type
于 2013-06-21T18:01:33.160 回答
0
select 
 color,
 type,
 max(date)
from
 yourtable
group by
 color,
 type
于 2013-06-21T18:02:22.263 回答