2013-08-23 36 views
1

我在MySQL中嘗試了下面的查詢,但沒有工作。查詢獲得MySQL中不同值的計數

select count(*) 
from (
     select count distinct(RADL_REQUEST_MSISDN) 
     from rbt_activation_details_log 
     where RADL_ACTIVE ='A' 
     group by RADL_REQUEST_MSISDN 
    ); 
+1

你需要什麼輸出?只有'select count distinct(RADL_REQUEST_MSISDN)from rbt_activation_details_log where RADL_ACTIVE ='A'group by RADL_REQUEST_MSISDN'有什​​麼問題? – Jim

+0

Mysql Server版本是:5.0.22-log –

+0

@jim:我需要總數。我收到以下錯誤mysql> select count distinct(RADL_REQUEST_MSISDN)from rbt_activation_details_log where RADL_ACTIVE ='A'group by RADL_REQUEST_MSISDN; 錯誤1064(42000):您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在rbt_activation_details_log中的'distinct(RADL_REQUEST_MSISDN)附近使用,其中RADL_ACTIVE'在第1行 mysql> –

回答

3
select count(distinct column_whose_distinct_values_you_want_to_count) 
from rbt_activation_details_log 
where RADL_ACTIVE ='A' 
group by RADL_REQUEST_MSISDN 

你分組和在同一列計數,所以這將永遠給你1作爲結果。

編輯:

然後只需條款

select count(distinct RADL_REQUEST_MSISDN) 
from rbt_activation_details_log 
where RADL_ACTIVE ='A' 
+0

謝謝,沒有注意到快速閱讀。相應地編輯我的答案。 – fancyPants

+0

我需要總數。例如在oracle中,這個查詢工作正常,並且從TARIFF_PLANS WHERE STATUS_N = 1組中的TARIFF_ID_N中的select count(distinct tariff_id_n)給出count select count(*);輸出126 –

+0

@ user2696461編輯我的答案。 – fancyPants

1
SELECT COUNT(DISTINCT RADL_REQUEST_MSISDN) 
FROM rbt_activation_details_log 
WHERE RADL_ACTIVE = 'A' 
GROUP BY RADL_REQUEST_MSISDN; 
1
SELECT Count(UNIQUE(radl_request_msisdn)) 
FROM rbt_activation_details_log 
WHERE radl_active = 'A' 
GROUP BY radl_request_msisdn 
+0

不能在mysql中工作 –

+0

@ user2696461我已經根據你的表信息編輯了答案。這應該做到這一點。 – Susie

1

省略GROUP BY查詢:

select count(*) 
from 
    (select count distinct(RADL_REQUEST_MSISDN) 
    from rbt_activation_details_log 
    where RADL_ACTIVE ='A' group by RADL_REQUEST_MSISDN); 

顯示爲您想檢索所有重複計數不按radl_request_msisdn分組。如果是這樣比你重寫查詢以這樣的方式相反,如果你想分組

select count distinct(RADL_REQUEST_MSISDN) 
    from rbt_activation_details_log 
    where RADL_ACTIVE ='A'; 

,查詢將是:

select count distinct(RADL_REQUEST_MSISDN) 
    from rbt_activation_details_log 
    where RADL_ACTIVE ='A' group by RADL_REQUEST_MSISDN; 
+0

我沒有得到想要的結果。我需要總數。這些查詢將列出所有的列,我不想要。我的查詢工作正常在oracle中,但對於MySQL我需要修改PLZ建議 –

+0

@ user2696461:親愛的,我的查詢只返回數字!第一個返回所有rbt_activation_details_log的不同計數;第二個返回一個不同的計數,取決於rbt_activation_details_log。使用ob子查詢來計數count並不總是正確的。 –

+0

是的喬你是正確的,查詢給出正確的輸出。如果我不使用截然不同的,則有大約100000個計數。我想要不同的計數。我不想列出所有這些數 –