我有以下表格:
Bradford_Score_Bands
BandNo InclusiveScore
------------------------
1 0
2 150
3 500
Bradford_Scores
ClockNo Dated Score
--------------------------------
2 30/10/14 123
99 30/10/14 3
2 29/10/14 101
99 29/10/14 8
雇员
ClockNo
--------------------
2
3
99
我的目标是根据他们的分数计算出今天和昨天每个 ClockNo 的 BandNo
我可以根据这样的分数值找到正确的 BandNo:
SELECT MIN(BandNo) FROM Bradford_Score_Bands WHERE InclusiveScore >= 123
我可以像这样找到每个人今天和昨天的分数:
SELECT DISTINCT EMP.ClockNo,
ISNULL((SELECT Score FROM Bradford_Scores BFT WHERE Dated = '2014-10-30' AND BFT.ClockNo = EMP.ClockNo), 0) As ScoreToday,
ISNULL((SELECT Score FROM Bradford_Scores BFT WHERE Dated = '2014-10-29' AND BFT.ClockNo = EMP.ClockNo), 0) As ScoreYesterday
FROM Employees EMP
但我似乎无法将两者结合起来。我认为这样的事情会起作用:
SELECT DISTINCT EMP.ClockNo,
(SELECT MIN(BandNo) FROM Bradford_Score_Bands WHERE InclusiveScore >=
(SELECT Score FROM Bradford_Scores BFT1 WHERE Dated = '2014-10-30' AND BFT1.ClockNo = EMP.ClockNo)),
(SELECT MIN(BandNo) FROM Bradford_Score_Bands WHERE InclusiveScore >=
(SELECT Score FROM Bradford_Scores BFT2 WHERE Dated = '2014-10-29' AND BFT2.ClockNo = EMP.ClockNo))
FROM Employees EMP
但是子查询中我引用 BFTX.ClockNo = EMP.ClockNo 的部分似乎导致查询失败。我收到有用的普遍错误“Data Record ManagerCurrency not on a record”
编辑:我在 SQL Server 中尝试了这个完全相同的查询并且它有效,那么有没有办法重新编写它以使其更加普遍友好?