[英]SQL Retest same table that returns 1st row
I'm a beginner when it comes to sql and this one is a little over my head and hoping someone could point me in the right direction.. 我是有关sql的初学者,这个内容有点过头,希望有人可以指出正确的方向。
I have written a query that correctly returns one row from the right hand table but I need to add another test in the final where clause that rechecks the table and checks all entries in the field contitems.tofollow
and looks for values greater than zero's which match the original contracts.ContNo
row returned.. 我编写了一个查询,该查询正确地从右侧表返回了一行,但我需要在最终的where子句中添加另一项测试,该子句重新检查该表并检查
contitems.tofollow
字段中的所有条目,并查找大于零的匹配值原始contracts.ContNo
续无行返回。
SELECT Contracts.*, Contitems.*
FROM dbo.Contracts
INNER JOIN dbo.ContItems
ON dbo.ContItems.RECID =
(SELECT TOP(1) RECID
FROM dbo.ContItems
WHERE (ContItems.CONTNO = dbo.Contracts.CONTNO))
WHERE dbo.Contracts.SOURCE = 2
and (contracts.custom = 1 or contitems.tofollow > 0)
and contracts.status not in (4,9)
I need to replace the contitems.tofollow > 0
with a test that rechecks the whole contitems.tofollow
results for each ContNO
but no idea how to achieve it.. can anyone help..? 我需要用重新检查每个
ContNO
的整个contitems.tofollow
结果的测试替换contitems.tofollow > 0
,但不知道如何实现。
i thought of another way to do this by selecting the single row with the greatest TOFOLLOW quantity -- no further test necessary.. 我想到了另一种方法,即选择具有最大TOFOLLOW数量的单行-无需进一步测试。
SELECT Contracts.*, Contitems.*
FROM dbo.Contracts
INNER JOIN dbo.ContItems
ON dbo.ContItems.RECID =
(SELECT TOP(1) RECID
FROM dbo.ContItems
WHERE (ContItems.CONTNO = dbo.Contracts.CONTNO)
ORDER BY TOFOLLOW Desc)
WHERE dbo.Contracts.SOURCE = 2
and (contracts.custom = 1 or contitems.tofollow > 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.