Pandas IS IN – 作为 Oracle SQL 查询

huangapple go评论102阅读模式
英文:

Pandas IS IN - as a Oracle SQL Query

问题

  1. SELECT
  2. testtable1.id,
  3. CASE
  4. WHEN (testtable1.tn_id IN
  5. SELECT
  6. b."customer_id"
  7. FROM
  8. cu.customer b)
  9. THEN 'Yes'
  10. ELSE 'No'
  11. END) BLACKLIST
  12. FROM
  13. testschema.testtable1
英文:
  1. SELECT
  2. testtable1.id,
  3. CASE
  4. WHEN (testtable1.tn_id IN
  5. SELECT
  6. b."customer_id"
  7. FROM
  8. cu.customer b)
  9. THEN 'Yes'
  10. ELSE 'No'
  11. END) BLACKLIST
  12. FROM
  13. 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

括号使用错误;应为

  1. SELECT testtable1.id,
  2. CASE
  3. WHEN testtable1.tn_id IN (SELECT b."customer_id" FROM cu.customer b)
  4. THEN 'Yes'
  5. ELSE 'No'
  6. END blacklist
  7. FROM testschema.testtable1

我不知道你想通过

我想对条件进行多次 "OR" 运算

表达什么意思。

英文:

Wrong usage of parenthesis; should be

  1. SELECT testtable1.id,
  2. CASE
  3. WHEN testtable1.tn_id IN (SELECT b."customer_id" FROM cu.customer b)
  4. THEN 'Yes'
  5. ELSE 'No'
  6. END blacklist
  7. 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

以下是翻译好的内容:

  1. 或者,可以这样写:
  2. SELECT testtable1.id,
  3. CASE
  4. WHEN exists (SELECT 1 FROM cu.customer b
  5. where b.customer_id=testtable1.tn_id)
  6. THEN '是'
  7. ELSE '否'
  8. END blacklist
  9. FROM testschema.testtable1
  10. 而且,我会避免在列名和表名上使用双引号,除非这些列和表在创建DDL时已被双引号括起来以区分大小写。除非在创建时对象名没有使用双引号,否则Oracle会将它们保存在数据字典中并将它们转换为大写。
英文:

Or, the same could be written as:

  1. SELECT testtable1.id,
  2. CASE
  3. WHEN exists (SELECT 1 FROM cu.customer b
  4. where b.customer_id=testtable1.tn_id)
  5. THEN 'Yes'
  6. ELSE 'No'
  7. END blacklist
  8. 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

以下是翻译好的内容:

  1. your SQL (corrected a bit):
  1. 选择 t.id,
  2. CASE t.tn_id IN( cust 中选择 CUSTOMER_ID ) THEN '是'
  3. ELSE '否'
  4. 结束 "黑名单"
  5. tbl t
  1. using LEFT JOIN
  1. 选择 t.ID, CASE Nvl(c.CUSTOMER_ID, 0) 0 THEN '否' ELSE '是' 结束 "黑名单"
  2. tbl t
  3. 左连接 cust c ON(c.CUSTOMER_ID = t.TN_ID)
  4. t.ID 排序
  1. using EXISTS
  1. 选择 t.ID, CASE EXISTS( cust 中选择 CUSTOMER_ID WHERE CUSTOMER_ID = t.TN_ID) THEN '是' ELSE '否' 结束 "黑名单"
  2. tbl t
  3. t.ID 排序

All three returns the same:

  1. ID 黑名单
  2. -- ---------
  3. A1
  4. A2
  5. A3
  6. A4
  7. A5
英文:

There are several options to do it.
Sample data:

  1. WITH
  2. tbl AS
  3. ( Select 'A1' "ID", 1 "TN_ID", 'C' "STATUS" From Dual Union All
  4. Select 'A2' "ID", 2 "TN_ID", 'A' "STATUS" From Dual Union All
  5. Select 'A3' "ID", 3 "TN_ID", 'B' "STATUS" From Dual Union All
  6. Select 'A4' "ID", 4 "TN_ID", 'B' "STATUS" From Dual Union All
  7. Select 'A5' "ID", 5 "TN_ID", 'A' "STATUS" From Dual
  8. ),
  9. cust AS
  10. ( Select 1 "CUSTOMER_ID", 'Name 1' "CUSTOMER_NAME" From Dual Union All
  11. Select 3 "CUSTOMER_ID", 'Name 3' "CUSTOMER_NAME" From Dual Union All
  12. Select 4 "CUSTOMER_ID", 'Name 4' "CUSTOMER_NAME" From Dual
  13. )
  1. your SQL (corrected a bit):
  1. SELECT t.id,
  2. CASE WHEN t.tn_id IN( SELECT CUSTOMER_ID FROM cust ) THEN 'Yes'
  3. ELSE 'No'
  4. END "BLACKLIST"
  5. FROM tbl t
  1. using LEFT JOIN
  1. SELECT t.ID, CASE Nvl(c.CUSTOMER_ID, 0) WHEN 0 THEN 'No' ELSE 'Yes' END "BLACKLIST"
  2. FROM tbl t
  3. LEFT JOIN cust c ON(c.CUSTOMER_ID = t.TN_ID)
  4. ORDER BY t.ID
  1. using EXISTS
  1. SELECT t.ID, CASE WHEN EXISTS(SELECT CUSTOMER_ID FROM cust WHERE CUSTOMER_ID = t.TN_ID) THEN 'Yes' ELSE 'No' END "BLACKLIST"
  2. FROM tbl t
  3. ORDER BY t.ID

All three returns the same:

  1. ID BLACKLIST
  2. -- ---------
  3. A1 Yes
  4. A2 No
  5. A3 Yes
  6. A4 Yes
  7. A5 No

huangapple
  • 本文由 发表于 2023年2月24日 16:20:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定