用于检索按年份分组的财政年度数据的 SQL 查询

发布于 2024-08-28 03:33:08 字数 216 浏览 9 评论 0原文

我有一个数据库,假设有两列(服务日期和发票金额)。我想创建一个 SQL 查询来检索每个财政年度(七月到六月)的数据并对其进行分组。

我有两年的数据,所以这是两个财政年度(即 2 个结果)。

我知道我可以手动执行此操作,方法是创建一个按月分组的 SQL 查询,然后通过 PHP 运行数据以创建财务年度数据,但我宁愿使用 SQL 查询。

欢迎所有想法。

谢谢

I have a database with lets assume two columns (service_date & invoice_amount). I would like to create an SQL query that would retrieve and group the data for each financial year (July to June).

I have two years of data so that is two financial years (i.e. 2 results).

I know I can do this manually by creating an SQL query to group by month then run the data through PHP to create the financial year data but I'd rather have an SQL query.

All ideas welcome.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难如初 2024-09-04 03:33:08
SELECT
   CASE WHEN MONTH(service_date)>=7 THEN
          concat(YEAR(service_date), '-',YEAR(service_date)+1)
   ELSE concat(YEAR(service_date)-1,'-', YEAR(service_date)) END AS financial_year,
   SUM(invoice_amount)
FROM mytable
GROUP BY financial_year

产生像下面这样的o/p

financial_year   invoice_amount
2007-2008        10000.00
2008-2009        15000.00
2009-2010        20000.00
SELECT
   CASE WHEN MONTH(service_date)>=7 THEN
          concat(YEAR(service_date), '-',YEAR(service_date)+1)
   ELSE concat(YEAR(service_date)-1,'-', YEAR(service_date)) END AS financial_year,
   SUM(invoice_amount)
FROM mytable
GROUP BY financial_year

which produce o/p like below

financial_year   invoice_amount
2007-2008        10000.00
2008-2009        15000.00
2009-2010        20000.00
别忘他 2024-09-04 03:33:08

这对我在 MSSQL 中有用。希望它对你在 MySQL 中有用

SELECT
   CASE WHEN MONTH(service_date)>=7 THEN
           YEAR(service_date)+1 
   ELSE YEAR(service_date) END AS financial_year,
   SUM(invoice_amount)
FROM mytable
GROUP BY
   CASE WHEN MONTH(service_date)>=7 THEN
           YEAR(service_date)+1 
   ELSE YEAR(service_date)

This works for me in MSSQL.. Hope it works for you in MySQL

SELECT
   CASE WHEN MONTH(service_date)>=7 THEN
           YEAR(service_date)+1 
   ELSE YEAR(service_date) END AS financial_year,
   SUM(invoice_amount)
FROM mytable
GROUP BY
   CASE WHEN MONTH(service_date)>=7 THEN
           YEAR(service_date)+1 
   ELSE YEAR(service_date)
人生百味 2024-09-04 03:33:08
to get data from April to March  ---

SELECT
CASE 
    WHEN MONTH(application_receiving_date)>=4 
    THEN concat(YEAR(application_receiving_date), '-',YEAR(application_receiving_date)+1) 
    ELSE concat(YEAR(application_receiving_date)-1,'-', YEAR(application_receiving_date)) 
END AS app_year,
from table_name 
GROUP BY app_year  
to get data from April to March  ---

SELECT
CASE 
    WHEN MONTH(application_receiving_date)>=4 
    THEN concat(YEAR(application_receiving_date), '-',YEAR(application_receiving_date)+1) 
    ELSE concat(YEAR(application_receiving_date)-1,'-', YEAR(application_receiving_date)) 
END AS app_year,
from table_name 
GROUP BY app_year  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文