我需要一个显示当天记录的解决方案并显示每列的SUM,到目前为止我有一个查询来显示联合记录,如:
SELECT
datetime,
remarks,
void,
invoice,
reload,
redeem
FROM
(
SELECT datetime, remarks, '' void, '' invoice, points_intake reload, '' redeem, 1 ord
FROM bpp_intake_logs
WHERE outlet = 'KUL' AND DATE(datetime) = '2017-06-13'
UNION ALL
SELECT datetime, remarks, void, invoice, '', points_consume, 2
FROM bpp_consume_logs
WHERE outlet = 'KUL' AND DATE(datetime) = '2017-06-13'
) q
ORDER BY datetime DESC, ord
,结果表显示如下:
datetime | remarks | void | invoice | reload | redeem |
2017-06-13 15:53:31 | Point restored - void XY203460 | | | 10 | |
2017-06-13 15:37:27 | reload / top-up credit | | | 10 | |
2017-06-13 15:35:56 | redeem from VIP card | 1 | XY203460 | | 10 |
2017-06-13 15:16:03 | redeem from VIP card | | XY203456 | | 5 |
现在,我想添加一个单元格,对其所属列底部的每个reload
和redeem
求和,如:
datetime | remarks | void | invoice | reload | redeem |
2017-06-13 15:53:31 | Point restored - void XY203460 | | | 10 | |
2017-06-13 15:37:27 | reload / top-up credit | | | 10 | |
2017-06-13 15:35:56 | redeem from VIP card | 1 | XY203460 | | 10 |
2017-06-13 15:16:03 | redeem from VIP card | | XY203456 | | 5 |
| 20 | 15 |
如图所示,SUM 20 和 15 分配在底部单元格中。
这可能以这种方式获得结果吗?
答案 0 :(得分:0)
也许这就是你要找的????
SELECT
datetime,
remarks,
void,
invoice,
SUM(reload),
SUM(redeem)
FROM
(
SELECT datetime, remarks, '' void, '' invoice, points_intake reload, '' redeem, 1 ord
FROM bpp_intake_logs
WHERE outlet = 'KUL' AND DATE(datetime) = '2017-06-13'
UNION ALL
SELECT datetime, remarks, void, invoice, '', points_consume, 2
FROM bpp_consume_logs
WHERE outlet = 'KUL' AND DATE(datetime) = '2017-06-13'
) q
GROUP BY datetime desc, remarks asc, void asc, invoice asc with rollup
ORDER BY datetime DESC, ord