2011-09-20 120 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

不客气。 :) –