如何修复Oracle中的“ ORA-00904”错误?

时间:2018-12-26 19:12:02

标签: sql oracle

嗨,我正在尝试在SQL Server中编写代码,但是服务器说存在“ ORA-00904”错误。 我必须使用SELECT来显示不在哈里森和伍德赛德居住并且帐户中有500多个帐户的客户的姓名和城市。

这是帐户表。

ACCOUNT_NUMBER   BRANCH_NAME   BALANCE
         A-101      Downtown       500
         A-102    Perryridge       400
         A-201      Brighton       900
         A-215        Mianus       700
         A-217      Brighton       750
         A-222       Redwood       700
         A-305    Round Hill       350

这是存款表。

CUSTOMER_NAME   ACCOUNT_NUMBER
        Hayes            A-102 
      Johnson            A-101
      Johnson            A-201
        Jones            A-217
      Lindsay            A-222
        Smith            A-215
       Turner            A-305 

这是客户表。

CUSTOMER_NAME   CUSTOMER_STREET   CUSTOMER_CITY
        Adams            Spring      Pittsfield
       Brooks           Senator        Brooklyn
        Curry             North             Rye
        Glenn         Sand Hill        Woodside
        Green            Walnut        Stamford
        Hayes              Main        Harrison
      Johnson              Alma       Palo Alto
        Jones              Main        Harrison
      Lindsay              Park      Pittsfield
        Smith             North             Rye
       Turner            Putnam        Stamford
     Williams            Nassau       Princeton

我已经尝试过下面的代码,并且我猜别名有问题,但是我不知道为什么。

SELECT customer_name, 
       customer_city 
FROM   (SELECT * 
        FROM   (SELECT * 
                FROM   depositor X 
                       full OUTER JOIN (SELECT * 
                                        FROM   customer Y 
                                        WHERE  NOT customer_city = 'Harrison' 
                                               AND NOT customer_city = 
                                                       'Woodside') 
                                    ON X.customer_name = y.customer_name) Z 
               full OUTER JOIN (SELECT * 
                                FROM   account W) 
                            ON Z.account_num = w.account_num) 
WHERE  balance >= 500; 

3 个答案:

答案 0 :(得分:2)

如果我正确理解了您的问题,则不需要所有带有full outer joins的子查询。您可以只使用常规的joins并在sum上进行汇总:

select c.customer_name, c.customer_city
from customer c
    join depositor d on c.customer_name = d.customer_name
    join account a on d.account_number = a.account_number
where c.customer_city not in ('Harrison','Woodside')
group by c.customer_name, c.customer_city
having sum(a.balance) > 500

一些注意事项:我建议使用customer_id作为primary key而不是customer_name。诸如johnson之类的重复名称可能会导致问题和错误的结果。而且看起来depositor表是带有1-n表的customer表。这就是为什么它使用带有having而不是sum条件的where语句的原因。

答案 1 :(得分:1)

  

我已经尝试了以下代码,但我猜别名存在问题   但我不知道为什么。

纠正错误,确实缺少别名。

SELECT customer_name, 
       customer_city 
FROM   (SELECT * 
        FROM   (SELECT * 
                FROM   depositor X 
                       full OUTER JOIN (SELECT * 
                                        FROM   customer Y 
                                        WHERE  NOT customer_city = 'Harrison' 
                                               AND NOT customer_city = 
                                                       'Woodside') 
                                    ON X.customer_name = y.customer_name) Z 
               full OUTER JOIN (SELECT * 
                                FROM   account W) AS alias # here it was missing
               ON Z.account_num = w.account_num) AS alias # here it was missing
WHERE  balance >= 500; 

答案 2 :(得分:0)

了解为什么查询的联接是如此复杂。可以尝试以下查询

     select c.customer_name,c.customer_city from customer c
      where 
       c.customer_city not in 
      ( 'Woodside','Harrison' )
       and c.customer_name in 
     (Select d.customer_name
       from depositor d where 
     d.account_number in
    (Select a.account_number from account a where
     a.balance >=500)
     )

上面的查询可能会有所帮助,因为它简短且不包含连接