获取列的行中的最大值

时间:2017-03-29 14:59:25

标签: sql

我有一些运行良好的SQL代码,我需要调整它以获取RevisionNo列的最大值。它不适合我,我不确定我是否需要一个Having条款或如何最好地实现它。这是工作代码:

SELECT ValueText FROM VariableValue
   WHERE (VariableID = 60) AND (DocumentID IN
               (SELECT DocumentID FROM Documents
                   WHERE (deleted = 0 and DocumentID IN
                             (SELECT DocumentID FROM Documents WHERE (Filename LIKE 'TCT-013963%.SLDASM'))))

这不是:

SELECT ValueText FROM VariableValue
   WHERE (VariableID = 60) AND MAX(RevisionNo) AND (DocumentID IN
               (SELECT DocumentID FROM Documents
                   WHERE (deleted = 0 and DocumentID IN
                             (SELECT DocumentID FROM Documents WHERE (Filename LIKE 'TCT-013963%.SLDASM')))))

我得到的运行查询的结果返回了来自valuetext列的两行(这是一个描述字段,我想要最新的,因此是RevisionNo列上的最大值)。

| ValueText | RevisionNo |

| PRESSURE VESSEL FRONT END ASSY BNCT | 1 |

| PRESSURE VESSEL FRONT END ASSY NUBEAM | 7 |

1 个答案:

答案 0 :(得分:0)

SELECT TOP 1 ValueText FROM VariableValue
   WHERE (VariableID = 60) AND (DocumentID IN
       (SELECT DocumentID FROM Documents
           WHERE (deleted = 0 and DocumentID IN
               (SELECT DocumentID FROM Documents WHERE (Filename LIKE 'TCT-013963%.SLDASM')))))
                   ORDER BY RevisionNo DESC