简体   繁体   English

Sql 查询 - 在列表上为每组具有相同 ID 的 N 个项目生成“批次”编号

[英]Sql Query - Generating "lot" numbers on a list for each group of N items with the same ID

I've come across a situation where I need to write a query, and I have no clue what the best route is.我遇到过需要编写查询的情况,但我不知道最佳路线是什么。

So assume I've got data like this:所以假设我有这样的数据:

+------------+-------------------+
| EmployeeID |  ProjectStartDate |
+------------+-------------------+
| 1          |  01/01/2017       |
| 1          |  02/02/2017       |
| 1          |  03/03/2017       |
| 1          |  04/04/2017       |
| 1          |  05/05/2017       |
| 1          |  06/06/2017       |
| 1          |  07/07/2017       |
| 2          |  01/01/2017       |
| 2          |  02/02/2017       |
| 2          |  03/03/2017       |
| 2          |  04/04/2017       |
+------------+-------------------+

I need to generate project "lots" for each employee, but the lots have a lot number associated with each group of N (let's say 5 here) items.我需要为每个员工生成项目“批次”,但批次有一个与每组 N(这里假设为 5 个)项目相关联的批号。 So I need a query that can group by EmployeeID, sort by ProjectStartDate, and then dynamically group each employee's results into groups of 5, assigning an incremental count to each group.所以我需要一个查询,可以按 EmployeeID 分组,按 ProjectStartDate 排序,然后将每个员工的结果动态分组为 5 组,为每个组分配一个增量计数。 So the end result would be like this:所以最终的结果是这样的:

+------------+-------------+------------------+
| LotNumber  | EmployeeId  | ProjectStartDate |
+------------+-------------+------------------+
| 1          | 1           | 01/01/2017       |
| 1          | 1           | 02/02/2017       |
| 1          | 1           | 03/03/2017       |
| 1          | 1           | 04/04/2017       |
| 1          | 1           | 05/05/2017       |
+------------+-------------+------------------+
| 2          | 1           | 06/06/2017       |
| 2          | 1           | 07/07/2017       |
+------------+-------------+------------------+
| 1          | 2           | 01/01/2017       |
| 1          | 2           | 02/02/2017       |
| 1          | 2           | 03/03/2017       |
| 1          | 2           | 04/04/2017       |
+------------+-------------+------------------+

Normally, I'd just create a table for lots, but the lot numbers can change dynamically since they go off of the Date with the first lots generated from the oldest dates.通常,我只是为批次创建一个表,但批次号可以动态更改,因为它们从最旧的日期生成的第一批从日期开始。 So adding or deleting projects will change every lot number after it.因此,添加或删除项目将更改其后的每个批号。

Does anyone have any thoughts on an efficient way to do this?有没有人对一种有效的方法有任何想法?

A combination of ROW_NUMBER and integer division should work: ROW_NUMBER和整数除法的组合应该可以工作:

WITH CTE AS
(
    SELECT  *,
            RN = ROW_NUMBER() OVER(PARTITION BY EmployeeID ORDER BT ProjectStartDate)
    FROM dbo.YourTable
)
SELECT  (RN -1 )/5 + 1 LotNumber,
        EmployeeID,
        ProjectStartDate
FROM CTE
;

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

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