如何从单个列创建满足多个条件的 select 语句?
How to create a select statement that satisifies multiple condition from a single column?
这里我有 3 列:ID(NUMBER)、PART_NAME(VARCHAR) 和 PART_VALUE(CLOB)(所有三列构成一个主键)。我的场景是这样的,我应该检查同一列的多个条件。
例如:对于特定的 ID,我应该检查 Different Part_Name 及其对应的 part_value。我知道下面是错误的。仅供参考,我包括
SELECT COUNT(0)
FROM MY_TABLE
WHERE ID = 'XYZ'
AND (
(
(
PART_TYPE='SW NUT Prod'
AND DBMS_LOB.COMPARE(PART_VALUE,'NUT and Non-Standard Key')=0
)
OR
(
PART_TYPE='SW NUT Prod'
AND DBMS_LOB.COMPARE(PART_VALUE,'LIMES Key and Document')=0
)
OR (
PART_TYPE='SW NUT Prod'
AND DBMS_LOB.COMPARE(PART_VALUE,'LIMES Physical Key and Document')=0
)
)
AND (
PART_TYPE='TRIM'
AND DBMS_LOB.COMPARE(PART_VALUE,'FALSE')=0
)
)
我们必须实现这一目标。我试过使用自关节。但这并没有帮助,因为我们有很大的疑问我们应该在 time.Any 处检查 10 Part_name 建议会帮助我
这些查询可能会有帮助。它将列 cnt
添加到 table 中的每一行,通知是否满足此 ID 的所有条件:
with data as (
SELECT id, part_type, part_value,
case when PART_TYPE='SW NUT Prod'
and (DBMS_LOB.COMPARE(PART_VALUE,'NUT and Non-Standard Key')=0
or DBMS_LOB.COMPARE(PART_VALUE,'LIMES Key and Document')=0
or DBMS_LOB.COMPARE(PART_VALUE,'LIMES Physical Key and Document')=0)
then 1 end c1,
case when PART_TYPE='TRIM' AND DBMS_LOB.COMPARE(PART_VALUE,'FALSE')=0
then 1 end c2
FROM MY_TABLE)
select id, part_type, part_value, case when c1 > 0 and c2 > 0 then 1 else 0 end cnt
from (
select id, part_type, part_value,
count(c1) over (partition by id) c1, count(c2) over (partition by id) c2
from data)
出于某种原因您不希望 group by
,但是您当然可以使用此子句简化输出。
如果您只对特定 id 感兴趣,请在第一个子查询中添加 where id=XYZ
。在 SQLFiddle 中我添加了第二个 ID,
并非所有条件都得到满足。
这里我有 3 列:ID(NUMBER)、PART_NAME(VARCHAR) 和 PART_VALUE(CLOB)(所有三列构成一个主键)。我的场景是这样的,我应该检查同一列的多个条件。 例如:对于特定的 ID,我应该检查 Different Part_Name 及其对应的 part_value。我知道下面是错误的。仅供参考,我包括
SELECT COUNT(0)
FROM MY_TABLE
WHERE ID = 'XYZ'
AND (
(
(
PART_TYPE='SW NUT Prod'
AND DBMS_LOB.COMPARE(PART_VALUE,'NUT and Non-Standard Key')=0
)
OR
(
PART_TYPE='SW NUT Prod'
AND DBMS_LOB.COMPARE(PART_VALUE,'LIMES Key and Document')=0
)
OR (
PART_TYPE='SW NUT Prod'
AND DBMS_LOB.COMPARE(PART_VALUE,'LIMES Physical Key and Document')=0
)
)
AND (
PART_TYPE='TRIM'
AND DBMS_LOB.COMPARE(PART_VALUE,'FALSE')=0
)
)
我们必须实现这一目标。我试过使用自关节。但这并没有帮助,因为我们有很大的疑问我们应该在 time.Any 处检查 10 Part_name 建议会帮助我
这些查询可能会有帮助。它将列 cnt
添加到 table 中的每一行,通知是否满足此 ID 的所有条件:
with data as (
SELECT id, part_type, part_value,
case when PART_TYPE='SW NUT Prod'
and (DBMS_LOB.COMPARE(PART_VALUE,'NUT and Non-Standard Key')=0
or DBMS_LOB.COMPARE(PART_VALUE,'LIMES Key and Document')=0
or DBMS_LOB.COMPARE(PART_VALUE,'LIMES Physical Key and Document')=0)
then 1 end c1,
case when PART_TYPE='TRIM' AND DBMS_LOB.COMPARE(PART_VALUE,'FALSE')=0
then 1 end c2
FROM MY_TABLE)
select id, part_type, part_value, case when c1 > 0 and c2 > 0 then 1 else 0 end cnt
from (
select id, part_type, part_value,
count(c1) over (partition by id) c1, count(c2) over (partition by id) c2
from data)
出于某种原因您不希望 group by
,但是您当然可以使用此子句简化输出。
如果您只对特定 id 感兴趣,请在第一个子查询中添加 where id=XYZ
。在 SQLFiddle 中我添加了第二个 ID,
并非所有条件都得到满足。