2015-05-21 86 views
-1

返回與組1點多行的問題,請幫忙解決什麼是錯我的查詢下面我得到「子查詢返回多個1行」子查詢由

select 
count(*) NONGENACC, AFFILIATE, 
(SELECT COUNT(ACCOUNTNO) from realdb.tbl_logs GROUP BY AFFILIATE)AS ALLACCS 
FROM realdb.tbl_logs 
where LASTDATE <> CURDATE() group by AFFILIATE 

返回下面當我走在GROUP BY從子查詢這不是我想要在allacs領域。請幫助...

NONGENACC AFFILIATE  ALLACCS 
1   ECOCBFBF  1481 
2   ECOCBIBI  1481 
13  ECOCBJBJ  1481 
11  ECOCCDKI  1481 
3   ECOCCFCF  1481 
+1

嗨,你可以創建[sql小提琴](http://sqlfiddle.com/)並告訴我們你期望的結果從查詢中獲取? –

回答

0

嘗試從子查詢中刪除GROUP BY AFFILIATE。 所以你最終的查詢將看起來像這樣:

select 
count(*) NONGENACC, AFFILIATE, 
(SELECT COUNT(ACCOUNTNO) from realdb.tbl_logs)AS ALLACCS 
FROM realdb.tbl_logs 
where LASTDATE <> CURDATE() group by AFFILIATE 
+0

他會得到相同的值**(SELECT COUNT(ACCOUNTNO)from realdb.tbl_logs)**所有行 –

+0

是的,但子查詢將只返回一行,而不是5 –

+0

,因爲它沒有太多的sans,計算表中的所有ACCOUNTNO ..他應該刪除完整的子查詢,並有COUNT(ACCOUNTNO)作爲可能工作的ALLACCS ...但如果他不提供預期的結果,我們無法確定他應該做什麼.. GL –

0

好吧謝謝你們所有的提示。我有一種方法,通過使用下面的查詢來獲得我想要的結果:

SELECT AFFILIATE, 'TotalAccounts', count(accountno) 
    FROM realdb.tbl_logs 
    GROUP BY AFFILIATE 
    UNION ALL 
    SELECT AFFILIATE, 'MissingAccount', count(accountno) 
    FROM realdb.tbl_logs 
    WHERE LASTDATE <> CURDATE() 
    GROUP BY AFFILIATE 
    ORDER BY 1, 2 DESC