Pandas IS IN – 作为 Oracle SQL 查询

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

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

以下是翻译好的内容:

  1. your SQL (corrected a bit):
选择	t.id,
        CASE  	 t.tn_id IN(  cust 中选择 CUSTOMER_ID ) THEN '是'
        ELSE '否'
        结束 "黑名单"
	tbl t
  1. 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 排序
  1. 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 
		)
  1. 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
  1. 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
  1. 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     

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:

确定