我正在为我的团队创建一个项目数据库。每个项目都位于一个名为PROJECT的表中,我有一个名为PROGRESSUPDATES的单独表来跟踪用户添加到项目中的任何进度。
我想在交互式报告中显示每个项目的最新更新。
数据样本是:
项目:
id | NAME | Description | Status
______________________
1 | Project 1 | abc | On Track
2 | Project 2 | def | On Hold
3 | Project 3 | ghi | Completed
4 | Project 4 | jkl | Cancelled
5 | Project 5 | mno | On Track
PROGRESSUPDATES:
id | NAME | Update | Date
______________________
1 | Project 1 | Update 1 of 3 | 04-APR-16 11.29.00.000000 AM
1 | Project 1 | Update 2 of 3 | 05-APR-16 11.29.00.000000 AM
1 | Project 1 | Update 3 of 3 | 10-APR-16 11.29.00.000000 AM
4 | Project 4 | Update for P4 | 29-MAR-16 01.30.00.000000 PM
4 | Project 4 | New Update for P4 | 05-APR-16 01.30.00.000000 PM
5 | Project 5 | Update for P5 | 05-APR-16 11.47.00.000000 AM
id | NAME | Description | Status | Latest Update
______________________
1 | Project 1 | abc | On Track | Update 3 of 3
2 | Project 2 | def | On Hold | No update found.
3 | Project 3 | ghi | Completed | No update found.
4 | Project 4 | jkl | Cancelled | New update for P4
5 | Project 5 | mno | On Track | Update for P5
答案 0 :(得分:0)
管理是为了让一些同事在工作中解决这个问题,并希望分享代码,以防其他人帮助他人。
SELECT a.* from (
select
ID,
NAME,
DESCRIPTION,
STATUS,
UPDATE AS "Latest Update",
DATE AS "Update Date",
ROW_NUMBER() OVER (PARTITION BY PROJECTS.ID ORDER BY DATE DESC) AS RN
from "PROJECTS"
LEFT outer join PROGRESSUPDATES
ON PROJECTS.ID = PROGRESSUPDATES.ID
)a
WHERE a.RN =1