在两列上采样 SQL

Sampling on two columns SQL

我在一家进行物业维修的公司工作。每个维修都有一个唯一的编号,每个 属性(大约 15000 条记录)和每个工人(大约有 30 个)也是如此。每个月我们都会进行 2000 到 4000 次维修。

为了让公司进行满意度调查,我们需要随机(最好,但可以只使用 TOP)select 每个工人每月 5 个属性,同时确保 属性 有在过去 3 个月内没有发送过调查(这将是一个属性)。

本质上,我正在寻找执行以下操作的提示:

+-------+---------+-------+-----------------+ | Place | Worker | Date | previous survey | +-------+---------+-------+-----------------+ | 0001 | 1 | june1 | | | 0002 | 1 | june1 | | | 0003 | 2 | june1 | | | 0004 | 1 | june1 | Y | | 0005 | 2 | june1 | | | 0006 | 2 | june1 | | | 0007 | 1 | june1 | | | 0008 | 1 | june1 | | | 0009 | 1 | june1 | | | 0010 | 2 | june1 | | | 0011 | 1 | june1 | | | 0012 | 2 | june1 | | | 0013 | 1 | june1 | | | 0014 | 1 | june1 | Y | | 0015 | 1 | june1 | | +-------+---------+-------+-----------------+

输出:

Worker | Place 1 |0001 1 |0002 1 |0007 1 |0008 1 |0009 2 |0003 2 |0005 2 |0006 2 |0012 2 |NULL ....等等。

任何帮助将不胜感激,我什至不知道如何google开始解决这个问题!

你可以很容易地利用 ROW_NUMBER 来做这种事情。

https://docs.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql

with SortedResults as
(
    select *
        , ROW_NUMBER() over (partition by Worker order by (Select newid())) as RowNum
    from YourTable
    where PreviousSurvey is null --or whatever the predicate would be here
)

select *
from SortedResults
where RowNum <= 5
SELECT worker_id, place 
FROM <yourtable> 
WHERE previous_survey != 'Y' 
ORDER BY place ASC LIMIT 5