问题如下有一张表,如下(表名就为CASH吧)       ACCT     SUB_ACCT   MONEY
       001         001_1     50
       001         001_2      0
       002         002_1     70
       002         002_2     20
       003         003_1      0
       003         003_2      0问题:用SQL语句查询该CASH表,要求查出所有子账户(SUB_ACCT)金额(MONEY)都为0的主账户(ACCT)。

解决方案 »

  1.   

    就是以acct分组,找出money都为0的记录吧
    1.
    select acct from cash group by acct having sum(money)=0
    因为money不会有负值,可以这么写
    2.
    select distinct acct from cash a where not exists(
      select 1 from cash where money<>0 and acct=a.acct)
    3.
    select distnct acct from (
      select acct,max(money)over(partition by acct)ma,min(money)over(partition by acct)mi
      from cash)
    where ma=0 and mi=0
    4.
    select acct from(
      select acct,money,
      row_number()over(partition by acct order by money desc)rn
      from cash)
    where rn=1 and money=0
      

  2.   

    select distinct acct, money from cash where money=0;
      

  3.   

     select distinct acct from cash where money=0 and acct not in (select acct from cash where money !=0);
      

  4.   

    select acct
    from cash
    where sub_acct
    in
    (
    select sub_acct
    from cash where money=0
    )
      

  5.   

    select a.acct from (select acct,sum(money) cm from cash group by acct) a
    where cm=0;
      

  6.   

    select acct 
    from cash 
    where sub_acct in (
    select distinct sub_acct 
    from cash 
    where money=0
    );
      

  7.   


    select acct 
    from cash 
    where sub_acct in (
    select distinct sub_acct 
    from cash 
    where money=0
    );