oracle查找一个或多个值

时间:2017-06-20 08:02:34

标签: sql oracle

我必须找到仍然是客户的客户(客户=开设了一个或多个帐户'O',即使他们已关闭帐户'C'

如果他们关闭了所有帐户,则不再是客户端)。所以我有两张桌子,一张是顾客,另一张是帐户。如何加入或选择正确选择?

CUSTOMER_NO| FULL_NAME| ACC_ISOPEN| COUNT(ACC_ISOPEN)
0114115| DAESCU ANCUTA-IOANA|   C|  2
0114115| DAESCU ANCUTA-IOANA|   O|  2
0141677| LINCAN CLAUDIA|    C|  2
0141677| LINCAN CLAUDIA|    O|  8
0149469| VOICU ANDRE GEORGE|    O|  2
0158605| CSORTAN BARNA| C|  15

所以,前三个人是客户,但最后一个人刚刚关闭账户,这意味着不再是一个活跃的客户。

怎么做?

客户表

CUSTOMER_NO FULL_NAME
0114115 DAESCU ANCUTA-IOANA
0141677 LINCAN CLAUDIA
0149469 VOICU ANDRE GEORGE
0158605 CSORTAN BARNA

帐户表

CUSTOMER_NO ACCOUNT_NO  ACC_ISOPEN
0114115 B13RONECON011411501 C
0114115 005EURP77Z011411501 C
0114115 005EURCRT0011411501 O
0114115 005RONCRT0011411501 O
0141677 008RONP09L014167701 O
0141677 008EURP111014167701 O
0141677 008RONP06L014167702 O
0141677 008USDCRT0014167701 O
0141677 008EURCRT0014167701 O
0141677 008RONP111014167701 O
0141677 008EURP03L014167701 O
0141677 008RONCRT0014167701 O
0141677 008RONP111014167702 C
0141677 008RONP06L014167701 C
0149469 058RONCRT0014946901 O
0149469 058EURCRT0014946901 O
0158605 008RON001L015860503 C
0158605 008RON0111015860503 C
0158605 008RON0111015860501 C
0158605 008RONP111015860502 C
0158605 A08RONECON015860501 C
0158605 008RON0111015860502 C
0158605 008RONP210015860501 C
0158605 008RONP09L015860501 C
0158605 008RON0210015860501 C
0158605 008RON012L015860502 C
0158605 008RON001L015860501 C
0158605 008RONP111015860501 C
0158605 008RON001L015860502 C
0158605 008RON012L015860501 C
0158605 008RON006L015860501 C

3 个答案:

答案 0 :(得分:0)

这应该通过每个表的单个表扫描来完成:

SELECT c.CUSTOMER_NO,
       MAX( c.FULL_NAME ) AS FULL_NAME,
       a.ACC_ISOPEN,
       COUNT( 1 ) As num_accounts
FROM   Customers c
       INNER JOIN (
         SELECT a.*,
                COUNT( CASE acc_isopen WHEN 'O' THEN 1 END )
                  OVER ( PARTITION BY customer_no ) AS num_open
         FROM   accounts a
       ) a
       ON (   c.customer_no = a.customer_no
          AND a.num_open    > 0 )
GROUP BY
       c.CUSTOMER_NO,
       a.ACC_ISOPEN
  

我想要显示名称,如果是客户则选择customer_no和'Y',如果不是,则为'N'......

SELECT c.CUSTOMER_NO,
       MAX( c.FULL_NAME ) AS FULL_NAME,
       CASE
         WHEN COUNT( CASE acc_isopen WHEN 'O' THEN 1 END ) > 0
         THEN 'Y'
         ELSE 'N'
       END AS has_open_account
FROM   Customers c
       INNER JOIN accounts a
       ON (   c.customer_no = a.customer_no )
GROUP BY
       c.CUSTOMER_NO

答案 1 :(得分:0)

SELECT DISTINCT x.customer_no, y.full_name, x.acc_isopen,
                COUNT (DECODE (acc_isopen, 'O', 1, 0)) 
                OVER (PARTITION BY x.customer_no, y.full_name ORDER BY x.acc_isopen) cnt
           FROM ACCOUNT_TABLE x JOIN CUSTOMER_TABLE y ON (x.customer_no = y.customer_no)
          WHERE acc_isopen <> 'C'

答案 2 :(得分:0)

也许不是编写查询的最佳方式,但可以理解:

select c.FULL_NAME , 'Y'
     from CUSTOMERS c 
      where c.CUSTOMER_NO in (select distinct CUSTOMER_NO
                    from ACCOUNTS 
                   where ACC_ISOPEN = 'O')
     -- the query above returns all the customers with at least one open account
     union 
     select c.FULL_NAME , 'N'
     from 
     CUSTOMERS c 
     where c.CUSTOMER_NO in 
                (   select CUSTOMER_NO from CUSTOMERS 
                    --here from all customers with are putting out from the result all customers with at least one open account (we return customers with no open account)
                     minus 
                    (select distinct CUSTOMER_NO
                    from ACCOUNTS 
                   where ACC_ISOPEN = 'O') 
                );