2013-12-19 35 views
3

我試圖在SQL中自我教育以便更好地在工作中使用數據庫。爲此,我正在使用Oracle Application Express。這是如果我第一次使用COUNT函數,並且在將我的查詢集成到其中時遇到一些困難。我做了大量的研究並閱讀了很多文獻,但我似乎無法做到。在使用Oracle的SQL查詢中集成COUNT函數時遇到的問題

我的目標是,可與具有該通道爲喜愛頻道(從調查表survey_result列)客戶數的計數沿每個通道顯示(從通道表)的channel_namechannel_number列。請參閱下面的代碼:

SELECT channel.channel_number, 
     channel.channel_name, 
     survey.survey_result, 
     count(SELECT survey.survey_result FROM survey) 
    From Channel, survey 
WHERE survey.channel_number = channel.channel_number 

目前我收到錯誤消息:

ORA-00936:缺少表達式。

回答

2

試試這個:

下面的查詢只給你這有最低1個客戶的渠道。

SELECT C.channel_number, C.channel_name, COUNT(S.survey_result) NoOfCustomers 
FROM Channel C 
INNER JOIN survey S ON S.channel_number = C.channel_number 
GROUP BY C.channel_number, C.channel_name; 

下面的查詢爲您提供所有渠道,無論客戶是否有客戶。

SELECT C.channel_number, C.channel_name, COUNT(S.survey_result) NoOfCustomers 
FROM Channel C 
LEFT JOIN survey S ON S.channel_number = C.channel_number 
GROUP BY C.channel_number, C.channel_name; 
2

無論是哪種可能爲你工作

SELECT channel.channel_number, 
     channel.channel_name, 
     count(survey.survey_result) 
    From Channel, survey 
WHERE survey.channel_number = channel.channel_number 
GROUP BY 
     channel.channel_number, 
     channel.channel_name 

SELECT channel.channel_number, 
     channel.channel_name, 
     survey.survey_result, 
     (SELECT count(survey_result) FROM survey) 
    From Channel, survey 
WHERE survey.channel_number = channel.channel_number 
+0

第一種選擇是走的路 –

1

count是一個聚合函數,因此您應該在channel.channel_number和channel.channel_name上有一個group。那麼只需使用count(survey.survey_result)而不是count(SELECT survey.survey_result FROM survey)。 Madhivanan和Saharsh Shah的答案對我來說很好。包括這個答案來解釋爲什麼。