如何在子查询中引用表

时间:2014-10-31 09:03:27

标签: sql sql-server subquery pervasive pervasive-sql

我有以下表格:

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 not a record”

编辑: 我在SQL Server中尝试了这个完全相同的查询并且它有效,所以有没有办法重新编写它以使其更加普遍友好?

1 个答案:

答案 0 :(得分: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上运行时,我得到的结果完全相同。