SQL查找前连续几天的计数

时间:2017-10-03 12:23:51

标签: sql sql-server-2008

Hiiya All,

尝试弄清楚如何使用SQL告诉我一个人在约会之前连续工作了多少天。

来源

+----------+---------+-----------+------------+
| Forename | Surname | EMP_Num   | Shift Date |
+----------+---------+-----------+------------+
| John     | Kerry   | 0111      | 2017-09-28 |
+----------+---------+-----------+------------+
| John     | Kerry   | 0111      | 2017-09-29 |
+----------+---------+-----------+------------+
| John     | Kerry   | 0111      | 2017-09-30 |
+----------+---------+-----------+------------+
| John     | Kerry   | 0111      | 2017-10-02 |
+----------+---------+-----------+------------+
| John     | Kerry   | 0111      | 2017-10-03 |
+----------+---------+-----------+------------+
| Bill     | Kerry   | 0112      | 2017-09-28 |
+----------+---------+-----------+------------+
| Bill     | Kerry   | 0112      | 2017-09-29 |
+----------+---------+-----------+------------+
| Bill     | Kerry   | 0112      | 2017-10-01 |
+----------+---------+-----------+------------+
| Bill     | Kerry   | 0112      | 2017-10-02 |
+----------+---------+-----------+------------+
| Bill     | Kerry   | 0112      | 2017-10-03 |
+----------+---------+-----------+------------+

所以今天(2017-10-03)的结果将是......

+----------+---------+-----------+-------------+
| Forename | Surname | EMP_Num   | Days Worked |
+----------+---------+-----------+-------------+
| John     | Kerry   | 0111      | 2           |
+----------+---------+-----------+-------------+
| Bill     | Kerry   | 0112      | 3           |
+----------+---------+-----------+-------------+

或2017-09-30的结果将是

+----------+---------+-----------+-------------+
| Forename | Surname | EMP_Num   | Days Worked |
+----------+---------+-----------+-------------+
| John     | Kerry   | 0111      | 3           |
+----------+---------+-----------+-------------+
| Bill     | Kerry   | 0112      | 0           |
+----------+---------+-----------+-------------+

任何想法?

我正在运行SQL 2008所以没有领先或滞后我害怕。

PS感谢http://www.tablesgenerator.com/text_tables

0 个答案:

没有答案