【求助】MySQL8.0根据银行流水日记账统计日报表的数据库设计方案。

如题,该需求为财务系统常见的日报表、余额表等报表查询统计,如何设计性能良好的查询方案(因获取不到财务软件的数据库,未能参考)。
方案需求说明:因流水数据较大,需考虑查询的性能。可新增其他表、辅助字段、触发器、索引优化等,MySQL版本为8.0新版,可使用窗口函数等特性
具体情况:
1、数据表结构(表字段使用中文只是为了粗暴地阅读,请忽略。。。):

流水表flows:

图片说明

2、需求的查询:2019-8-11至2019-8-12日的资金日报表,期初余额为截至10日当天的余额,期末余额为12日当天的余额
图片说明

流水表SQL

-- ----------------------------
-- Table structure for flows
-- ----------------------------
DROP TABLE IF EXISTS `flows`;
CREATE TABLE `flows` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `单位` varchar(255) DEFAULT NULL,
  `银行账户` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  `日期` date DEFAULT NULL,
  `收入` decimal(18,2) DEFAULT NULL,
  `支出` decimal(18,2) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- ----------------------------
-- Records of flows
-- ----------------------------
INSERT INTO `flows` VALUES ('1', 'A', 'A01', '2019-07-15', '300.00', '0.00');
INSERT INTO `flows` VALUES ('2', 'A', 'A01', '2019-07-30', '0.00', '120.00');
INSERT INTO `flows` VALUES ('3', 'A', 'A01', '2019-08-01', '0.00', '70.00');
INSERT INTO `flows` VALUES ('4', 'A', 'A01', '2019-08-03', '99.00', '0.00');
INSERT INTO `flows` VALUES ('5', 'A', 'A01', '2019-08-11', '0.00', '77.00');
INSERT INTO `flows` VALUES ('6', 'A', 'A01', '2019-08-22', '0.00', '55.00');
INSERT INTO `flows` VALUES ('7', 'A', 'A01', '2019-08-25', '88.00', '0.00');
INSERT INTO `flows` VALUES ('8', 'A', 'A01', '2019-08-26', '0.00', '90.00');
INSERT INTO `flows` VALUES ('9', 'A', 'A02', '2019-07-02', '60.00', '0.00');
INSERT INTO `flows` VALUES ('10', 'A', 'A02', '2019-07-30', '0.00', '35.00');
INSERT INTO `flows` VALUES ('11', 'B', 'B01', '2019-08-10', '365.00', '0.00');
INSERT INTO `flows` VALUES ('12', 'B', 'B01', '2019-08-12', '98.00', '0.00');

https://ask.csdn.net/questions/763940

存在自己采纳自己的行为1次,所以先采纳才能回答你。

费了一个小时设计,请参考
1.先用 distinct 排除多余数据
2.利用子查询求出每一栏的数字
3.期末=期初+收入-支出

select distinct 
   单位
  ,银行账户
  ,(select sum(flows2.收入)- sum(flows2.支出) from flows flows2 where flows2.日期 <= '2019-08-10' and flows2.单位= flows.单位 and flows2.银行账户 = flows.银行账户) 期初余额
  ,ifnull((select sum(flows2.收入) from flows flows2 where flows2.日期 >= '2019-08-11' and flows2.日期 <= '2019-08-12' and flows2.单位= flows.单位 and flows2.银行账户 = flows.银行账户),0) 收入累计
  ,ifnull((select sum(flows2.支出) from flows flows2 where flows2.日期 >= '2019-08-11' and flows2.日期 <= '2019-08-12' and flows2.单位= flows.单位 and flows2.银行账户 = flows.银行账户),0) 支出累计
  --期末=期初+收入-支出
  ,ifnull((select sum(flows2.收入)- sum(flows2.支出) from flows flows2 where flows2.日期 <= '2019-08-10' and flows2.单位= flows.单位 and flows2.银行账户 = flows.银行账户),0) +
   ifnull((select sum(flows2.收入) from flows flows2 where flows2.日期 >= '2019-08-11' and flows2.日期 <= '2019-08-12' and flows2.单位= flows.单位 and flows2.银行账户 = flows.银行账户),0) - 
   ifnull((select sum(flows2.支出) from flows flows2 where flows2.日期 >= '2019-08-11' and flows2.日期 <= '2019-08-12' and flows2.单位= flows.单位 and flows2.银行账户 = flows.银行账户),0) 期末余额

  from flows