如何在oracle中获取最近两年的年度数据

时间:2018-04-12 05:17:10

标签: sql oracle oracle11g

我的项目有一个名为sales_data的表。表格结构如下。

ID  Name    Year    Sales
1000    ABC 2016    50000
1000    ABC 2017    80000
1000    ABC 2015    90000
1000    ABC 2014    45000
1000    ABC 2013    30000
2000    PQR 2017    80000
2000    PQR 2015    90000
2000    PQR 2014    75000
2000    PQR 2013    60000
3000    XYZ 2015    123000
3000    XYZ 2013    56000
3000    XYZ 2012    45000
3000    XYZ 2011    30000

从这张表中。我想获取最新的连续年度数据。我的结果集必须如下所示

ID  Name    Year    Sales
1000    ABC 2017    80000
1000    ABC 2016    50000
2000    PQR 2017    80000
2000    PQR 2016    0
3000    XYZ 2015    123000
3000    XYZ 2014    0

因为对于PQR公司来说,最新的数据是2017年所以它是80000但是2016年的数据不存在于此表中所以它需要为0。 与XYZ公司类似。

请帮助我。我正在使用Oracle 11g。

4 个答案:

答案 0 :(得分:2)

你可以试试这个。

SELECT  t2.ID,t2.Name,t2.years,COALESCE(t1.Sales,0) Sales
FROM sales_data t1
RIGHT JOIN
(
  SELECT ID,Name,MAX(Year)   years
  FROM sales_data
  GROUP BY ID,Name
  UNION ALL 
  SELECT ID,Name,MAX(Year) - 1  years
  FROM sales_data
  GROUP BY ID,Name
) t2 ON t1.ID = t2.ID AND t1.Name = t2.Name AND  t1.Year = t2.years 
ORDER BY t2.ID,t2.years DESC

sqlfiddle:http://sqlfiddle.com/#!4/4fc37c/5

答案 1 :(得分:0)

WITH Years AS
(
  SELECT ID,Name,MAX(Year) AS year
  FROM Table1
  GROUP BY ID,Name
  UNION ALL
  SELECT ID,Name,MAX(Year) - 1 AS year
  FROM Table1
  GROUP BY ID,Name
)
SELECT  y.ID,
        y.Name,
        y.year,
        COALESCE(t.Sales,0) AS Sales
FROM Years y
LEFT JOIN Table1 t
ON t.ID = y.ID 
AND t.Name = y.Name 
AND t.Year = y.year 
ORDER BY y.ID,y.year DESC 

演示

  

http://sqlfiddle.com/#!4/c20fd/21

答案 2 :(得分:0)

又一个选择:

SQL> with test (id, year, sales) as
  2    (select 1000, 2016, 50000 from dual union
  3     select 1000, 2017, 80000 from dual union
  4     select 1000, 2015, 90000 from dual union
  5     select 1000, 2014, 45000 from dual union
  6     --
  7     select 2000, 2017, 80000 from dual union
  8     select 2000, 2015, 90000 from dual union
  9     select 2000, 2014, 75000 from dual union
 10     --
 11     select 3000, 2015, 123000 from dual union
 12     select 3000, 2013, 56000 from dual union
 13     select 3000, 2012, 45000 from dual
 14    ),
 15  years as
 16    (select id, min_year + column_value - 1 year
 17     from (select id, min(year) min_year, max(year) max_year
 18           from test
 19           group by id
 20          ),
 21          table(cast(multiset(select level from dual
 22                              connect by level <= max_year - min_year + 1
 23                             ) as sys.odcinumberlist))
 24    ),
 25  ranking as
 26    (select y.id, y.year, nvl(t.sales, 0) sales,
 27       row_number() over (partition by y.id order by y.year desc) rn
 28     from years y left join test t on t.id = y.id and t.year = y.year
 29    )
 30  select r.id, r.year, r.sales
 31  from ranking r
 32  where r.rn <= 2
 33  order by r.id, r.year desc;

        ID       YEAR      SALES
---------- ---------- ----------
      1000       2017      80000
      1000       2016      50000
      2000       2017      80000
      2000       2016          0
      3000       2015     123000
      3000       2014          0

6 rows selected.

SQL>

答案 3 :(得分:0)

试试这个

SELECT sd1.ID,  sd1.name, sd2.year, COALESCE(sd3.sales,0) 
FROM
(SELECT distinct ID, name from sales_data) as sd1 join 
(SELECT distinct year from sales_data ) as sd2
Left join sales_data sd3
on sd1.ID = sd3.ID AND sd2.year = sd3.year