【发布时间】:2021-04-20 09:11:50
【问题描述】:
要创建的列使用列作者类型进行排名。
例子:
| PMID | Rank |
|---|---|
| 200 | 3 |
| 201 | 0 |
| 200 | 0 |
| 202 | 0 |
| 200 | 2 |
| 201 | 1 |
| 200 | 1 |
预期:
| PMID | Rank | Author_type |
|---|---|---|
| 200 | 3 | Last Author |
| 201 | 0 | First Author |
| 200 | 0 | First Author |
| 202 | 0 | First Author |
| 200 | 2 | Co Author |
| 201 | 1 | Last Author |
| 200 | 1 | Co Author |
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [datacleaning].[pub_set_authors]
AS
BEGIN
WITH cte AS
(
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY [pub_id] ORDER BY [rank] DESC) AS rnk
FROM
datacleaning.pubmed_details
)
UPDATE datacleaning.pubmed_details
SET author_type = 'Last Author'
WHERE row_id IN (SELECT row_id
FROM cte
WHERE rnk = 1)
UPDATE datacleaning.pubmed_details
SET author_type = 'First Author'
WHERE rank = 0;
UPDATE datacleaning.pubmed_details
SET author_type = 'Co Author'
WHERE author_type is NULL;
END
【问题讨论】:
标签: python sql pandas dataframe