TSQL-加入自己的表,仅选择最新行

发布于 2024-12-06 20:12:03 字数 1217 浏览 0 评论 0原文

我有一个表 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耳根太软 2024-12-13 20:12:03
SELECT t1.RecordID,t1.RISEID,t2.UpdateCounter
FROM
   tbl_Partner t1
      inner join
   tbl_Partner t2
      on
          t1.RISEID = t2.RISEID
      left join
   tbl_Partner t1_anti
      on
          t1.RISEID = t1_anti.RISEID and
          t1_anti.UpdateCounter < t1.UpdateCounter
      left join
   tbl_Partner t2_anti
      on
          t1.RISEID = t2_anti.RISEID and
          t2_anti.UpdateCounter > t2.UpdateCounter
where
    t1_anti.RISEID is null and
    t2_anti.RISEID is null

目前尚不清楚您是否希望为只有单行具有特定 RISEID 的项目显示行。如果不是,请在 t1t2 之间的连接中添加一个额外条件,以便 UpdateCountert2 上更高>。

两个外连接是为了确保不存在早于 t1 (t1_anti) 的行,也不存在晚于 t2 (t2_anti )。在 WHERE 子句中,我们确保这些连接不成功。


另一种方法,使用稍微现代的风格:

;With OrderedRows as (
    Select RecordID,RISEID,UpdateCounter,
      ROW_NUMBER() OVER (PARTITION BY RISEID ORDER BY UpdateCounter ASC) as rnEarly,
      ROW_NUMBER() OVER (PARTITION BY RISEID ORDER BY UpdateCounter DESC) as rnLate
    from tbl_Partner
)
SELECT t1.RecordID,t1.RISEID,t2.UpdateCounter
FROM
   OrderedRows t1
       inner join
   OrderedRows t2
       on
            t1.RISEID = t2.RISEID
WHERE
    t1.rnEarly = 1 and
    t2.rnLate = 1

这可能需要更少的桌子扫描。

SELECT t1.RecordID,t1.RISEID,t2.UpdateCounter
FROM
   tbl_Partner t1
      inner join
   tbl_Partner t2
      on
          t1.RISEID = t2.RISEID
      left join
   tbl_Partner t1_anti
      on
          t1.RISEID = t1_anti.RISEID and
          t1_anti.UpdateCounter < t1.UpdateCounter
      left join
   tbl_Partner t2_anti
      on
          t1.RISEID = t2_anti.RISEID and
          t2_anti.UpdateCounter > t2.UpdateCounter
where
    t1_anti.RISEID is null and
    t2_anti.RISEID is null

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 between t1 and t2, such that the UpdateCounter is higher on t2.

The two outer joins are to ensure that there is no earlier row than t1 (t1_anti) and no later row than t2 (t2_anti). In the WHERE clause, we ensure that those joins were unsuccessful.


Alternate one, using slightly more modern style:

;With OrderedRows as (
    Select RecordID,RISEID,UpdateCounter,
      ROW_NUMBER() OVER (PARTITION BY RISEID ORDER BY UpdateCounter ASC) as rnEarly,
      ROW_NUMBER() OVER (PARTITION BY RISEID ORDER BY UpdateCounter DESC) as rnLate
    from tbl_Partner
)
SELECT t1.RecordID,t1.RISEID,t2.UpdateCounter
FROM
   OrderedRows t1
       inner join
   OrderedRows t2
       on
            t1.RISEID = t2.RISEID
WHERE
    t1.rnEarly = 1 and
    t2.rnLate = 1

which may require fewer scans on the table.

感性不性感 2024-12-13 20:12:03

选择最大更新计数器

 select distinct(
 (select top 1 recordid from tbl_partner p2 where p2.riseid=p1.riseid p2.updatecounter ASC
 ) )as RecordID,(select  Max(updatecounter) from tbl_partner p2 where p2.riseid=p1.riseid   ) as MaxUpdateCounter , riseid  from tbl_partner p1

Select Max UpdateCounter

 select distinct(
 (select top 1 recordid from tbl_partner p2 where p2.riseid=p1.riseid p2.updatecounter ASC
 ) )as RecordID,(select  Max(updatecounter) from tbl_partner p2 where p2.riseid=p1.riseid   ) as MaxUpdateCounter , riseid  from tbl_partner p1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文