简体   繁体   English

SQL重新测试返回第一行的同一表

[英]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.

相关问题 如何在另一个表中选择具有相同值的SQL表中的第一行和第二行实例? - How to select 1st and 2nd row instance in SQL table having the same value in another table? SQL:从词法排序表中仅选择第一行 - SQL: Select only 1st row from lexical ordered table SQL 仅在第一个 TRUE 条件下返回 - SQL returns only for 1st TRUE condition 如何在第一行的相同 ID 中显示相同的行 ID? - How to display same id of row in same id of 1st row? SQL Server:从同一列中具有匹配值的第一行之后的列中选择所有行 - SQL Server : select all rows from a column after the 1st row with matching value from the same column SQL:如何从表中选择第 1、3、11 和第 n 行? - SQL: How to select 1st, 3rd, 11th and nth row from a table? 想要查询连接第一个表的第一行和第二个表的前两列 - want query to connect 1st table 1st row and 2nd table first two column 如何在sql中比较同一表的第一条记录中的列A和第二条记录中的列B - How to compare columnA in 1st record and columnB in 2nd record of a same table in sql Oracle sql 添加自定义列名作为结果集的第一行 - Oracle sql adding custom column name as 1st row of the resultSet SQL查询要在表上进行迭代,以计算在其字段之一中具有相同值的第一条记录和第二条记录之间的时间差? - SQL query to iterate on a table to calculate the time difference between the 1st and 2nd record that have the same value in one of their fields?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM