获取项目ID的最新标记

时间:2014-09-30 08:52:20

标签: sql oracle

我想为每个ProjectID获取最新的tag_Values。

此刻我全力以赴:

SELECT t.tag_value, t.tag_desc, u.update_dat_ins AS INSETION_DATE, p.structure_id AS PROJECT_ID
 FROM updated u 
 JOIN project p ON p.project_id = u.project_id  
 JOIN tag t ON t.tag_id = u.tag_id 
 WHERE t.tag_desc LIKE 'Equity%';

如何仅为每个INSERTION_DATE获取最新的(tag_valuesproductID个?

更新

+----------------------------------------------+
| TAG_VALUE TAG_DESC INSERTION_DATE PROJECT_ID |
+----------------------------------------------+
| 200         Equity  17-DEC-13       1        |
| 10012       Equity  14-DEC-14       1        |
| 312         Equity  12-DEC-12       2        |
| 13415       Equity  16-DEC-11       5        |
| 13415       Equity  17-DEC-11       5        |
| 13415       Equity  18-SEPT-13      2        |
| 13415       Equity  16-OCT-10       9        |
+----------------------------------------------+

预期结果:

+----------------------------------------------+
| TAG_VALUE TAG_DESC INSERTION_DATE PROJECT_ID |
+----------------------------------------------+
| 10012       Equity  14-DEC-14       1        |
| 13415       Equity  17-DEC-11       5        |
| 13415       Equity  18-SEPT-13      2        |
| 13415       Equity  16-OCT-10       9        |
+----------------------------------------------+

1 个答案:

答案 0 :(得分:1)

您需要使用ANALYTIC

类似的东西:

ROW_NUMBER() OVER(PARTITION BY project_id ORDER BY insertion_date DESC) rn

在外部查询中,将WHERE条件添加为WHERE rn = 1以获取最新的tag_value。

更新:根据要求并根据OP的新输入添加了完整的测试用例

SQL> WITH DATA AS(
  2  SELECT 200 TAG_VALUE,    'Equity' TAG_DESC, TO_DATE('17-DEC-13','DD-MON-RR') INSERTION_DATE, 1 PROJECT_ID FROM DUAL UNION ALL
  3  SELECT 10012 TAG_VALUE, 'EQUITY' TAG_DESC, TO_DATE('14-DEC-14','DD-MON-RR') INSERTION_DATE, 1 PROJECT_ID FROM DUAL UNION ALL
  4  SELECT 312 TAG_VALUE,   'EQUITY' TAG_DESC, TO_DATE('12-DEC-12','DD-MON-RR') INSERTION_DATE, 2 PROJECT_ID FROM DUAL UNION ALL
  5  SELECT 13415  TAG_VALUE,'EQUITY' TAG_DESC, TO_DATE('16-DEC-11','DD-MON-RR') INSERTION_DATE, 5 PROJECT_ID FROM DUAL UNION ALL
  6  SELECT 13415 TAG_VALUE,'EQUITY' TAG_DESC, TO_DATE('17-DEC-11','DD-MON-RR') INSERTION_DATE, 5 PROJECT_ID FROM DUAL UNION ALL
  7  SELECT 3415 TAG_VALUE, 'EQUITY' TAG_DESC, TO_DATE('18-SEP-13','DD-MON-RR') INSERTION_DATE, 2 PROJECT_ID FROM DUAL UNION ALL
  8  SELECT 3415  TAG_VALUE,'EQUITY' TAG_DESC, TO_DATE('16-OCT-10','DD-MON-RR') INSERTION_DATE, 9 PROJECT_ID FROM DUAL)
  9  SELECT TAG_VALUE, TAG_DESC, INSERTION_DATE, PROJECT_ID FROM(
 10  SELECT A.*,
 11    ROW_NUMBER() OVER(PARTITION BY PROJECT_ID ORDER BY INSERTION_DATE DESC) RN
 12  FROM data A)
 13  WHERE RN = 1
 14  /

 TAG_VALUE TAG_DE INSERTION PROJECT_ID
---------- ------ --------- ----------
     10012 EQUITY 14-DEC-14          1
      3415 EQUITY 18-SEP-13          2
     13415 EQUITY 17-DEC-11          5
      3415 EQUITY 16-OCT-10          9

SQL>

更新#2:在OP的请求中用单词解释查询

如果您看到内部查询,则ROW_NUMBER()分析函数会为按PROJECT_ID DESCENDING顺序排序的每个INSERTION_DATE组分配一个排名。因此,最近的记录,即具有最新日期的行将在每个project_id上位于顶部。

最后,在外部查询中,有一个过滤器,只选择排名为1的那些行。

因此,您最终只能获得每个project_id具有最新inserted_date的记录。