mysql - 为mysql中每个不同值的每个数据类型选择几个最大类型

标签 mysql max distinct

userid      data_type,          timespentaday
 1       League of Legends          500
 1       Hearthstone                1500
 1       Hearthstone                1400
 2       World of Warcraft          1200
 1       Dota 2                     100
 2       Final Fantasy              500
 1       Dota 2                     700

鉴于此数据。我想查询每个用户在每个上花费的最多时间。

所需输出:

User    League Of Legends    Hearthstone   World of Warcraft     Dota 2    
 1           500                1500             0                  700
 2           0                  0                1200               0

我尝试过类似的事情

SELECT t1.* FROM user_info GROUP BY userid JOIN(
 SELECT(
         (SELECT max(timespentaday) where data_type='League of Legends'),
          (SELECT max(timespentaday) where data_type='Hearhstone'),
          (SELECT max(timespentaday) where data_type='Dota 2)'
FROM socialcount AS t2
) as t2
ON t1.userid = t2.userid

最佳答案

基本上要做到这一点,你需要每组最大的n..有一篇很好的文章,但要点在mysql中,你必须使用变量才能接近这一点..特别是在表上进行数据透视(一个假的数据透视表,因为 MySQL 没有对此的本地支持)。

SELECT userid,
    MAX(CASE WHEN data_type = "League of Legends" THEN timespentaday ELSE 0 END) as "League of Legends",
    MAX(CASE WHEN data_type = "Hearthstone" THEN timespentaday ELSE 0 END) as "Hearthstone",
    MAX(CASE WHEN data_type = "Dota 2" THEN timespentaday ELSE 0 END) as "Dota 2",
    MAX(CASE WHEN data_type = "World of Warcraft" THEN timespentaday ELSE 0 END) as "World of Warcraft",
    MAX(CASE WHEN data_type = "Final Fantasy" THEN timespentaday ELSE 0 END) as "Final Fantasy"
FROM
(   SELECT *, @A := if(@B = userid, if(@C = data_type, @A + 1, 1), 1) as count_to_use, @B := userid, @C := data_type
    FROM
    (   SELECT userid, timespentaday, data_type
        FROM gamers
        CROSS JOIN(SELECT @A := 0, @B := 0, @C := '') temp
        ORDER BY userid ASC, data_type ASC, timespentaday DESC
    ) t
    HAVING count_to_use = 1
)t1
GROUP BY userid

DEMO

注意:

MySQL DOCS关于使用用户定义变量的警告非常清楚:

As a general rule, you should never assign a value to a user variable and read the value within the same statement. You might get the results you expect, but this is not guaranteed. The order of evaluation for expressions involving user variables is undefined and may change based on the elements contained within a given statement; in addition, this order is not guaranteed to be the same between releases of the MySQL Server. In SELECT @a, @a:=@a+1, ..., you might think that MySQL will evaluate @a first and then do an assignment second. However, changing the statement (for example, by adding a GROUP BY, HAVING, or ORDER BY clause) may cause MySQL to select an execution plan with a different order of evaluation.

关于mysql - 为mysql中每个不同值的每个数据类型选择几个最大类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27115158/

相关文章:

javascript - 单击表格内容并使其可编辑

php - 使用 MYSQL Distinct 和 PHP 显示一次年份

php - 在 Valetina studio 中获取 mysql 中的当前时间戳

mysql - Where 子句中的未知列

mysql - SQL查询以获取与另一列的MAX值对应的列值?

mysql - MySQL MAX函数的结果

if-statement - Tableau - 返回具有 MAX 值的维度

linq-to-sql - Linq 到 Sql : Select distinct rows while ignoring specified columns

sql - 内部联接返回冗余数据而不是预期数据

mysql - 如何选择表行值作为值总和的列