TSQL-加入自己的表,仅选择最新行
我有一个表 tbl_Partner
(缩短),其中有两个“重复”合作伙伴:
RecordID (GUID) | RISEID (varchar(40)) | UpdateCounter(int) | etc. ...
-----------------------------------------------------------
12345678-9012-3456-7890-123456789012 | RISE123 | 0
87564321-9012-3456-7890-123456789012 | RISE123 | 1
-- other partners with different updatecounters etc.
现在我需要一个视图,其中包含每个合作伙伴的最新数据(最高的 UpdateCounter),但第一个 RecordID(最低的 UpdateCounter)
我尝试了这个SQL 已经:
select
(select top 1 recordid from tbl_partner p2 where p2.riseid=p1.riseid order by p2.updatecounter ASC) as RecordID
, riseid, updatecounter
from tbl_partner p1
order by riseid
我得到:
RecordID (GUID) | RISEID (varchar(40)) | UpdateCounter(int)
-----------------------------------------------------------
12345678-9012-3456-7890-123456789012 | RISE123 | 0 --(old data)
12345678-9012-3456-7890-123456789012 | RISE123 | 1 --(new correct data with the first RecordID)
-- other rows/columns omitted
如何仅为每个 RecordID/RiseID 选择最新行? (本例中 UpdateCounter=1)
郑重声明: UpdateCounter 当然可以是 1 以外的值,并且还有其他具有不同值的数据集。所以我不能使用简单的 WHERE updatecounter>0
。
I have a table tbl_Partner
(shortened) with two "duplicate" partners:
RecordID (GUID) | RISEID (varchar(40)) | UpdateCounter(int) | etc. ...
-----------------------------------------------------------
12345678-9012-3456-7890-123456789012 | RISE123 | 0
87564321-9012-3456-7890-123456789012 | RISE123 | 1
-- other partners with different updatecounters etc.
Now I need a view which has the latest data for each partner (highest UpdateCounter) but the first RecordID (lowest UpdateCounter)
I tried this SQL already:
select
(select top 1 recordid from tbl_partner p2 where p2.riseid=p1.riseid order by p2.updatecounter ASC) as RecordID
, riseid, updatecounter
from tbl_partner p1
order by riseid
I get:
RecordID (GUID) | RISEID (varchar(40)) | UpdateCounter(int)
-----------------------------------------------------------
12345678-9012-3456-7890-123456789012 | RISE123 | 0 --(old data)
12345678-9012-3456-7890-123456789012 | RISE123 | 1 --(new correct data with the first RecordID)
-- other rows/columns omitted
How can I select the latest row for each RecordID/RiseID only? (UpdateCounter=1 in this example)
For the record: UpdateCounter can be something else than 1 of course and there are other datasets with different values. So I cannot use a simple WHERE updatecounter>0
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前尚不清楚您是否希望为只有单行具有特定
RISEID
的项目显示行。如果不是,请在t1
和t2
之间的连接中添加一个额外条件,以便UpdateCounter
在t2
上更高>。两个外连接是为了确保不存在早于
t1
(t1_anti
) 的行,也不存在晚于t2
(t2_anti )。在
WHERE
子句中,我们确保这些连接不成功。另一种方法,使用稍微现代的风格:
这可能需要更少的桌子扫描。
It's not clear if you want rows to appear for items where only a single row has a particular
RISEID
. If not, add an extra condition into the join betweent1
andt2
, such that theUpdateCounter
is higher ont2
.The two outer joins are to ensure that there is no earlier row than
t1
(t1_anti
) and no later row thant2
(t2_anti
). In theWHERE
clause, we ensure that those joins were unsuccessful.Alternate one, using slightly more modern style:
which may require fewer scans on the table.
选择最大更新计数器
Select Max UpdateCounter