2011-09-20 63 views
1

這可能是用於SQL一個頑皮的使用......但在這裏它去....伯爵在MySQL中的計算的行

我有一個表:

Banners 
-------------- 
BannerID 
request_t0 
clicks_t0 
request_t1 
clicks_t1 
... 
request_t6 
clicks_t6 

使用的請求數和點擊,我計算CTR(點擊,以曝光比)爲每個組.... ctr_t0 = clicks_t0/request_t0 * 100

所以現在我有每行中6次單獨的點擊率....

對於輸出,我想每個CTR如何往往是在它的行中的最高計數......

所以給出的一組:

ctr_t0 ctr_t1 ctr_t3 
------ ------ ------ 
2.39%  1.24%  1.5% 
    1.4%  2.46%  2.2% 
    3.1%  2.45%  1.45% 

我想爲我的結果:

ctr_t0_count ctr_t1_count ctr_t3_count 
------------ ------------ ------------ 
      2    1    0 

關於如何做這個沒有學習編程語言的任何想法? :-)

回答

2
select 
sum(case when greatest(ctr_t0,ctr_t1,ctr_t3) = ctr_t0 then 1 else 0 end) as ctr_t0_count, 
sum(case when greatest(ctr_t0,ctr_t1,ctr_t3) = ctr_t1 then 1 else 0 end) as ctr_t1_count, 
sum(case when greatest(ctr_t0,ctr_t1,ctr_t3) = ctr_t3 then 1 else 0 end) as ctr_t3_count 
from (select ....) as t 

其中在選擇內有您先前的查詢。

+0

工作很好,謝謝! – Eric

+0

不客氣。 :) –