gpt4 book ai didi

MySQL - 按财政年度准确显示数据

转载 作者:行者123 更新时间:2023-11-29 11:17:26 25 4
gpt4 key购买 nike

寻求帮助。这是我的架构

CREATE TABLE `net_savings` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_id` int(11) NOT NULL,
`monthname` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
`fiscal_year` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
`amount` decimal(30,2) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `net_savings_id_unique` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

我的数据:

    INSERT INTO `net_savings` VALUES  
(10,1,'January','2016',-1291.47,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(11,1,'February','2017',389296.02,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(12,1,'March','2018',216143.70,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(13,2,'April','2019',53840.00,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(14,2,'May','2017',1669693.00,'0000-00-00 00:00:00','2016-08-30 04:00:00'),
(15,2,'June','2018',1980328.00,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(16,2,'July','2019',2428559.00,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(17,3,'August','2016',963.32,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(18,3,'September','2017',3960.93,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(19,4,'October','2016',-261.74,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(20,4,'November','2017',24160.80,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(21,4,'December','2018',24160.80,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(22,4,'January','2019',8053.60,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(23,5,'February','2016',8846.54,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(24,5,'March','2017',41141.34,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(25,5,'April','2018',41141.34,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(26,5,'May','2019',41141.34,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(27,5,'June','2020',27427.56,'2016-08-30 04:00:00','2016-08-30 04:00:00'),
(31,1,'March','2016',100.00,'2016-09-14 11:33:48','2016-09-14 11:33:48');

这是我的存储过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `financial_forecast_yearly`()
BEGIN
SET group_concat_max_len=2048;
SET @sql = NULL;

SELECT GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(n.fiscal_year = ''',
n.fiscal_year, ''', n.amount, NULL))AS ',
CONCAT("'","FY_",n.fiscal_year,"'")))
INTO @sql
FROM net_savings n join accounts a
ON n.account_id = a.id;

SET @sql = CONCAT('SELECT a.id,
a.account,
a.region,
SUM(n.amount) as total_net_savings,'
,@sql,
'FROM net_savings n join accounts a
on n.account_id = a.id
GROUP BY a.account, n.account_id
ORDER BY a.id');

PREPARE statement FROM @sql;
EXECUTE statement;
DEALLOCATE PREPARE statement;
END

输出如下所示:

enter image description here

但是,显示的数据并不准确。例如,我在 2016 年 3 月的某个地方输入了相同的金额,总数并未汇总到 FY2016 列中。

最佳答案

这个存储过程很复杂,很难弄清楚!

您正在尝试做两件事:

  1. 您正在尝试按会计年度进行汇总。
  2. 您正在尝试调整结果。

最好分两步处理这些问题。

第一:(http://sqlfiddle.com/#!9/26179/3/0)

SELECT SUM(amount) amount,
account_id, fiscal_year
FROM net_savings
GROUP BY account_id, fiscal_year WITH ROLLUP

其次,旋转是 MySQL 中的一大难题。

你可能需要这样的东西:

SELECT account_id,
SUM(CASE WHEN fiscal_year IS NULL THEN amount ELSE 0 END) total,
SUM(CASE WHEN fiscal_year = 2016 THEN amount ELSE 0 END) fy2016,
SUM(CASE WHEN fiscal_year = 2017 THEN amount ELSE 0 END) fy2017,
SUM(CASE WHEN fiscal_year = 2018 THEN amount ELSE 0 END) fy2018,
SUM(CASE WHEN fiscal_year = 2019 THEN amount ELSE 0 END) fy2019,
SUM(CASE WHEN fiscal_year = 2020 THEN amount ELSE 0 END) fy2020
FROM (
SELECT SUM(amount) amount,
account_id, fiscal_year
FROM net_savings
GROUP BY account_id, fiscal_year WITH ROLLUP
) summary
WHERE account_id IS NOT NULL
GROUP BY account_id

一旦您知道这是您的目标语句,您就可以编写存储过程来创建它并准备它。

关于MySQL - 按财政年度准确显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39494415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com