1

我有以下表格:

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 中尝试了这个完全相同的查询并且它有效,那么有没有办法重新编写它以使其更加普遍友好?

4

1 回答 1

1

现在这是用 SQL Server 标记的,我觉得不需要编写一个有效的普遍查询。

我接受了您的原始查询并以更简单的方式重写了它。也许试试这个,看看它是否能解决你的问题?

DECLARE @Bradford_Score_Bands TABLE (BandNo INT, InclusiveScore INT);
INSERT INTO @Bradford_Score_Bands VALUES (1, 0);
INSERT INTO @Bradford_Score_Bands VALUES (2, 150);
INSERT INTO @Bradford_Score_Bands VALUES (3, 500);
DECLARE @Bradford_Scores TABLE (ClockNo INT, Dated DATE, Score INT);
INSERT INTO @Bradford_Scores VALUES (2, '20141030', 123);
INSERT INTO @Bradford_Scores VALUES (99, '20141030', 3);
INSERT INTO @Bradford_Scores VALUES (2, '20141029', 101);
INSERT INTO @Bradford_Scores VALUES (99, '20141029', 8);
DECLARE @Employees TABLE (ClockNo INT);
INSERT INTO @Employees VALUES (2);
INSERT INTO @Employees VALUES (3);
INSERT INTO @Employees VALUES (99);

--Original Query
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;

--New query
SELECT  
    e.ClockNo,
    MIN(bsbt.BandNo),
    MIN(bsby.BandNo)
FROM 
    @Employees e
    LEFT JOIN @Bradford_Scores bst ON bst.ClockNo = e.ClockNo AND bst.Dated = '20141030'
    LEFT JOIN @Bradford_Scores bsy ON bsy.ClockNo = e.ClockNo AND bsy.Dated = '20141029'
    LEFT JOIN @Bradford_Score_Bands bsbt ON bsbt.InclusiveScore >= bst.Score
    LEFT JOIN @Bradford_Score_Bands bsby ON bsby.InclusiveScore >= bsy.Score
GROUP BY
    e.ClockNo;

在 SQL Server 上运行这两个查询时,我得到了完全相同的结果。

于 2014-10-31T16:03:22.690 回答