我正在尝试获取当前财政年度和上一个财政年度的月度总金额。我不能硬编码日期。它应该基于当前日期。日期列的格式如下: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 ;
答案 0 :(得分: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
定义了鳍年的开始(因为它不是每个人的六月: - ))
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>
答案 1 :(得分: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;
希望这有帮助!