Ser*_*rge 5 sql oracle distinct oracle11g
我有一个使用左外连接构造的查询,如下所示:
left outer JOIN GA_LOAN GA
ON LOAN.LOAN_TYPE = GA.LOAN_TYP
AND LOAN.DT = GA.GUARANTY_DT
AND LOAN.FFEL_DUP_ID = GA.SEP_LOAN_IND
AND LOAN.SCH_BR_CODE = GA.ORIG_SCHL_CD
AND STU.CURR_SSN = GA.STU_SSN
AND STU.DOB = GA.DOB
and stu.curr_fst = ga.stu_first_nam
--and (plus_bor.curr_ssn is not distinct from ga.plus_brwr_ssn )
Run Code Online (Sandbox Code Playgroud)
当我添加注释掉的行时,出现以下错误。
ORA-00908: missing NULL keyword
00908. 00000 - "missing NULL keyword"
*Cause:
*Action:
Run Code Online (Sandbox Code Playgroud)
与 DB2 中这种结构的正常工作没有什么不同,但 Oracle 给我带来了问题。有什么建议么?
is not distinct from如果我用 a替换,我不会收到任何错误=,但这在逻辑上是不一样的。
is not distinct from如果两个值都是 ,则 with 给出匹配项null,而 as=在这种情况下不匹配。
IS DISTINCT FROM您可以通过NOT EXISTS结合使用来模拟INTERSECT:
plus_bor.curr_ssn IS DISTINCT FROM ga.plus_brwr_ssn
<=>
NOT EXISTS (SELECT plus_bor.curr_ssn FROM dual INTERSECT
SELECT ga.plus_brwr_ssn FROM dual);
Run Code Online (Sandbox Code Playgroud)
例子:
WITH cte(a,b) AS (
SELECT 1, NULL FROM dual UNION ALL
SELECT 1,2 FROM dual UNION ALL
SELECT 1,1 FROM dual UNION ALL
SELECT NULL, 1 FROM dual UNION ALL
SELECT NULL, NULL FROM dual
)
SELECT *
FROM cte
WHERE NOT EXISTS (SELECT a FROM dual INTERSECT
SELECT b FROM dual)
Run Code Online (Sandbox Code Playgroud)
输出:
A B
------------
1 NULL
1 2
NULL 1
Run Code Online (Sandbox Code Playgroud)
在你的情况下IS NOT DISTINCT FROM很简单EXISTS:
plus_bor.curr_ssn IS NOT DISTINCT FROM ga.plus_brwr_ssn
<=>
EXISTS (SELECT plus_bor.curr_ssn FROM dual INTERSECT
SELECT ga.plus_brwr_ssn FROM dual);
Run Code Online (Sandbox Code Playgroud)
例子:
WITH cte(a,b) AS (
SELECT 1, NULL FROM dual UNION ALL
SELECT 1,2 FROM dual UNION ALL
SELECT 1,1 FROM dual UNION ALL
SELECT NULL, 1 FROM dual UNION ALL
SELECT NULL, NULL FROM dual
)
SELECT *
FROM cte
WHERE EXISTS (SELECT a FROM dual INTERSECT
SELECT b FROM dual);
Run Code Online (Sandbox Code Playgroud)
输出:
A B
1 1
NULL NULL
Run Code Online (Sandbox Code Playgroud)
与评论中提出的方法相比,这种方法有一个很大的优势COALESCE/NVL。您不必考虑依赖于数据类型的默认中性值。
例如,如果列是数据类型//DATE那么你必须编写如下内容:INTTEXT
coalesce(col1,DATE '1900-01-01') = coalesce(col2,DATE '1900-01-01')
coalesce(col1, 0) = coalesce(col2, 0)
coalesce(col1, ' ') = coalesce(col2, ' ')
Run Code Online (Sandbox Code Playgroud)
当然,碰撞的可能性很小。例如:
coalesce(col1, 0) = coalesce(col2, 0)
=>
col1 = NULL
col2 = 0
and we have incorrect match!!!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1268 次 |
| 最近记录: |