选择一列中具有重复编号的记录,以及另一列中的唯一字符串

时间:2017-01-05 02:16:53

标签: sql database postgresql

我有一个Postgres记录表,其中包含许多带有reg_number的新Filed帐户,以及许多已使用相同reg_number完成的已归档帐户。

由于不一致,我无法按日期或行号查询。

我需要能够选择:
任何归档行 - 尚未最终确定,和 任何Finalized行,但不是他们原来的“提交”行。

来源数据表

reg_num   |  file_final   |  otherCols  
1234      |   filed       |   foo  
1234      |   final       |   foo  
1235      |   filed       |   foo  
1218      |   filed       |   foo  
1111      |   final       |   foo  
1235      |   final       |   foo  

所需选择:

reg_num   |  file_final  |  otherCols  
1234      |   final      |   foo  
1218      |   filed      |   foo  
1111      |   final      |   foo  
1235      |   final      |   foo  

我已尝试过SELECT DISTINCT ON和JOINS的多种组合,但是卡住了。
上面显示了两个相关的字段,整个表格有大约25个其他列,
我需要能够从中进行选择。

任何帮助将不胜感激。谢谢!
我尝试过的一些查询(10个以上)包括:

SELECT *
来自ca_enforce
在哪里reg_number IN

    SELECT DISTINCT ON(reg_number)reg_number
    WHERE file_final ='Final'OR file_final ='Filed'
    GROUP BY reg_number
);

另一个:

选择DISTINCT ON reg_number,
    ID,
    COL3,
    COL4,
    file_final,
    COL6,
    COL7,
    reg_number
  WHERE file_final ='最终'
  来自my_table
  ORDER BY file_final;

3 个答案:

答案 0 :(得分:0)

听起来像两个可以联合起来的查询:

--get all of the filed records that don't have final records.
select reg_num
from my_table
where reg_num not in (
  select reg_num
  from my_table
  where final_file = 'final'
)
where final_file = 'file'

union all
--get all of the final records.
select reg_num
from my_table
where file_final = 'final'

答案 1 :(得分:0)

select reg_num,file_final,othercols
from table_name
where reg_num not in (
              select reg_num
              from table_name
               where final_file = 'final'
                     )
union all
select reg_num,file_final,othercols
from table_name
where file_final = 'final'

答案 2 :(得分:0)

union技术迫使PostGreSQL在数据中传递两次。

使用reg_num列上的索引,以下查询应该执行得更快:

select reg_num, file_final, otherCols
from t t1
where file_final = 'final' or not exists (
  select *
  from t t2
  where t2.reg_num = t1.reg_num and t2.file_final = 'final');