当数字= 0或>时,计算连续的10天数。 10

时间:2013-09-25 21:58:52

标签: mysql sql

这是我使用mysql查询生成的sqlfiddle http://sqlfiddle.com/#!2/f2794/4

当存在= 0时它连续计数10天,但是我需要添加第二个条件以计算存在的位置> 10.

例如

11 22 0 0 0 0 0 0 0 0 0 0 0 0 1

应该算上14

这是查询

select sum(count) total from (

SELECT COUNT(present) as count FROM (
SELECT
IF((q.present != 0), @rownum:=@rownum+1, @rownum:=@rownum) AS rownumber, @prevDate:=q.date, q.*
FROM (
SELECT
name
, date
, present
FROM
teacher, (SELECT @rownum:=0, @prevDate:='') vars
WHERE date BETWEEN '2013-07-01' AND '2013-07-31'
ORDER BY date, present
) q
) sq
GROUP BY present, rownumber
HAVING COUNT(*) >= 10

) d

所以,如果你可以帮助我,请这样做:)

最好的问候 米。

1 个答案:

答案 0 :(得分:0)

我真的不太了解您的查询,但我认为只需更改(q.Present!= 0)以合并其他测试就可以解决您的问题:

SELECT sum(count) total from (
  SELECT COUNT(present) as count FROM (
    SELECT
      IF((q.present != 0 AND q.present <= 10), @rownum:=@rownum+1, @rownum:=@rownum) AS rownumber, @prevDate:=q.date, q.*
    FROM (
      SELECT
        name
        , date
        , present
      FROM
        teacher, (SELECT @rownum:=0, @prevDate:='') vars
      WHERE date BETWEEN '2013-07-01' AND '2013-07-31'
      ORDER BY date, present
    ) q
  ) sq
  GROUP BY present, rownumber
  HAVING COUNT(*) >= 10
) d