如何在mysql中查找重复值和计数

时间:2016-02-08 08:38:24

标签: mysql

我有3列的表,现在如果立即出现3次,如何找到值 即第一个trnas_value连续3次出现(重复4次),第2和第6行也重复相同的date列从A_Z排序

date               tran_val                name
23mar                22                    mark
24mar                22                    mark
25mar                22                    mark
26mar                22                    mark
27mar                22                    mark
28jan                99                    john
29jan                99                    john
30jan                99                    john
31jan                99                    john

输出

name     trans_value        consecutive_count
mark       22                   2
john       99                   1

我们有一个代码没有给出上述输出..

SELECT name, 
        tran_val, 
        MAX(cnt - 3) AS consecutive_count
FROM
(
    SELECT date, 
            tran_val, 
            name, 
            @cnt:=IF(@tran_val=tran_val AND @name=name, @cnt + 1, 1) AS cnt,
            @tran_val:=tran_val,
            @name:=name
    FROM some_table
    CROSS JOIN (SELECT @cnt:=0, @tran_val:=0, @name:='') sub0
    ORDER BY `date`
) sub1
GROUP BY name, 
        tran_val

上述代码中的任何修改都将获得所需的输出。谢谢

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT `tran_val`, `name`, COUNT(*) - 3
FROM (
  SELECT `date`, `tran_val`, `name`, rn - seq AS grp
  FROM (
    SELECT `date`, `tran_val`, `name`,
           @rn := @rn + 1 AS rn,
           @seq := IF(@name = `name` And @val = `tran_val`, @seq+1, 1) AS seq,
           @name := name,
           @val := tran_val
    FROM mytable
    CROSS JOIN (SELECT @rn := 0, @seq := 0, @name = '', @val = 0) AS vars
    ORDER BY `date`) AS t ) AS s
GROUP BY `tran_val`, `name`, grp
HAVING COUNT(*) > 3

您需要两个单独的变量来枚举序列:

  • @rn只列举连续的表格行
  • @seq枚举具有相同nametran_val值的连续表格行。

这两个变量之间的差异,即@rn - @seq,标识具有相同nametran_val的连续表行的 islands 值。

编辑:我在查询中添加了HAVING子句,以便过滤掉连续行数为3或更少的岛屿。

Demo here