如何只保留 sql 中的最近日期并根据条件从 table 中删除其余日期?
How to keep only the recent date in sql and delete the rest from the table based on conditions?
我有一个 SQL table,我的数据如下所示:
PAN_NO |NIFTY_TREND | COUNT_OF_TREND | PURCHASE_DATE | NEW_SCH_CODE
XXX | 011 | 1 | 29-SEP-16 | 168
YYY | 111 | 1 | 02-JUN-17 | 168
ZZZ | 011 | 3 | 27-OCT-14 | 178
ZZZ | 011 | 3 | 28-JUN-17 | 168
ZZZ | 011 | 3 | 28-JUN-17 | 04
预期输出条件:
对于每个平底锅,如果 count_of_trend 大于 1,那么我应该保留最近的记录
购买日期并删除其余部分。如果最近的购买日期也相同,则可以选择2个中的任何一个。
Expected Output :
PAN_NO |NIFTY_TREND | COUNT_OF_TREND | PURCHASE_DATE | NEW_SCH_CODE
XXX | 011 | 1 | 29-SEP-16 | 168
YYY | 111 | 1 | 02-JUN-17 | 168
ZZZ | 011 | 3 | 28-JUN-17 | 168
我试过这样做:
select pan_no, nifty_trend, count_of_trend, max(purchase_date) over (partition by pan_no)
from table group by pan_no having count_of_trend >1
但是上面的代码不起作用。
我正在 sql 开发人员中执行所有这些。
对我来说,它看起来像这样:
示例数据:
SQL> with test (pan_no, nifty_trent, count_of_trend, purchase_date, new_sch_code) as
2 (select 'xxx', '011', 1, date '2016-09-29', 168 from dual union all
3 select 'yyy', '111', 1, date '2017-06-02', 168 from dual union all
4 select 'zzz', '011', 3, date '2014-10-27', 178 from dual union all
5 select 'zzz', '011', 3, date '2017-06-28', 168 from dual union all
6 select 'zzz', '011', 3, date '2017-06-28', 4 from dual
7 ),
查询从这里开始; temp
CTE 按 purchase_date
降序“排序”每个 pan_no
的行;最后的 select
然后 returns 其 rn = 1
的行(即它们排名为“最高”),或 count_of_trend = 1
(因为 sorting 只能应用于 count_of_trend
大于 1 的行)。
8 temp as
9 (select t.*,
10 row_number() over (partition by pan_no order by purchase_date desc) rn
11 from test t
12 )
13 select pan_no, nifty_trent, count_of_trend, purchase_date, new_sch_code
14 from temp
15 where rn = 1
16 or count_of_trend = 1;
PAN NIF COUNT_OF_TREND PURCHASE_D NEW_SCH_CODE
--- --- -------------- ---------- ------------
xxx 011 1 29.09.2016 168
yyy 111 1 02.06.2017 168
zzz 011 3 28.06.2017 4
SQL>
我有一个 SQL table,我的数据如下所示:
PAN_NO |NIFTY_TREND | COUNT_OF_TREND | PURCHASE_DATE | NEW_SCH_CODE
XXX | 011 | 1 | 29-SEP-16 | 168
YYY | 111 | 1 | 02-JUN-17 | 168
ZZZ | 011 | 3 | 27-OCT-14 | 178
ZZZ | 011 | 3 | 28-JUN-17 | 168
ZZZ | 011 | 3 | 28-JUN-17 | 04
预期输出条件:
对于每个平底锅,如果 count_of_trend 大于 1,那么我应该保留最近的记录 购买日期并删除其余部分。如果最近的购买日期也相同,则可以选择2个中的任何一个。
Expected Output :
PAN_NO |NIFTY_TREND | COUNT_OF_TREND | PURCHASE_DATE | NEW_SCH_CODE
XXX | 011 | 1 | 29-SEP-16 | 168
YYY | 111 | 1 | 02-JUN-17 | 168
ZZZ | 011 | 3 | 28-JUN-17 | 168
我试过这样做:
select pan_no, nifty_trend, count_of_trend, max(purchase_date) over (partition by pan_no)
from table group by pan_no having count_of_trend >1
但是上面的代码不起作用。
我正在 sql 开发人员中执行所有这些。
对我来说,它看起来像这样:
示例数据:
SQL> with test (pan_no, nifty_trent, count_of_trend, purchase_date, new_sch_code) as
2 (select 'xxx', '011', 1, date '2016-09-29', 168 from dual union all
3 select 'yyy', '111', 1, date '2017-06-02', 168 from dual union all
4 select 'zzz', '011', 3, date '2014-10-27', 178 from dual union all
5 select 'zzz', '011', 3, date '2017-06-28', 168 from dual union all
6 select 'zzz', '011', 3, date '2017-06-28', 4 from dual
7 ),
查询从这里开始; temp
CTE 按 purchase_date
降序“排序”每个 pan_no
的行;最后的 select
然后 returns 其 rn = 1
的行(即它们排名为“最高”),或 count_of_trend = 1
(因为 sorting 只能应用于 count_of_trend
大于 1 的行)。
8 temp as
9 (select t.*,
10 row_number() over (partition by pan_no order by purchase_date desc) rn
11 from test t
12 )
13 select pan_no, nifty_trent, count_of_trend, purchase_date, new_sch_code
14 from temp
15 where rn = 1
16 or count_of_trend = 1;
PAN NIF COUNT_OF_TREND PURCHASE_D NEW_SCH_CODE
--- --- -------------- ---------- ------------
xxx 011 1 29.09.2016 168
yyy 111 1 02.06.2017 168
zzz 011 3 28.06.2017 4
SQL>