比较两个SQL查询的输出?

时间:2017-10-25 04:09:35

标签: mysql sql aggregate-functions

我在使用复杂的SQL查询时遇到了一些麻烦,我将不胜感激。我已经简化了表格&&名。

SCHEMA:   

  Business Account
    payment_account_id
    business_id

  Business Misc
    business_user_id
    business_id

  Payment Account
    business_user_id
    payment_account_id

我需要一个SQL查询,该查询包含所有拥有多个付款帐户的商家,所有拥有多个付款帐户的商家用户,并在这些付款帐户具有相同ID时重叠。

到目前为止,我已经得到了:

business_with_multiple_accounts = 

select * 
from "Business Account" 
group by business_id 
having count(distinct payment_account_id) > 1;

users_with_multiple_accounts =

select * 
from "Payment Account" 
group by business_user_id 
having count(distinct payment_account_id) > 1;

我想我只需要比较这两个输出(但我可能错了!)但我不确定如何继续。我尝试以各种方式嵌套查询,以及在三向连接中使用“Business Misc”表。

感谢您提供任何帮助!

1 个答案:

答案 0 :(得分:0)

请试试这个:

business_with_multiple_accounts = 
select * 
from business_acct
where 
      (Select Distinct count(*) 
       from 
         payment_acct  
         where payment_acct.payment_acct_id= business_acct.payment_acct_id
       )>1
group by business_id 

users_with_multiple_accounts =
select * 
from business_misc 
where 
      (Select Distinct count(*) 
       from 
         payment_acct  
         where payment_acct.business_user_id= business_misc.business_user_id
       )>1
group by business_user_id