如何計算連續兩次缺席的用戶?我必須創建一張圖表來顯示員工缺勤頻率。PHP MYSQLI計數連續兩個日期的用戶
我的表名= incidencias
id | name | dateA | description
1 | al |2017-08-01| absence
2 | al |2017-08-02| absence
3 | alex |2017-08-01| absence
4 | alex |2017-08-02| absence
5 | alex |2017-08-03| absence
6 | al2 |2017-08-01| absence
7 | al2 |2017-08-02| absence
我想要的結果是2,只有人與AL2已經連續兩個日期,其中描述=缺席。
我正在使用PHP來運行查詢,我沒有嘗試這個代碼,我發現,但我測試它在sqlfiddle和偉大的工程。但不在我的主機中。我認爲這是針對PostgreSQL的。
$query2 = mysqli_query($conn, "SELECT name,
sum(diff) as days,
(dateA) as work_start,
(dateA) as work_end
FROM (SELECT name,
dateA,
diff
FROM (select name,
dateA,
nvl(dateA- lag(dateA) over (partition by name order by dateA),1) as diff
from incidencias
where description = 'absence'
) t1
where diff = 1
) t2
group by name
having sum(diff) = 2");
$row_cnt = mysqli_num_rows($query2);
printf("Result set has %d rows.\n", $row_cnt);
我真的很感激它。
能否請您發佈的SQL格式的一些示例數據? – sumit