MySQL:单列

时间:2015-05-17 20:29:02

标签: mysql

对于图形,我被要求用MYSQL语句构造数据。 它需要以下输出(3个列):

AVG_points | Playerid |日期

在数据库中,我没有每个roundID的平均点数,只是每个玩家每轮得分。

很容易用平均值(点数)计算当前平均点,但我需要每轮的平均点数,因此可以在图表中绘制出来。

我试图制作一个SQL语句来给我每一轮的平均值,但它没有以可用的格式绘制图形。我读到了Pivotting,但那不是在这种情况下有效,我认为我的sql很简单,加上每一个新的回合,我需要编写更多的行,这意味着手动编辑每个表更新,以使图形工作..

这就是我的尝试:

SELECT   t1.playerid as player
    ,date_format(t1.CreatedTime, '%Y%m%d%H%i') as date

/* calculate average points per round */                

                ,(select avg(points) from pokermax_scores t2 where tournamentid <= (select distinct(tournamentid) from pokermax_scores order by tournamentid desc limit 0,1) and t2.playerid = t1.playerid) as avg_current
                ,(select avg(points) from pokermax_scores t2 where tournamentid <= (select distinct(tournamentid) from pokermax_scores order by tournamentid desc limit 1,1) and t2.playerid = t1.playerid) as 1_avg_last
                ,(select avg(points) from pokermax_scores t2 where tournamentid <= (select distinct(tournamentid) from pokermax_scores order by tournamentid desc limit 2,1) and t2.playerid = t1.playerid) as 2_avg_last
                ,(select avg(points) from pokermax_scores t2 where tournamentid <= (select distinct(tournamentid) from pokermax_scores order by tournamentid desc limit 3,1) and t2.playerid = t1.playerid) as 3_avg_last
                ,(select avg(points) from pokermax_scores t2 where tournamentid <= (select distinct(tournamentid) from pokermax_scores order by tournamentid desc limit 4,1) and t2.playerid = t1.playerid) as 4_avg_last
                ,(select avg(points) from pokermax_scores t2 where tournamentid <= (select distinct(tournamentid) from pokermax_scores order by tournamentid desc limit 5,1) and t2.playerid = t1.playerid) as 5_avg_last

FROM pokermax_scores as t1, pokermax_players as t3
GROUP BY player

给出以下输出:&lt;查看SQLFIDDLE LINK&gt;

但我需要这种格式的数据,因此PHP可以正确循环:

http://i57.tinypic.com/10wists.png

这里有没有SQL大师知道如何编辑我的声明以使其如上图所示?

感谢阅读所有这些:)

这里是SQLFIDDLE:http://sqlfiddle.com/#!9/a956f/2

2 个答案:

答案 0 :(得分:1)

目前还不清楚您的数据真实情况。以下似乎是一个很好的起点,因为它以您想要的格式生成输出:

SELECT date(ps.CreatedTime) as date,
       ps.playerid as player,
       avg(ps.score)
FROM pokermax_scores ps
GROUP BY date(ps.CreatedTime), ps.playerid;

编辑:

评论有帮助。你什么都没有打电话&#34; round&#34;在数据中。

我猜它是tournamentid。如果是其他内容,则可以轻松修改查询。我想你想要两个级别的聚合:

SELECT date, player, avg(score)
FROM (SELECT date(ps.CreatedTime) as date,
             ps.playerid as player, tournamentid,
             SUM(ps.points) as score
      FROM pokermax_scores ps
      GROUP BY date(ps.CreatedTime), ps.playerid, tournamentid
     ) dpt
GROUP BY date, player;

Here它在SQL小提琴中。

答案 1 :(得分:0)

您可以使用它来获得锦标赛积分上的级联总和:

set @score_accumulator=0;
set @accumulator=0;

SELECT sub.tournamentId, 
        sub.datePS,
        -- SUM(@score_accumulator := @score_accumulator + sub.points) score,
        -- @accumulator := @accumulator + 1 nrTour,
       SUM(@score_accumulator := @score_accumulator + sub.points)/(@accumulator := @accumulator + 1) score
     FROM 
          (SELECT DISTINCT tournamentId,
           date(CreatedTime) AS datePS,
           points points
           FROM pokermax_scores ps2
           ORDER BY CreatedTime) sub
     GROUP BY sub.tournamentId ORDER BY sub.datePS

同样,你可以为球员积分进化。

话虽这么说,这种逻辑应该位于应用程序级别而不是数据库级别