下面是场景和表结构:
对于给定令牌,可以分配3个值
我正在尝试查询一个查询,该查询可以获取Token_Id
列中没有“草稿”值的记录Field_Value
。
从上面的图像中,我需要值1
。因为它是唯一没有draft
值的记录。
如果我触发以下查询,是否正确。
select * from cash_memo where field_value not in ('Cash')
请指导。
答案 0 :(得分:1)
这可能是一种方法:
select token_id
from yourTable
group by token_id
having count (case when field_value = 'Draft' then 1 end) = 0
在这里,我用field_value = 'Draft'
对行进行计数,只得到该计数为0的token_id
。
答案 1 :(得分:1)
一个简单的NOT EXISTS
应该可以做到:
SELECT * -- or DISTINCT token_id
FROM cash_memo
WHERE field_value <> 'draft'
AND NOT EXISTS (
SELECT 1
FROM cash_memo AS x
WHERE token_id = cash_memo.token_id
AND field_value = 'draft'
)
答案 2 :(得分:0)
您可以使用not in
select c.* from cash_memo c
where token_id not in ( select token_id
from cash_memo
where field_value = 'Draft')
答案 3 :(得分:0)
Oracle设置:
CREATE TABLE cash_memo ( token_id, field_value ) AS
SELECT 1, 'Cash' FROM DUAL UNION ALL
SELECT 2, 'Draft' FROM DUAL UNION ALL
SELECT 2, 'Cheque' FROM DUAL UNION ALL
SELECT 3, 'Cash' FROM DUAL UNION ALL
SELECT 3, 'Draft' FROM DUAL UNION ALL
SELECT 3, 'Cheque' FROM DUAL UNION ALL
SELECT 4, 'Draft' FROM DUAL UNION ALL
SELECT 4, 'Cheque' FROM DUAL UNION ALL
SELECT 5, 'Cash' FROM DUAL UNION ALL
SELECT 5, 'Cheque' FROM DUAL;
查询1 :如果您希望该行的所有列都没有'Draft'
的{{1}}值,则可以使用解析函数:
token_id
输出:
TOKEN_ID | FIELD_VALUE -------: | :---------- 1 | Cash 5 | Cash 5 | Cheque
查询2 :如果您只想要一个SELECT token_id,
field_value
FROM (
SELECT token_id,
field_value,
COUNT( CASE field_value WHEN 'Draft' THEN 1 END ) OVER ( PARTITION BY token_id )
AS numDraft
FROM cash_memo c
)
WHERE numDraft = 0;
且没有token_id
值的列表:
'Draft'
输出:
| TOKEN_ID | | -------: | | 1 | | 5 |
db <>提琴here
答案 4 :(得分:-1)
使用 FIND_IN_SET 很简单,您可以尝试
SELECT token_id FROM yourTable WHERE FIND_IN_SET('{Draft}', field_value )
此处搜索关键字{}