Oracle SQL - 不在子查询中

时间:2015-05-27 22:40:18

标签: sql oracle

我对此并不熟悉并使用Oracle SQL。我有以下表格:

表1 CaseDetail

CaseNumber | CaseType
1          | 'RelevantToThisQuestion'
2          | 'RelevantToThisQuestion'
3          | 'RelevantToThisQuestion'
4          | 'NotRelevantToThisQuestion'

表2 LinkedPeople

CaseNumber | RelationshipType | LinkedPerson
1          | 'Owner'          | 123
1          | 'Agent'          | 124
1          | 'Contact'        | 125
2          | 'Owner'          | 126
2          | 'Agent'          | 127
2          | 'Contact'        | 128
3          | 'Owner'          | 129
3          | 'Agent'          | 130
3          | 'Contact'        | 131

表3 位置

LinkedPerson| Country
123         | 'AU'
124         | 'UK'
125         | 'UK'
126         | 'US'
127         | 'US'
128         | 'UK'
129         | 'UK'
130         | 'AU'
131         | 'UK'

我想计算与此问题相关的CaseNumbers,而'AU'中没有LinkedPeople。因此,上述数据的结果将是1

我一直在尝试将聚合函数和子查询结合起来,但我想我可能会过度复杂化。

只需要朝着正确的方向努力,谢谢!

3 个答案:

答案 0 :(得分:1)

demo

Oracle 11g R2架构设置

SELECT COUNT( DISTINCT CASENUMBER ) AS Num_Relevant_Cases
FROM   CASEDETAIL c
WHERE  CaseType = 'RelevantToThisQuestion'
AND    NOT EXISTS ( SELECT 1
                    FROM   LINKEDPEOPLE p
                           INNER JOIN LOCATION l
                           ON ( p.LinkedPerson = l.LinkedPerson )
                    WHERE  c.CaseNumber = p.CaseNumber
                    AND    l.Country = 'AU' )

查询1

| NUM_RELEVANT_CASES |
|--------------------|
|                  1 |

<强> SQL Fiddle

#ifndef FORMULAS_H_INCLUDED
#define FORMULAS_H_INCLUDED

float a;
float Pi=3.1415926536;
float r;
float b;
float c;
float h;




char formula_volume_cube[100] = "Formula for the Volume of a Cube: V=a*a*a";
const float volume_cube=(a*a*a);



#endif // FORMULAS_H_INCLUDED

答案 1 :(得分:0)

我不确定确切的语法,但我相信你想要的东西:

select count distinct CaseNumber from LinkedPeople where Country != 'AU'

答案 2 :(得分:0)

获取所有记录:

SELECT COUNT(DISTINCT CaseNumber) 
FROM LinkedPeople
WHERE CaseNumber NOT IN 
(
  SELECT DISTINCT C.CaseNumber
  FROM CaseDetail C       
  INNER JOIN LinkedPeople P ON C.CaseNumber = P.CaseNumber
  INNER JOIN Location L
  ON P.LinkedPerson = L.LinkedPerson
  WHERE Country = 'AU' AND C.CaseType = 'RelevantToThisQuestion'
)