英文:
Pandas IS IN - as a Oracle SQL Query
问题
SELECT
testtable1.id,
CASE
WHEN (testtable1.tn_id IN
SELECT
b."customer_id"
FROM
cu.customer b)
THEN 'Yes'
ELSE 'No'
END) BLACKLIST
FROM
testschema.testtable1
英文:
SELECT
testtable1.id,
CASE
WHEN (testtable1.tn_id IN
SELECT
b."customer_id"
FROM
cu.customer b)
THEN 'Yes'
ELSE 'No'
END) BLACKLIST
FROM
testschema.testtable1
It should check if the value is found in the other table. And then give a output "Yes" or "No" if its found or not.
I want to do this for conditions so there will be multiple "OR".
Thanks in advance.
答案1
得分: 2
括号使用错误;应为
SELECT testtable1.id,
CASE
WHEN testtable1.tn_id IN (SELECT b."customer_id" FROM cu.customer b)
THEN 'Yes'
ELSE 'No'
END blacklist
FROM testschema.testtable1
我不知道你想通过
我想对条件进行多次 "OR" 运算
表达什么意思。
英文:
Wrong usage of parenthesis; should be
SELECT testtable1.id,
CASE
WHEN testtable1.tn_id IN (SELECT b."customer_id" FROM cu.customer b)
THEN 'Yes'
ELSE 'No'
END blacklist
FROM testschema.testtable1
I don't know what you meant to say by
> I want to do this for conditions so there will be multiple "OR"
答案2
得分: 2
以下是翻译好的内容:
或者,可以这样写:
SELECT testtable1.id,
CASE
WHEN exists (SELECT 1 FROM cu.customer b
where b.customer_id=testtable1.tn_id)
THEN '是'
ELSE '否'
END blacklist
FROM testschema.testtable1
而且,我会避免在列名和表名上使用双引号,除非这些列和表在创建DDL时已被双引号括起来以区分大小写。除非在创建时对象名没有使用双引号,否则Oracle会将它们保存在数据字典中并将它们转换为大写。
英文:
Or, the same could be written as:
SELECT testtable1.id,
CASE
WHEN exists (SELECT 1 FROM cu.customer b
where b.customer_id=testtable1.tn_id)
THEN 'Yes'
ELSE 'No'
END blacklist
FROM testschema.testtable1
And I'd avoid double quoting column names and table names, that is unless those columns and tables have been created in Oracle to be case-sensitive by double quoting the name in the create DDL. Unless object names are not double quoted on creation, Oracle has them in the data dictionary as uppercase.
答案3
得分: 2
以下是翻译好的内容:
- your SQL (corrected a bit):
选择 t.id,
CASE 当 t.tn_id IN( 从 cust 中选择 CUSTOMER_ID ) THEN '是'
ELSE '否'
结束 "黑名单"
从 tbl t
- using LEFT JOIN
选择 t.ID, CASE Nvl(c.CUSTOMER_ID, 0) 当 0 THEN '否' ELSE '是' 结束 "黑名单"
从 tbl t
左连接 cust c ON(c.CUSTOMER_ID = t.TN_ID)
按 t.ID 排序
- using EXISTS
选择 t.ID, CASE 当 EXISTS(从 cust 中选择 CUSTOMER_ID WHERE CUSTOMER_ID = t.TN_ID) THEN '是' ELSE '否' 结束 "黑名单"
从 tbl t
按 t.ID 排序
All three returns the same:
ID 黑名单
-- ---------
A1 是
A2 否
A3 是
A4 是
A5 否
英文:
There are several options to do it.
Sample data:
WITH
tbl AS
( Select 'A1' "ID", 1 "TN_ID", 'C' "STATUS" From Dual Union All
Select 'A2' "ID", 2 "TN_ID", 'A' "STATUS" From Dual Union All
Select 'A3' "ID", 3 "TN_ID", 'B' "STATUS" From Dual Union All
Select 'A4' "ID", 4 "TN_ID", 'B' "STATUS" From Dual Union All
Select 'A5' "ID", 5 "TN_ID", 'A' "STATUS" From Dual
),
cust AS
( Select 1 "CUSTOMER_ID", 'Name 1' "CUSTOMER_NAME" From Dual Union All
Select 3 "CUSTOMER_ID", 'Name 3' "CUSTOMER_NAME" From Dual Union All
Select 4 "CUSTOMER_ID", 'Name 4' "CUSTOMER_NAME" From Dual
)
- your SQL (corrected a bit):
SELECT t.id,
CASE WHEN t.tn_id IN( SELECT CUSTOMER_ID FROM cust ) THEN 'Yes'
ELSE 'No'
END "BLACKLIST"
FROM tbl t
- using LEFT JOIN
SELECT t.ID, CASE Nvl(c.CUSTOMER_ID, 0) WHEN 0 THEN 'No' ELSE 'Yes' END "BLACKLIST"
FROM tbl t
LEFT JOIN cust c ON(c.CUSTOMER_ID = t.TN_ID)
ORDER BY t.ID
- using EXISTS
SELECT t.ID, CASE WHEN EXISTS(SELECT CUSTOMER_ID FROM cust WHERE CUSTOMER_ID = t.TN_ID) THEN 'Yes' ELSE 'No' END "BLACKLIST"
FROM tbl t
ORDER BY t.ID
All three returns the same:
ID BLACKLIST
-- ---------
A1 Yes
A2 No
A3 Yes
A4 Yes
A5 No
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论