协助进行SQL查询以返回具有最新时间戳的重复对象的记录

时间:2018-08-29 20:36:14

标签: sql-server

我正在尝试针对包含特定值的表输出结果,并且该表仅引用最新的时间戳记

下面的示例是我正在运行的SQL查询-

SELECT id, componentNameId, attrName, attrVal, created 
FROM APP_SubClientProp
WHERE attrName = 'sys:UseMultipleStreamsWithinDrive' AND attrVal = 1
ORDER BY componentName

输出将显示以下结果-

id    componentNameId attrName                          attrVal   created
46397     310         sys:UseMultipleStreamsWithinDrive    1      1534884250
46402     310         sys:UseMultipleStreamsWithinDrive    1      1534885567
46404     310         sys:UseMultipleStreamsWithinDrive    1      1534887691
36458    1141         sys:UseMultipleStreamsWithinDrive    1      1530825395

问题是...。如何概述结果以仅显示最新时间戳?我尝试将MAX属性与创建的列一起使用,但它仅显示针对sys:UseMultipleStreamsWithinDrive属性显示的最新行

请帮助!

2 个答案:

答案 0 :(得分:0)

它不允许我发表评论。 这不是您想要的吗?:

SELECT max(created)
FROM APP_SubClientProp
WHERE attrName = 'sys:UseMultipleStreamsWithinDrive' AND attrVal = 1

或者这个:

SELECT id, componentNameId, attrName, attrVal, created 
FROM APP_SubClientProp app_sub
LEFT JOIN
(SELECT max(created) max_dt
FROM APP_SubClientProp
WHERE attrName = 'sys:UseMultipleStreamsWithinDrive' AND attrVal = 1)tb2
on tb2.max_dt on app_sub.created
WHERE tb2.max_dt = app_sub.created

答案 1 :(得分:0)

尝试添加GROUP BY子句吗?

SELECT id, componentNameId, attrName, attrVal, max(created)
FROM APP_SubClientProp
WHERE attrName = 'sys:UseMultipleStreamsWithinDrive' AND attrVal = 1
GROUP BY  id, componentNameId, attrName, attrVal

如果这不能满足您的要求,则需要准确显示查询所需的输出。否则,我们只是在猜测。