mysql - 计算 mysql 中的计算行

标签 mysql sql

这可能是对 sql 的一个顽皮的使用...但是这里是...

我有一张 table :

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

使用请求数和点击数,我计算了每个集合的点击率(点击展示率).... ctr_t0 = clicks_t0/request_t0 * 100

所以现在我在每行中有 6 个单独的点击率......

对于输出,我想计算每个点击率在其行中最高的频率......

所以给定集合:

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

关于如何在不学习编程语言的情况下做到这一点的任何想法? :-)

最佳答案

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

在 select 中有您之前的查询。

关于mysql - 计算 mysql 中的计算行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7488610/

相关文章:

mysql - 规范化超出 MySQL 知识范围?

Python字典键长度与mysql中查询返回的行不同

mysql - 日期编码问题

sql - 当字段之间存在不规则关系时如何设计表格

php - 如何在 mysql 中创建自动更新时间字段?

mysql - 无法从另一个表更新表

MySQL加入3个表和空行

php - mysql 在 php 中分组

mySQL 如何将多个 JOIN 结果作为唯一列返回?

MySQL:选择仅包含列表中的元素及其组合的条目