我需要通过RelationShipId选择3个最新的唯一行(因此,没有重复的行),其中一个时间戳记与今天最接近。
我现在有一个看起来像这样的查询,但是它不起作用。
我得到一个::在选择列表中无效,因为它没有包含在聚合函数或GROUP BY子句中。
到目前为止我拥有的脚本:
SELECT * FROM PlayerAction
WHERE [ActionType] = 'SomeString' AND [RelationShipType] = 'SomeString' AND [PlayerId] = 5 AND [Deleted] = 0
GROUP BY RelationShipId
ORDER BY [TimeStamp] DESC
OFFSET 1 ROWS
FETCH NEXT 3 ROWS ONLY
SampleData
{
PlayerId=5
ActionType="SomeString"
RelationShipType="SomeString"
RelationShipId=1
TimeStamp=2019-07-04 07:45:47.400
Deleted=0
}
{
PlayerId=5
ActionType="SomeString"
RelationShipType="SomeString"
RelationShipId=2
TimeStamp=2019-07-03 14:29:10.530
Deleted=0
}
{
PlayerId=5
ActionType="SomeString"
RelationShipType="SomeString"
RelationShipId=3
TimeStamp=2019-07-03 13:56:26.057
Deleted=0
}
想象有5行具有相同的RelationShipId,无休止的RelationShipIds。我想要时间戳记的前3个唯一行,最后三个PlayerActions
答案 0 :(得分:2)
看来您需要与众不同的
SELECT distinct top 3 * FROM PlayerAction
WHERE [ActionType] = 'SomeString' AND [RelationShipType] = 'SomeString' AND [PlayerId] = SomeNumber AND [Deleted] = 0
ORDER BY [TimeStamp] DESC