简体   繁体   English

根据唯一列获取单行

[英]Getting a single row based on unique column

I think this is mostly a terminology issue, where I'm having a hard time articulating a problem. 我认为这主要是一个术语问题,在此我很难说明问题。

I've got a table with a couple columns that manage some historical log data. 我有一个带有几个列的表,这些列管理一些历史日志数据。 The two columns I'm interested in are timestamp(or Id, as the id is generated sequentially) and terminalID. 我感兴趣的两列是时间戳(或ID,因为ID是按顺序生成的)和terminalID。

I'd like to supply a list of terminal ids and find only the latest data, that is highest id or timestamp per terminalID 我想提供一个终端ID列表,仅查找最新数据,即每个终端ID最高的ID或时间戳


Ended up using group solution as @Danny suggested, and the other solution he referenced 最终按照@Danny的建议使用组解决方案,而他引用了另一个解决方案

I found the time difference to be quite noticeable, so I'm posting both results here for anyone's FYI. 我发现时差非常明显,因此我将两个结果都张贴在这里供任何人参考。

S1: S1:

SELECT UR.* FROM(
SELECT TerminalID, MAX(ID) as lID 
    FROM dbo.Results
    WHERE TerminalID in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)
    GROUP BY TerminalID
) GT left join dbo.Results UR on UR.id=lID

S2 S2

SELECT *
FROM (
   SELECT TOP 100
      Row_Number() OVER (PARTITION BY terminalID ORDER BY Id DESC) AS [Row], *
   FROM dbo.Results
   WHERE TerminalID in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)
   ORDER BY Id DESC
) a
WHERE a.row=1

the results were: 结果是:

S1: S1:

  • CPU time = 297 ms, elapsed time = 343 ms. CPU时间= 297毫秒,经过的时间= 343毫秒。
  • Query Cost 36% 查询费用36%
  • Missing index impact - 94% 缺少指标影响-94%

S2: S2:

  • CPU time = 562 ms, elapsed time = 1000 ms. CPU时间= 562毫秒,经过时间= 1000毫秒。
  • Query Cost 64% 查询费用64%
  • Missing index impact - 41% 缺少指标影响-41%

After adding the missing index to solution one (indexing ID only, as opposed to s2, where multiple columns needed an index), I got the query down to 15ms 将缺少的索引添加到解决方案一(仅索引ID,而不是s2,其中多列需要一个索引)之后,我将查询时间缩短到15ms

使用TOP关键字:

SELECT TOP 1 ID, terminalID FROM MyTable WHERE <your condition> ORDER BY <something that orders it like you need so that the correct top row is returned>.

I think you're on the right track with GROUP BY . 我认为您使用GROUP BY的方向正确。 Sounds like you want: 听起来像您想要的:

SELECT TerminalID, MAX(Timestamp) AS LastTimestamp
    FROM [Table_Name]
    WHERE TerminalID IN (.., .., .., ..)
    GROUP BY TerminalID

While not as obvious as using MAX with a GROUP BY , this can offer extra flexibility if you need to have more than one column determining which row or rows you want pulled back. 尽管不如将MAXGROUP BY ,但如果您需要多个列来确定要回退的行,则可以提供更大的灵活性。

SELECT *
FROM (
   SELECT
      Row_Number() OVER (PARTITION BY terminalID ORDER BY Id DESC) AS [Row],
      [terminalID],[Id],[timestamp]
   FROM <TABLE>
   ORDER BY Id DESC
) a
WHERE a.row=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM