作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为fund
的表。它包含一个 Id
、source
、received_date
和 value
。请参阅下面的 DDL、示例查询和相同的 fiddle :
CREATE TABLE `fund` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`source` varchar(100) NOT NULL,
`value` decimal(8,2) NOT NULL,
`received_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` varchar(5) NOT NULL,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`modified_by` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `fund` (`source`, `value`, `received_date`, `created_at`, `created_by`, `updated_at`, `modified_by`)
VALUES
('rsp - hibah', 5000.00, '2019-01-03', CURRENT_TIMESTAMP, 'admin', CURRENT_TIMESTAMP, 'admin'),
('rsp - takaful', 7500.00, '2019-01-03', CURRENT_TIMESTAMP, 'admin', CURRENT_TIMESTAMP, 'admin'),
('rsp - takaful', 100000.00, '2019-01-30', CURRENT_TIMESTAMP, 'admin', CURRENT_TIMESTAMP, 'admin'),
('rsp - hibah', 1200.00, '2019-05-07', CURRENT_TIMESTAMP, 'admin', CURRENT_TIMESTAMP, 'admin'),
('rsp - hibah', 5022.00, '2019-05-23', CURRENT_TIMESTAMP, 'admin', CURRENT_TIMESTAMP, 'admin'),
('rsp - takaful', 2000.00, '2019-11-21', CURRENT_TIMESTAMP, 'admin', CURRENT_TIMESTAMP, 'admin')
SELECT
CONCAT('Additional Amount ') AS NAME,
date_format( received_date, '%Y-%m-%d') AS m_date,
( CASE WHEN source = 'rsp - hibah' THEN SUM(value) ELSE NULL END ) AS total_hibah,
( CASE WHEN source = 'rsp - takaful' THEN SUM(value) ELSE NULL END ) AS total_takaful,
SUM(value) AS total_contribution
FROM
fund
WHERE
source LIKE 'rsp -%'
GROUP BY
YEAR(m_date),
MONTH(m_date),
source
UNION ALL
SELECT
CONCAT( 'Balance ', source, ' ' ) AS NAME,
-- ADDTIME( LAST_DAY( date_format( received_date, '%Y-%m-%d' )), 2 ) AS m_date,
LAST_DAY( date_format( received_date, '%Y-%m-%d' )) AS m_date,
( CASE WHEN source = 'rsp - hibah' THEN @sum_hibah := @sum_hibah + SUM( VALUE ) ELSE NULL END ) AS total_hibah,
( CASE WHEN source = 'rsp - takaful' THEN @sum_takaful := @sum_takaful + SUM( VALUE ) ELSE NULL END ) AS total_takaful,
NULL AS total_contribution
FROM
fund,
(SELECT @sum_takaful := 0) AS col_takaful,
(SELECT @sum_hibah := 0) AS col_hibah
WHERE
source LIKE 'rsp -%'
GROUP BY
YEAR (m_date),
MONTH (m_date),
source
WITH ROLLUP
UNION ALL
SELECT
CONCAT('Total') AS NAME,
LAST_DAY( date_format( received_date, '%Y-%m-%d' )) AS m_date,
NULL AS total_hibah,
NULL AS total_takaful,
(@sum_contribution := SUM(value) + @sum_contribution) AS total_contribution
FROM
fund,
(SELECT @sum_contribution:=0) AS col_contribution
WHERE
source LIKE 'rsp -%'
GROUP BY
YEAR(received_date),
MONTH(received_date)
WITH ROLLUP
ORDER BY
m_date, total_hibah, total_takaful, total_contribution
http://sqlfiddle.com/#!9/1fb70e/23/0
为什么表中有一个没有值的空行?
有没有办法防止这个或我的查询不正确。请指教
结果
+-----------------------+------------+-------------+---------------+--------------------+
| ME | m_date | total_hibah | total_takaful | total_contribution |
+-----------------------+------------+-------------+---------------+--------------------+
| Additional Amount | 2019-01-03 | (null) | 107500 | 107500 |
| Additional Amount | 2019-01-03 | 5000 | (null) | 5000 |
| (null) | 2019-01-31 | (null) | (null) | (null) | //empty row
| Total | 2019-01-31 | (null) | (null) | 112500 |
| Balance rsp - takaful | 2019-01-31 | (null) | 107500 | (null) |
| Balance rsp - hibah | 2019-01-31 | 5000 | (null) | (null) |
| Additional Amount | 2019-05-07 | 6222 | (null) | 6222 |
| (null) | 2019-05-31 | (null) | (null) | (null) | //empty row
| Total | 2019-05-31 | (null) | (null) | 118722 |
| Balance rsp - hibah | 2019-05-31 | 11222 | (null) | (null) |
| Additional Amount | 2019-11-21 | (null) | 2000 | 2000 |
| (null) | 2019-11-30 | (null) | (null) | (null) | //empty row
| (null) | 2019-11-30 | (null) | (null) | (null) | //empty row
| (null) | 2019-11-30 | (null) | (null) | (null) | //empty row
| Total | 2019-11-30 | (null) | (null) | 120722 |
| Total | 2019-11-30 | (null) | (null) | 239444 |
| Total | 2019-11-30 | (null) | (null) | 239444 |
| Balance rsp - takaful | 2019-11-30 | (null) | 109500 | (null) |
+-----------------------+------------+-------------+---------------+--------------------+
最佳答案
我想评论有点长。但是在第二个查询中使用 ROLLUP 导致了这个问题。您只需要从那里删除 ROLLUP 子句 -
GROUP BY
YEAR (m_date),
MONTH (m_date),
source
WITH ROLLUP -- Here is the issue.
Here是更新后的 fiddle 。
关于MySQL - 通过 Group By 和 Rollup 接收到空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58911568/
我是一名优秀的程序员,十分优秀!