如何在oracle sql开发人员按年和月分组获取当前财政年度的数据?

问题描述 投票:1回答:2

我试图获取当前财政年度和上一个财政年度的月度总金额。我不能硬编码日期。它应该基于当前日期。日期列的格式如下为13-JAN-10,金额为整数格式。

我希望它采用以下格式:

Month       2016-17   2017-18

June         17890    50980
July         45900    14879
August
September
October
November
December
January
February
March 
April
May

我无法超越我写的当前查询

SELECT
   CASE WHEN MONTH(sysdate)>=7 THEN
          concat(YEAR(sysdate), '-',YEAR(sysdate)+1)
   ELSE concat(YEAR(sysdate)-1,'-', YEAR(sysdate)) END AS financial_year,
   SUM(AMOUNT)
FROM TABLE a
GROUP BY financial_year ;
sql oracle
2个回答
0
投票

我没有太认真地检查我的日期算术,但希望这个例子能让你知道如何解决它

设置一些测试数据:

SQL> exec dbms_random.seed(0)

PL/SQL procedure successfully completed.

--
-- 1000 random dates and values
--
SQL> create table t as
  2  select date '2016-01-01' + trunc(dbms_random.value(1,800)) dte,
  3        rownum amount
  4  from dual
  5  connect by level <= 1000;

Table created.

--
-- sample data at the extrema
--

SQL>
SQL> select * from t
  2  order by 1
  3  fetch first 10 rows only;

DTE           AMOUNT
--------- ----------
02-JAN-16        562
03-JAN-16        486
04-JAN-16        843
05-JAN-16        661
06-JAN-16        255
06-JAN-16        382
07-JAN-16         31
07-JAN-16        506
07-JAN-16        290
07-JAN-16        185

10 rows selected.

SQL>
SQL> select * from t
  2  order by 1 desc
  3  fetch first 10 rows only;

DTE           AMOUNT
--------- ----------
10-MAR-18        978
09-MAR-18        262
08-MAR-18        295
07-MAR-18         33
06-MAR-18        469
03-MAR-18        454
03-MAR-18        593
03-MAR-18        538
02-MAR-18        226
02-MAR-18        928

10 rows selected.

SQL>

这是查询。 fin_start定义了鳍年的开始(因为它不是每个人的6月:-))

SQL> with fin_start as
  2   ( select date '2017-06-01' fs from dual )
  3  select mth, last_fin_year, this_fin_year
  4  from (
  5  select
  6    to_char(dte,'Month') mth,
  7    to_char(dte,'MM') mth_seq,
  8    fin_start.fs,
  9    sum(case when dte between add_months(fin_start.fs,-112) and fin_start.fs-1 then amount end) last_fin_year,
 10    sum(case when dte between fin_start.fs and add_months(fin_start.fs,12)-1 then amount end) this_fin_year
 11  from t, fin_start
 12  group by
 13    to_char(dte,'Month'),
 14    to_char(dte,'MM'),
 15    fin_start.fs
 16  )
 17  order by
 18    case when mth_seq >= to_char(fs,'MM') then 1 else 2 end,
 19    mth_seq;

MTH                                  LAST_FIN_YEAR THIS_FIN_YEAR
------------------------------------ ------------- -------------
June                                         20658         22188
July                                         13267          8844
August                                       15907         20457
September                                    15140         21906
October                                      21850         15100
November                                     18153         21052
December                                     14111         14677
January                                      44159         23112
February                                     43819         14267
March                                        50641          5047
April                                        37397
May                                          38748

12 rows selected.

SQL>
SQL>

0
投票

您没有提供有关“a”表中的列的任何线索。所以,在我的回答中,我假设你有一些YOURTABLE表,你有一些交易(订单)行。在这些行中,您有YOURTABLE.YOURDATE日期作为订单日期,YOURTABLE.YOURAMOUNT作为订单金额。

您需要按月对订单总额进行分组:

group by trunc(t.YOURDATE, 'mm')

但是,如果您想要将这些组表示为2列(如示例中所示),则应从YOURTABLe中选择2个(过去年份和当前年份)。然后加入他们。这是一个例子:

with curryear as
 (select to_char(trunc(t.YOURDATE, 'mm'), 'month') dmonth,
         sum(t.YOURAMOUNT) dresults
    from YOURTABLE t
   group by trunc(t.YOURDATE, 'mm')
  having trunc(t.YOURDATE, 'mm') >= '01-JUL-2017'
   order by trunc(t.YOURDATE, 'mm') asc),
pastyear as
 (select to_char(trunc(t.YOURDATE, 'mm'), 'month') dmonth,
         sum(t.YOURAMOUNT) dresults
    from YOURTABLE t
   group by trunc(t.YOURDATE, 'mm')
  having trunc(t.YOURDATE, 'mm') between '01-JUL-2016' and '01-JUN-2017'
   order by trunc(t.YOURDATE, 'mm') asc)
select curryear.dmonth   "Month",
       pastyear.dresults    "2016-17",
       curryear.dresults "2017-18"
  from curryear, pastyear
 where curryear.dmonth = pastyear.dmonth;

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.