我需要编写查询,但是我不确定如何编写查询。我需要根据状态获取所有EmployeeId。如果已完成且未决,则以完成为准。但是,如果只有待处理的工作尚未完成,我将处理待处理的工作,否则不要处理该员工的任何行。每个EmployeeId我只需要一项。从技术上讲,我也需要最早的时间,但是我想我会写这个部分。
RowNumber Status EmployeeId Produce Date
-------------------------------------------------------
1 New 1 Apples 1/1/18
2 Pending 1 BlueBerry 1/2/18
3 New 1 Oranges 1/3/18
4 Pending 2 Bananas 1/1/18
5 New 2 Grapes 1/2/18
6 Complete 2 Limes 1/3/18
因此在此示例中,我需要以下内容
RowNumber Status EmployeeId Produce Date
--------------------------------------------------------
2 Pending 1 BlueBerry 1/2/18
6 Complete 2 Limes 1/3/18
对我来说最困难的部分是试图弄清楚如何比较字符串。基本上(半伪代码)
Select top 1
t.EmployeeId, t.Produce, t.Date, stat.Status
(Case
If t.Status = 'Complete'
Select 'Complete'
If t.Status = 'Pending'
Select 'Pending'
Else
Dont Add this row ) stat
From
Table t
Where
t.Status = 'Complete' or t.Status = 'Pending'
Order by
t.Date
答案 0 :(得分:2)
我想到至少有两种不同的方法:
SELECT DISTINCT EmployeeId
FROM Table
WHERE Status = "Complete"
UNION
SELECT DISTINCT t1.EmployeeId
FROM Table t1
LEFT OUTER JOIN Table t2
ON t2.EmployeeId = t1.EmployeeId
AND t2.Status = "Complete"
WHERE t1.Status = "Pending"
AND t2.Status IS NULL
…或如果数据库引擎支持,则使用coalesce()
:
SELECT DISTINCT pend.EmployeeId,
coalesce(comp.Status,pend.Status),
coalesce(comp.RowNumber,pend.RowNumber)
FROM MyTable pend
LEFT OUTER JOIN MyTable comp
ON comp.EmployeeId = pend.EmployeeId
AND comp.Status = 'Complete'
WHERE pend.Status IN ('Complete','Pending');
答案 1 :(得分:1)
使用row_number()
是您想要的吗?
select top (1) with ties *
from table t
where Status in ('Completed', 'Pending')
order by row_number() over (partition by EmployeeId
order by (case Status when 'Completed'
then 0
when 'Pending'
then 1
end)
);
答案 2 :(得分:0)
您可以使用UNION ALL结合WHERE NOT EXISTS来实现:
$("[id='bpm.mnit_Nav']").on('click', 'xyz hover', function(){console.log('done');});
答案 3 :(得分:0)
您可以计算使用CASE WHEN的ROW_NUMBER。
然后对此进行过滤。
例如:
SELECT RowNumber, [Status], EmployeeId, Produce, [Date]
FROM
(
SELECT
RowNumber, [Status], EmployeeId, Produce, [Date],
ROW_NUMBER() OVER (
PARTITION BY EmployeeId
ORDER BY [Date] DESC,
CASE [Status] WHEN 'Complete' THEN 1 WHEN 'Pending' THEN 2 ELSE 9 END
) AS RN
FROM [YourTable] t
WHERE [Status] IN ('Complete','Pending')
) q
WHERE RN = 1
ORDER BY EmployeeId