所以我有一个独特的查询,可以将我的列表缩小到我想要的内容,但有时候最后一个表可能有3个正确的选择,我只想要最新的出现。
当前查询:
SELECT DISTCINT
D.Filename, S.Name AS State, W.Name AS 'Workflow Name', U.Username AS 'Name',
CONVERT(VARCHAR, TH.Date, 101) AS [Entered State],
DATEDIFF(dd, TH.Date, GETUTCDATE()) AS [Days In State]
FROM
Status AS S
INNER JOIN
Documents AS D ON D.CurrentStatusID = S.StatusID
INNER JOIN
Workflows AS W ON W.WorkflowID = S.WorkflowID
INNER JOIN
TransitionHistory AS TH ON D.DocumentID = TH.DocumentID
AND D.LatestRevisionNo - 1 = TH.RevNr
INNER JOIN
Users AS U ON U.UserID = TH.UserID
WHERE
d.Filename NOT LIKE '%test%'
AND d.filename NOT LIKE '%^%'
AND S.Name IN ('Initiated', 'Design Review', 'Change Pending Approval',
'Under Editing', 'Waiting for Approval', 'Under Change',
'ECO Design Review', 'Minor Change no Revision',)
AND W.Name IN ('Production')
ORDER BY
Filename
例如,如果我选择一个特定文件(见图),你可以看到结果:
您可以在上一个表中看到结果产生符合条件的3行,我试图找到一种简单的方法让它符合我的所有条件,然后只输出最高的TransitionNr或最新的日期。
我不是一个SQL用户,我把它放在谷歌搜索中 - 但我似乎无法找到最后一块。
感谢您的帮助。
答案 0 :(得分:1)
尝试在查询中用TH.Date
替换MAX(TH.Date)
的所有引用。
答案 1 :(得分:1)
快速解决方案是:
SELECT D.Filename, S.Name AS STATE, W.Name AS 'Workflow Name', U.Username AS 'Name',
CONVERT(VARCHAR, MAX(TH.DATE), 101) AS [Entered State],
DATEDIFF(dd, MAX(TH.DATE), GETUTCDATE()) AS [Days In State]
FROM STATUS S
INNER JOIN Documents D ON D.CurrentStatusID = S.StatusID
INNER JOIN Workflows W ON W.WorkflowID = S.WorkflowID
INNER JOIN TransitionHistory TH ON (D.DocumentID = TH.DocumentID AND D.LatestRevisionNo - 1 = TH.RevNr)
INNER JOIN Users AS U ON U.UserID = TH.UserID
WHERE d.Filename NOT LIKE '%test%'
AND d.filename NOT LIKE '%^%'
AND S.Name IN ('Initiated', 'Design Review', 'Change Pending Approval', 'Under Editing', 'Waiting for Approval', 'Under Change', 'ECO Design Review', 'Minor Change no Revision')
AND W.Name IN ('Production')
GROUP BY D.Filename, S.Name, W.Name, U.Username