简体   繁体   English

Mysql - 计算行数,直到达到第一个不同的值

[英]Mysql - Count rows until a first distinct value is reached

I am trying to count the number of rows until the first occurrence of distinct value, for every distinct value.我正在尝试为每个不同的值计算行数,直到第一次出现不同的值。

Table example表格示例

game winner
-----------
1    Mark
2    Joe
3    Mark
4    Paula
5    Paula
6    Paula
7    Joe
8    Anna

With query below i get this..通过下面的查询,我得到了这个..

SELECT winner,COUNT(*) as count FROM tablename GROUP BY winner;

Result结果

Mark won 2 games
Joe won 2 games
Paula won 3 games
Anna won 1 game

Below are the results that I want to get:以下是我想要得到的结果:

Mark won 2 games, but didn't won last 6 games 
Joe won 2 games, but didn't won last 1 games 
Paula won 3 games, but didn't won last 2 games 
Anna won 1 game, but didn't won last 0 games

Thank you for taking the time to help me, I really appreciate it.谢谢你花时间帮助我,我真的很感激。

You can count the last not won games in a correlated subquery.您可以计算相关子查询中最后未赢的比赛。

select winner, count(*) as won, (
  select count(*)
  from tablename t2
  where t2.game > max(t1.game)
) as not_won
from tablename t1
group by winner

Demo: https://www.db-fiddle.com/f/czHPqscvEGgLPLeVYHV5hk/0演示: https : //www.db-fiddle.com/f/czHPqscvEGgLPLeVYHV5hk/0

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(game SERIAL PRIMARY KEY
,winner VARCHAR(12) NOT NULL
);

INSERT INTO my_table VALUES
(1,'Mark'),
(2,'Joe'),
(3,'Mark'),
(4,'Paula'),
(5,'Paula'),
(6,'Paula'),
(7,'Joe'),
(8,'Anna');

SELECT x.winner 
     , y.total
     , COUNT(z.game) games_since_last_win
  FROM my_table x 
  JOIN 
     ( SELECT COUNT(game) total
            , MAX(game) game 
         FROM my_table 
        GROUP 
           BY winner
     ) y 
    ON y.game = x.game
  LEFT
  JOIN my_table z
    ON z.winner <> x.winner
   AND z.game > x.game
 GROUP   
    BY x.winner;
+--------+-------+----------------------+
| winner | total | games_since_last_win |
+--------+-------+----------------------+
| Anna   |     1 |                    0 |
| Joe    |     2 |                    1 |
| Mark   |     2 |                    5 |
| Paula  |     3 |                    2 |
+--------+-------+----------------------+

I based my answer on this one我的回答基于这个

I would sort descending by game and use a ranking on it, then wrap your query around this one.我会按游戏降序排序并对其使用排名,然后将您的查询包裹在这个问题上。

I changed COUNT(*) as count to COUNT(*) as cnt to avoid confusion, count is a SQL keyword我将COUNT(*) as count改为COUNT(*) as cnt以避免混淆, count是一个 SQL 关键字

Schema (MySQL v5.7)架构 (MySQL v5.7)

CREATE TABLE test (
  `game` INTEGER,
  `winner` VARCHAR(5)
);

INSERT INTO test
  (`game`, `winner`)
VALUES
  (1, 'Mark'),
  (2, 'Joe'),
  (3, 'Mark'),
  (4, 'Paula'),
  (5, 'Paula'),
  (6, 'Paula'),
  (7, 'Joe'),
  (8, 'Anna');

Query #1查询#1

SELECT winner, COUNT(*) as cnt, MIN(rank) AS lastwon FROM
(
    SELECT    winner,
              game,
              @curRank := @curRank + 1 AS rank
    FROM      test, (SELECT @curRank := -1) r
    ORDER BY  game DESC
 ) ranking
 GROUP BY winner
 ORDER BY cnt;

Output输出

| winner | cnt | lastwon |
| ------ | --- | ------- |
| Anna   | 1   | 0       |
| Joe    | 2   | 1       |
| Mark   | 2   | 5       |
| Paula  | 3   | 2       |

View on DB Fiddle在 DB Fiddle 上查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM