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

时间:2010-04-07 10:17:52

标签: mysql financial

我有一个数据库,让我们假设有两列(service_date& invoice_amount)。我想创建一个SQL查询来检索和分组每个财政年度(7月到6月)的数据。

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

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

欢迎所有想法。

由于

3 个答案:

答案 0 :(得分:19)

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

答案 1 :(得分:1)

这适用于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)

答案 2 :(得分:0)

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() => runApp(MyApp());

class X with ChangeNotifier {
  int _count = 0;
  int get count => _count;

  void increment() {
    _count++;
    notifyListeners();
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => X()),
      ],
      child: Consumer<X>(
        builder: (context, counter, _) {
          return MaterialApp(
            home: LoginScreen(),
          );
        },
      ),
    );
  }
}


class LoginScreen extends StatelessWidget {
  const LoginScreen({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final user_Provider = Provider.of<X>(context);
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '${user_Provider.count}',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: IncrementCounterButton(),
    );
  }
}


class IncrementCounterButton extends StatelessWidget {
  const IncrementCounterButton({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return FloatingActionButton(
      onPressed: () {
        Provider.of<X>(context, listen: false).increment();
      },
      tooltip: 'Increment',
      child: const Icon(Icons.add),
    );
  }
}