从上一个可用值sql中获取最新值

wvyml7n5  于 2021-08-13  发布在  Java
关注(0)|答案(3)|浏览(276)

我有两张table
主人

MasterID PaperID Version PaperName
1        ISBN-1X 1       Medical Research
2        ISBN-1X 2       Medical Research(2)
3        ISBN-1X 3       Medical Research(2.1)
4        ISBN-2X 1       Some Research(1.0)
5        ISBN-3X 1       Unpublished Research(0.0)

论文作者

MasterId AuthorName
1        ABC
2        XYZ
4        LMN

注意:只有当author表的author与以前的版本不同时,才能获取条目。否则纸质版本在母版中更新,作者保持不变。
我如何得到最新版本的论文与最后更新作者
预期结果

MasterID PaperID Version PaperName                  AuthorName
3        ISBN-1X 3       Medical Research(2.1)      XYZ
4        ISBN-2X 1       Some Research(1.0)         LMN
5        ISBN-3X 1       Unpublished Research(0.0)

我的尝试:

Select M.MasterID, M.PaperID ,M.Version, M.PaperName, A.AuthorName
From Master M 
Inner Join 
(select  PaperID , max(version) maxversion from Master group by PaperID ) M1 
on m1.PaperID  = M.PaperID   AND M1.maxversion = M.Version
left join PaperAuthor A on M.MasterID  = A.MasterID

演示

8hhllhi2

8hhllhi21#

你可以用一个 join 以及一些窗口功能:

select top (1) with ties m.masterid, paperid, version, papername,
       max(authorname) over (partition by paperid, grp) as authorname
from (select m.*, pa.authorname,
             count(pa.authorname) over (partition by m.paperid order by m.version) as grp
      from master m left join
           paperauthor pa
           on pa.masterid = m.masterid
     ) m
order by row_number() over (partition by m.paperid order by m.version desc);

它使用 with ties 获取最新版本而不需要附加子查询的技巧。
这是一把小提琴。

baubqpgj

baubqpgj2#

用下列方法尝试 row_number ,这是演示。

;with mst as
(
  select
    *,
    row_number() over (partition by PaperID order by version desc) as rnk
  from master
),
ppauth as
(
  select 
    *,
    row_number() over (order by MasterID desc) as rnk
  from PaperAuthor
)

select
  m.MasterID,
  PaperID,
  Version,
  PaperName,
  AuthorName
from mst m
join ppauth p
on m.rnk = p.rnk
where m.rnk = 1

输出:

-------------------------------------------------------------
| MasterID  PaperID  Version     PaperName        AuthorName|
-------------------------------------------------------------
|  3        ISBN-1X    3    Medical Research(2.1)    XYZ    | 
-------------------------------------------------------------
wlzqhblo

wlzqhblo3#

你可以试试下面的查询。它将最近的作者与每个isbn的最新图书数据对应起来。

;WITH CTE_Master AS
(
SELECT *, ROW_NUMBER() OVER(PARTITION BY PaperID ORDER BY Version DESC) AS recentOrder
FROM Master
)
SELECT c.MasterId, c.PaperId, c.Version, c.PaperName, pa.AuthorName
FROM CTE_Master as c
CROSS APPLY
(SELECT TOP 1 MasterId, AuthorName From PaperAuthor WHERE MasterId <= c.MasterId ORDER BY MasterId Desc) as pa(MasterId,AuthorName)
WHERE c.recentOrder = 1
+----------+---------+---------+-----------------------+------------+
| MasterId | PaperId | Version |       PaperName       | AuthorName |
+----------+---------+---------+-----------------------+------------+
|        3 | ISBN-1X |       3 | Medical Research(2.1) | XYZ        |
|        4 | ISBN-2X |       1 | Medical Research(2.1) | LMN        |
+----------+---------+---------+-----------------------+------------+

更新:如果你想要空作者也

;WITH CTE_Master AS
(
SELECT *, ROW_NUMBER() OVER(PARTITION BY PaperID ORDER BY Version DESC) AS recentOrder
FROM Master
)
SELECT c.MasterId, c.PaperId, c.Version, c.PaperName, pa.AuthorName
FROM CTE_Master as c
CROSS APPLY
(SELECT TOP 1 MasterId, AuthorName From PaperAuthor WHERE MasterId <= c.MasterId ORDER BY MasterId Desc) as pa(MasterId,AuthorName)
WHERE c.recentOrder = 1 and c.MasterId <= (SELECT MAX(MasterId) FROM PaperAuthor)
UNION ALL
SELECT c.MasterId, c.PaperId, c.Version, c.PaperName, NULL as AuthorName
FROM CTE_Master as c
WHERE c.MasterId > (SELECT MAX(MasterId) FROM PaperAuthor)
+---------+---------+---------+---------------------------+------------+
| asterId | PaperId | Version |         PaperName         | AuthorName |
+---------+---------+---------+---------------------------+------------+
|       3 | ISBN-1X |       3 | Medical Research(2.1)     | XYZ        |
|       4 | ISBN-2X |       1 | Medical Research(2.1)     | LMN        |
|       5 | ISBN-3X |       1 | Unpublished Research(0.0) | null       |
+---------+---------+---------+---------------------------+------------+

相关问题