我有这些ID,我总是以这种格式获取它们:
538693,538693,541616,541616,541616,541620,541116,541116 ,538639,538639,538720,538720,538720,541608,541608, 541608,538675,538675,538675,538675
然后我查询哪些返回我的一些,例如返回我最后3个ID。 现在我需要从第一个记录集中选择剩余的ID而不是最后一个。
这只是示例,我有超过200行的这个ID ..
答案 0 :(得分:1)
这是一个可能会给你一个想法的例子。看看。
SQL> with
2 -- a long list of (duplicate?) IDs
3 all_ids (col) as
4 (select '538693 , 538693 , 541616 , 541616 , 541616 , 541620 , 541116 , 541116 , 538639 , 538639' from dual
5 ),
6 -- that's what you get from your query
7 do_query (col) as
8 (select '541116 , 538639 , 538639' from dual
9 ),
10 -- split ALL_IDs to rows
11 all_1 as
12 (select regexp_substr(col, '[^ , ]+', 1, level) col
13 from all_ids
14 connect by level <= regexp_count(col, ',') + 1
15 ),
16 -- split IDs from DO_QUERY to rows as well
17 do_1 as
18 (select regexp_substr(col, '[^ , ]+', 1, level) col
19 from do_query
20 connect by level <= regexp_count(col, ',') + 1
21 ),
22 -- MINUS set operator will return IDs from ALL_IDS that aren't in DO_QUERY IDs
23 minus_me as
24 (select col From all_1
25 minus
26 select col from do_1
27 )
28 -- finally, compose the remaining **unique** IDs back
29 select listagg(col, ' , ') within group (order by col) result
30 from minus_me;
RESULT
--------------------------------------------------------------------------------
538693 , 541616 , 541620
SQL>
答案 1 :(得分:1)
使用xmltable
代码
SELECT *
FROM (
SELECT to_number(column_value) AS n
FROM XMLTABLE('538693 , 538693 , 541616 , 541616 , 541616 , 541620 , 541116 ,
541116 , 538639 , 538639 , 538720 , 538720 , 538720 , 541608 ,
541608 , 541608 , 538675 , 538675 , 538675 , 538675')
) o
WHERE n NOT IN (
select to_number(column_value)
FROM xmltable(
'538675
,538675
,538675')
);
答案 2 :(得分:0)
我没有oracle试用,但可能会有效。
EG。如果您有ID 1,2,3 ... 10,则下面的查询将返回1,2,3,... 7
SELECT ID FROM TableName WHERE ID NOT IN
(SELECT ID FROM TableName WHERE ROWNUM < 3)
不确定语法,只需尝试逻辑:)
答案 3 :(得分:0)
这是另一种可能的解决方案w / out将输出转换为字符串。看看你的情况是否可以接受。这会将您的行转换为列。然后很容易消除最后3行:
select distinct column_value from table(sys.odcinumberlist(1,1,2,3,3,4,4,5));