如何根据excel或sql中的日期从一组列中获取前N条记录?

时间:2016-11-08 18:05:29

标签: excel oracle-sqldeveloper

我有一张表:

Sr.no .....产品代码...产品ID ...地区... Year_month

1 .................. XXX ................... 123 ....... .....北.......... 201605 2 .................. XXX ................... 123 .......... ..North .......... 201604 3 .................. YYY ................... 124 .......... ..South .......... 201510 4 .................. YYY ................... 124 .......... ..South .......... 201509 5 .................. YYY ................... 124 .......... ..South .......... 201507 6 .................. ZZZ ................... 125 .......... ..West ........... 201612 7 .................. ZZZ ................... 125 .......... ..West ........... 201611 8 .................. ZZZ ................... 125 .......... ..West 201604 ........... 9 .................. ZZZ ................... 125 .......... ..West ........... 201603

产品代码组,ProductID,Region是唯一的,我需要每组前n个记录数,连续减少数月没有中断,所以我的输出将是:

Sr.no .....产品代码...产品ID ...地区... Year_month 1 .................. XXX ................... 123 .......... ..North .......... 201605 2 .................. XXX ................... 123 .......... ..North .......... 201604 3 .................. YYY ................... 124 .......... ..South .......... 201510 4 .................. YYY ................... 124 .......... ..South .......... 201509 5 .................. ZZZ ................... 125 .......... ..West ........... 201612 6 .................. ZZZ ................... 125 .......... ..West ........... 201611

2 个答案:

答案 0 :(得分:0)

根据您的示例数据,看起来Sr.no是生成的值而不是记录数据的一部分。以下查询将在子因子查询t1中生成分区行号。然后输出查询将在返回数据时添加Sr.no行号,并将限制每组返回的行数:

with t1 as (
 SELECT Product_Code
      , Product_ID
      , Region
      , Year_month
      , row_number() over (partition by product_code, prodcut_id, region order by year_month desc) rn
   FROM your_data
)
 select row_number() over (order by product_code, prodcut_id, region, year_month desc) Sr_no
      , Product_Code
      , Product_ID
      , Region
      , Year_month
   from t1
  where rn <= 2;

要返回在product_code,product_id和region上分区的前N个连续Year_Month记录,您可以使用Tabibitosan method,通过计算行号和序列号对记录进行分组,然后减去一个从另一个。行号很简单,可以使用ROW_NUMBER()分析函数计算。序列需要是某个函数,对于当前组中的每个记录,该函数递增1。对于您的数据,我们可以将MONTHS_BETWEEN()函数与分析MAX(year_month)结合使用。从RN中减去SEQ会产生GRP。现在只想获得第一组,您需要做的就是限制为GRP=0

with your_data(Sr_no, Product_Code, Product_ID, Region, Year_month) as (
  select 1, 'XXX', 123, 'North', date '2016-05-01' from dual union all
  select 2, 'XXX', 123, 'North', date '2016-04-01' from dual union all
  select 3, 'YYY', 124, 'South', date '2015-10-01' from dual union all
  select 4, 'YYY', 124, 'South', date '2015-09-01' from dual union all
  select 5, 'YYY', 124, 'South', date '2015-07-01' from dual union all
  select 6, 'ZZZ', 125, 'West', date '2016-12-01' from dual union all
  select 7, 'ZZZ', 125, 'West', date '2016-11-01' from dual union all
  select 8, 'ZZZ', 125, 'West', date '2016-04-01' from dual union all
  select 9, 'ZZZ', 125, 'West', date '2016-03-01' from dual
), t1 as (
 SELECT Product_Code
      , Product_ID
      , Region
      , Year_month
      , row_number()
        over (partition by product_code, product_id, region
                  order by year_month desc) rn
      , months_between( max(year_month)
                        over (partition by product_code, product_id, region)
                      , year_month) + 1 seq
      , months_between( max(year_month)
                        over (partition by product_code, product_id, region)
                      , year_month) + 1
      - row_number()
        over (partition by product_code, product_id, region
                  order by year_month desc) grp
   FROM your_data
)
select * from t1 where grp = 0;

PRO PRODUCT_ID REGIO YEAR_MONTH                   RN        SEQ        GRP
--- ---------- ----- -------------------- ---------- ---------- ----------
XXX        123 North 01-MAY-2016 00:00:00          1          1          0
XXX        123 North 01-APR-2016 00:00:00          2          2          0
YYY        124 South 01-OCT-2015 00:00:00          1          1          0
YYY        124 South 01-SEP-2015 00:00:00          2          2          0
ZZZ        125 West  01-DEC-2016 00:00:00          1          1          0
ZZZ        125 West  01-NOV-2016 00:00:00          2          2          0

 6 rows selected 

答案 1 :(得分:0)

在Excel中,在H列中设置包含公式

的辅助列
=IF(OR(B2&C2&D2<>B1&C1&D1,AND(EDATE(E2,1)=E1,H1<>"")),A2,"")

并过滤此列,不包括空格。

这假设您的数据按照示例中显示的方式排序,并且日期是实际日期。

enter image description here