我有来自MySql语句的标准数据
SELECT tipe.*
FROM
( SELECT 'Menunggu Approve Atasan' AS tipe UNION
SELECT 'Sudah Approved Atasan' UNION
SELECT 'Dalam Proses Pengerjaan IT' UNION
SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
SELECT 'Ditutup'
) AS tipe;
+---------------------------------------+
| tipe |
+---------------------------------------+
| Menunggu Approve Atasan |
| Sudah Approved Atasan |
| Dalam Proses Pengerjaan IT |
| Sudah Selesai Tapi Belum Ditutup User |
| Ditutup |
+---------------------------------------+
5 rows in set (0.02 sec)
然后我有这样的数据
SELECT requests.*
FROM
( SELECT DATE(request.tanggal_permintaan) as tanggal_permintaan
, request.`status`
, COUNT(*) as total
FROM request
WHERE YEAR(request.tanggal_permintaan) = '2018'
GROUP
BY DATE(request.tanggal_permintaan)
, request.status
) AS requests;
+--------------------+---------------------------------------+-------+
| tanggal_permintaan | status | total |
+--------------------+---------------------------------------+-------+
| 2018-01-02 | Menunggu Approve Atasan | 1 |
| 2018-01-02 | Ditutup | 4 |
| 2018-01-03 | Ditutup | 1 |
+--------------------+---------------------------------------+-------+
3 rows in set (0.02 sec)
您可以看到第二个查询的表单结果,它为我提供了基于日期的数据。但是,我需要在第二个查询中包括所有标准数据。 我需要这样的结果:
+--------------------+---------------------------------------+-------+
| tanggal_permintaan | status | total |
+--------------------+---------------------------------------+-------+
| 2018-01-02 | Menunggu Approve Atasan | 1 |
| 2018-01-02 | Sudah Approved Atasan | 0 |
| 2018-01-02 | Dalam Proses Pengerjaan IT | 0 |
| 2018-01-02 | Sudah Selesai Tapi Belum Ditutup User | 0 |
| 2018-01-02 | Ditutup | 4 | // one day
| 2018-01-03 | Menunggu Approve Atasan | 0 |
| 2018-01-03 | Sudah Approved Atasan | 0 |
| 2018-01-03 | Dalam Proses Pengerjaan IT | 0 |
| 2018-01-03 | Sudah Selesai Tapi Belum Ditutup User | 0 |
| 2018-01-03 | Ditutup | 1 | // one day
+--------------------+---------------------------------------+-------+
我用来获得这样的查询是什么? 到目前为止,我只是尝试了左外部联接,但是没有运气
SELECT requests.* FROM (
SELECT
DATE(request.tanggal_permintaan) as tanggal_permintaan,
request.`status`,
COUNT(*) as total
FROM request
WHERE YEAR(request.tanggal_permintaan) = '2018'
GROUP BY DATE(request.tanggal_permintaan), request.status
) requests
LEFT OUTER JOIN (
SELECT tipe.* FROM(
SELECT 'Menunggu Approve Atasan' AS tipe UNION
SELECT 'Sudah Approved Atasan' UNION
SELECT 'Dalam Proses Pengerjaan IT' UNION
SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
SELECT 'Ditutup'
) tipe
)standard
ON standard.tipe = requests.status
ORDER BY requests.tanggal_permintaan
答案 0 :(得分:2)
要获得该输出,您需要使用CROSS JOIN
表中每个不同的日期来创建tipe
值的request
:
SELECT r.date, t.tipe
FROM (SELECT 'Menunggu Approve Atasan' AS tipe UNION
SELECT 'Sudah Approved Atasan' UNION
SELECT 'Dalam Proses Pengerjaan IT' UNION
SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
SELECT 'Ditutup'
) t
CROSS JOIN
(SELECT DISTINCT DATE(tanggal_permintaan) AS date
FROM request
WHERE YEAR(tanggal_permintaan) = 2018) r
这将为您提供一个如下表:
| date | tipe |
+--------------------+---------------------------------------+
| 2018-01-02 | Menunggu Approve Atasan |
| 2018-01-02 | Sudah Approved Atasan |
| 2018-01-02 | Dalam Proses Pengerjaan IT |
| 2018-01-02 | Sudah Selesai Tapi Belum Ditutup User |
| 2018-01-02 | Ditutup |
| 2018-01-03 | Menunggu Approve Atasan |
| 2018-01-03 | Sudah Approved Atasan |
| 2018-01-03 | Dalam Proses Pengerjaan IT |
| 2018-01-03 | Sudah Selesai Tapi Belum Ditutup User |
| 2018-01-03 | Ditutup |
然后您可以LEFT JOIN
将其requests
表中获取所需的结果:
SELECT s.date, s.tipe, COUNT(*) AS total
FROM (SELECT r.date, t.tipe
FROM (SELECT 'Menunggu Approve Atasan' AS tipe UNION
SELECT 'Sudah Approved Atasan' UNION
SELECT 'Dalam Proses Pengerjaan IT' UNION
SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
SELECT 'Ditutup'
) t
CROSS JOIN
(SELECT DISTINCT DATE(tanggal_permintaan) AS date
FROM request
WHERE YEAR(tanggal_permintaan) = 2018) r
) s
LEFT JOIN request rq ON rq.status = s.tipe
GROUP BY s.date, s.tipe
答案 1 :(得分:1)
Year()
函数不允许它使用索引。我已将其更改为使用Between..
,以便它可以使用索引。Cross Join
以获得所有可能的组合。all_combinations
和left join
用于主要查询,启用 status
和date
,以获取所需的结果集。尝试以下操作:
SELECT all_combinations.tanggal_permintaan,
all_combinations.tipe,
COALESCE(requests.total, 0) AS total
FROM
(
SELECT all_tanggal_permintaan.tanggal_permintaan,
all_tipe.tipe
FROM
(SELECT DISTINCT DATE(tanggal_permintaan) as tanggal_permintaan
FROM request
WHERE tanggal_permintaan BETWEEN '2018-01-01 00:00:00' AND
'2018-12-31 23:59:59'
) AS all_tanggal_permintaan
CROSS JOIN
(SELECT 'Menunggu Approve Atasan' AS tipe UNION ALL
SELECT 'Sudah Approved Atasan' UNION ALL
SELECT 'Dalam Proses Pengerjaan IT' UNION ALL
SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION ALL
SELECT 'Ditutup'
) AS all_tipe
) AS all_combinations
LEFT OUTER JOIN
(
SELECT
DATE(tanggal_permintaan) as tanggal_permintaan,
status,
COUNT(*) AS total
FROM request
WHERE tanggal_permintaan BETWEEN '2018-01-01 00:00:00' AND
'2018-12-31 23:59:59'
GROUP BY DATE(tanggal_permintaan), status
) AS requests
ON requests.tanggal_permintaan = all_combinations.tanggal_permintaan AND
requests.status = all_combinations.tipe
ORDER BY all_combinations.tanggal_permintaan
答案 2 :(得分:1)
这是一个猜测,但似乎您需要一个状态值列表,这让我感到惊讶,因为它尚未在表中,但这可能有用:
SELECT
g.tanggal_permintaan, g.total, s.status
FROM (
SELECT DISTINCT
request.status
FROM request
) s
LEFT JOIN (
SELECT
DATE(request.tanggal_permintaan) AS tanggal_permintaan
,request.status
,COUNT(*) AS total
FROM request
WHERE request.tanggal_permintaan) >= '20180101'
AND request.tanggal_permintaan) < '20190101'
GROUP BY
DATE(request.tanggal_permintaan)
,request.status
) g
ON s.status = g.status
答案 3 :(得分:0)
感谢兄弟@Nick和@Used_By_Already
这是我的最终查询。 我创建了一个基于id的唯一concat,以通过左联接将它们连接起来。
SELECT standard.*, transactions.* FROM (
SELECT CONCAT(r.date, '#', t.tipe) as 'id', r.date AS tanggal_permintaan, t.tipe
FROM (
SELECT DISTINCT
request.status AS tipe
FROM request
) t
CROSS JOIN (
SELECT DISTINCT DATE(tanggal_permintaan) AS date
FROM request
WHERE YEAR(request.tanggal_permintaan) = '2018'
) r
) AS standard
LEFT JOIN (
SELECT CONCAT(requests.tanggal_permintaan, '#' , requests.status) AS 'id', requests.tanggal_permintaan, requests.status, requests.total
FROM (
SELECT
DATE(request.tanggal_permintaan) as tanggal_permintaan,
request.`status`,
COUNT(*) as total
FROM request
WHERE YEAR(request.tanggal_permintaan) = '2018'
GROUP BY DATE(request.tanggal_permintaan), request.status
) requests
) AS transactions
ON transactions.id = standard.id
ORDER BY standard.id