我使用以下代码创建表:
CREATE TABLE PaL (
country TEXT,
Day DATE,
Revenue CHAR(255),
Costs CHAR(255)
);
INSERT INTO PaL
(country, Day, Revenue, Costs)
VALUES
("DE", "2017-01-31", "5600", "-3000"),
("DE", "2017-02-28", "3900", "-1550"),
("DE", "2017-03-31", "4100", "-2200"),
("DE", "2017-04-30", "7200", "-5000"),
("DE", "2017-05-31", "5300", "-4000"),
("DE", "2017-06-30", "8800", "-6550"),
("DE", "2017-07-31", "1120", "-500"),
("DE", "2017-08-31", "5000", "-3250"),
("DE", "2017-09-30", "2600", "-1225"),
("DE", "2017-10-31", "9200", "-8350"),
("DE", "2017-11-30", "5100", "-2995"),
("DE", "2017-12-31", "3950", "-3400"),
("DE", "2018-01-31", "7840", "-6985"),
("DE", "2018-02-28", "9358", "-1452"),
("DE", "2018-03-31", "2225", "-1655"),
("DE", "2018-04-30", "3820", "-2950"),
("DE", "2018-05-31", "9587", "-8500"),
("DE", "2018-06-30", "3000", "-2785"),
("DE", "2018-07-31", "5450", "-4500"),
("DE", "2018-08-31", "3800", "-2650"),
("DE", "2018-09-30", "9000", "-7456"),
("DE", "2018-10-31", "7780", "-6900"),
("DE", "2018-11-30", "6585", "-5450"),
("DE", "2018-12-31", "1280", "-850"),
("DE", "2019-01-31", "6585", "-5450"),
("DE", "2019-02-28", "1280", "-850");
然后,我使用以下代码从中查询收入:
Select country, sum(Revenue)
From Pal
Where Day Between '2017-03-01' AND '2017-02-28'
Group By country
您还可以在SQL小提琴here中找到所有内容
到目前为止,所有这些工作正常。
但是,我的目标是最终SQL结果如下所示:
Fiscal Year Country Values
2017 DE Sum from 2017-03-01 - 2018-02-28
2018 DE Sum from 2018-03-01 - 2019-02-28
为了获得所需的结果,我必须对代码进行哪些更改?
答案 0 :(得分:0)
您可以通过减去2个月并提取年份来计算会计年度。所以:
Select year(day - interval 2 month) as fy,
country, sum(Revenue)
From Pal
Group By fy, country