英文:
Make one query from 2 complex queries
问题
我有关于将两个查询合并为一个查询的困惑,因为这些查询对我来说太复杂了。
这是我的第一个查询。
SELECT
COALESCE(s1.kode_barang, s2.kode_barang) AS kode_barang,
COALESCE(s1.nama, s2.nama) AS nama,
COALESCE(s1.sisa, 0) AS sisa_JUNI,
COALESCE(s2.total_jumlah, 0) AS masuk_JULI,
COALESCE(s1.sisa, 0) + COALESCE(s2.total_jumlah, 0) AS total
FROM
(
SELECT
s2.max_tanggal,
s2.kode_barang,
s1.nama,
s2.sisa
FROM
ws_fh.simas_barang s1
LEFT JOIN (
SELECT
sm1.kode_barang,
sm1.tanggal AS max_tanggal,
sm1.created_at,
sm1.sisa
FROM
ws_fh.simas_mutasi sm1
JOIN (
SELECT
kode_barang,
MAX(tanggal) AS max_tanggal,
created_at
FROM
ws_fh.simas_mutasi
WHERE
tanggal BETWEEN '2022-06-01' AND '2022-06-30'
GROUP BY
kode_barang
) sm2 ON sm2.kode_barang = sm1.kode_barang
AND sm2.max_tanggal = sm1.tanggal
GROUP BY
sm1.kode_barang,
sm1.created_at,
sm1.sisa
ORDER BY
sm1.kode_barang,
sm1.created_at DESC
) s2 ON s2.kode_barang = s1.id
WHERE
s1.jenis = 'ATK DAN SEJENISNYA'
GROUP BY
s2.kode_barang
) s1
LEFT JOIN (
SELECT
sm.tanggal,
sm.kode_barang,
sb.nama,
SUM(sm.jumlah) AS total_jumlah
FROM
ws_fh.simas_mutasi sm
JOIN ws_fh.simas_barang sb ON sb.id = sm.kode_barang
WHERE
sb.jenis = 'ATK DAN SEJENISNYA'
AND sm.kegiatan != 'mengeluarkan stok'
AND sm.kegiatan != 'mengubah barang'
AND sm.tanggal BETWEEN '2022-07-01' AND '2022-07-31'
GROUP BY
sb.nama
) s2 ON s1.nama = s2.nama
UNION
SELECT
COALESCE(s1.kode_barang, s2.kode_barang) AS kode_barang,
COALESCE(s1.nama, s2.nama) AS nama,
COALESCE(s1.sisa, 0) AS sisa,
COALESCE(s2.total_jumlah, 0) AS total_jumlah,
COALESCE(s1.sisa, 0) + COALESCE(s2.total_jumlah, 0) AS total
FROM
(
SELECT
s2.max_tanggal,
s2.kode_barang,
s1.nama,
s2.sisa
FROM
ws_fh.simas_barang s1
RIGHT JOIN (
SELECT
sm1.kode_barang,
sm1.tanggal AS max_tanggal,
sm1.created_at,
sm1.sisa
FROM
ws_fh.simas_mutasi sm1
JOIN (
SELECT
kode_barang,
MAX(tanggal) AS max_tanggal,
created_at
FROM
ws_fh.simas_mutasi
WHERE
tanggal BETWEEN '2022-06-01' AND '2022-06-30'
GROUP BY
kode_barang
) sm2 ON sm2.kode_barang = sm1.kode_barang
AND sm2.max_tanggal = sm1.tanggal
GROUP BY
sm1.kode_barang,
sm1.created_at,
sm1.sisa
ORDER BY
sm1.kode_barang,
sm1.created_at DESC
) s2 ON s2.kode_barang = s1.id
WHERE
s1.jenis = 'ATK DAN SEJENISNYA'
GROUP BY
s2.kode_barang
) s1
RIGHT JOIN (
SELECT
sm.tanggal,
sm.kode_barang,
sb.nama,
SUM(sm.jumlah) AS total_jumlah,
sm.kegiatan
FROM
ws_fh.simas_mutasi sm
JOIN ws_fh.simas_barang sb ON sb.id = sm.kode_barang
WHERE
sb.jenis = 'ATK DAN SEJENISNYA'
AND sm.kegiatan != 'mengeluarkan stok'
AND sm.kegiatan != 'mengubah barang'
AND sm.tanggal BETWEEN '2022-07-01' AND '2022-07-31'
GROUP BY
sb.nama
) s2 ON s1.nama = s2.nama
WHERE
s1.nama IS NULL
ORDER BY
nama;
这是我的第二个查询。
SELECT COALESCE(sm.kode_barang, s1.id) AS kode_barang,
COALESCE(sb.nama, s1.nama) AS nama,
SUM(sm.jumlah) AS keluar_JULI,
s2.sisa AS sisa_JULI
FROM ws_fh.simas_mutasi sm
LEFT JOIN ws_fh.simas_barang sb ON sb.id = sm.kode_barang
LEFT JOIN (
SELECT sm1.kode_barang, sm1.tanggal AS max_tanggal, sm1.created_at, sm1.sisa
FROM ws_fh.simas_mutasi sm1
JOIN (
SELECT kode_barang, MAX(tanggal) AS max_tanggal, created_at
FROM ws_fh.simas_mutasi
WHERE tanggal BETWEEN '2022-07-01' AND '2022-07-31'
GROUP BY kode_barang
) sm2 ON sm2.kode_barang = sm1.kode_barang AND sm2.max_tanggal = sm1.tanggal
GROUP BY sm1.kode_barang, sm1.created_at, sm1.sisa
ORDER BY sm1.kode_barang, sm1.created_at DESC
) s2 ON s2.kode_barang = sb.id
RIGHT JOIN ws_fh.simas_barang s1 ON s1.id = s2.kode_barang
WHERE COALESCE(sb.jenis, s1.jenis) = 'ATK DAN SEJENISNYA'
GROUP BY COALESCE(sb.nama, s1.nama)
HAVING keluar_JULI IS NOT NULL AND sisa_JULI IS NOT NULL
ORDER BY COALESCE(sb.nama, s1.nama);
我想将它们合并为一个查询。使用 "nama" 进行分组,以便第一个查询和第二个查询中相同的 "nama" 将出现在同一行。如果第一个查询中的 "nama" 与第二个查询中的 "nama" 不匹配,则将其显示在表
英文:
I have confusion about making 2 queries into 1 query because those queries are too complex for me.
This is my first query.
SELECT
COALESCE(s1.kode_barang, s2.kode_barang) AS kode_barang,
COALESCE(s1.nama, s2.nama) AS nama,
COALESCE(s1.sisa, 0) AS sisa_JUNI,
COALESCE(s2.total_jumlah, 0) AS masuk_JULI,
COALESCE(s1.sisa, 0) + COALESCE(s2.total_jumlah, 0) AS total
FROM
(
SELECT
s2.max_tanggal,
s2.kode_barang,
s1.nama,
s2.sisa
FROM
ws_fh.simas_barang s1
LEFT JOIN (
SELECT
sm1.kode_barang,
sm1.tanggal AS max_tanggal,
sm1.created_at,
sm1.sisa
FROM
ws_fh.simas_mutasi sm1
JOIN (
SELECT
kode_barang,
MAX(tanggal) AS max_tanggal,
created_at
FROM
ws_fh.simas_mutasi
WHERE
tanggal BETWEEN '2022-06-01' AND '2022-06-30'
GROUP BY
kode_barang
) sm2 ON sm2.kode_barang = sm1.kode_barang
AND sm2.max_tanggal = sm1.tanggal
GROUP BY
sm1.kode_barang,
sm1.created_at,
sm1.sisa
ORDER BY
sm1.kode_barang,
sm1.created_at DESC
) s2 ON s2.kode_barang = s1.id
WHERE
s1.jenis = 'ATK DAN SEJENISNYA'
GROUP BY
s2.kode_barang
) s1
LEFT JOIN (
SELECT
sm.tanggal,
sm.kode_barang,
sb.nama,
SUM(sm.jumlah) AS total_jumlah
FROM
ws_fh.simas_mutasi sm
JOIN ws_fh.simas_barang sb ON sb.id = sm.kode_barang
WHERE
sb.jenis = 'ATK DAN SEJENISNYA'
AND sm.kegiatan != 'mengeluarkan stok'
AND sm.kegiatan != 'mengubah barang'
AND sm.tanggal BETWEEN '2022-07-01' AND '2022-07-31'
GROUP BY
sb.nama
) s2 ON s1.nama = s2.nama
UNION
SELECT
COALESCE(s1.kode_barang, s2.kode_barang) AS kode_barang,
COALESCE(s1.nama, s2.nama) AS nama,
COALESCE(s1.sisa, 0) AS sisa,
COALESCE(s2.total_jumlah, 0) AS total_jumlah,
COALESCE(s1.sisa, 0) + COALESCE(s2.total_jumlah, 0) AS total
FROM
(
SELECT
s2.max_tanggal,
s2.kode_barang,
s1.nama,
s2.sisa
FROM
ws_fh.simas_barang s1
RIGHT JOIN (
SELECT
sm1.kode_barang,
sm1.tanggal AS max_tanggal,
sm1.created_at,
sm1.sisa
FROM
ws_fh.simas_mutasi sm1
JOIN (
SELECT
kode_barang,
MAX(tanggal) AS max_tanggal,
created_at
FROM
ws_fh.simas_mutasi
WHERE
tanggal BETWEEN '2022-06-01' AND '2022-06-30'
GROUP BY
kode_barang
) sm2 ON sm2.kode_barang = sm1.kode_barang
AND sm2.max_tanggal = sm1.tanggal
GROUP BY
sm1.kode_barang,
sm1.created_at,
sm1.sisa
ORDER BY
sm1.kode_barang,
sm1.created_at DESC
) s2 ON s2.kode_barang = s1.id
WHERE
s1.jenis = 'ATK DAN SEJENISNYA'
GROUP BY
s2.kode_barang
) s1
RIGHT JOIN (
SELECT
sm.tanggal,
sm.kode_barang,
sb.nama,
SUM(sm.jumlah) AS total_jumlah,
sm.kegiatan
FROM
ws_fh.simas_mutasi sm
JOIN ws_fh.simas_barang sb ON sb.id = sm.kode_barang
WHERE
sb.jenis = 'ATK DAN SEJENISNYA'
AND sm.kegiatan != 'mengeluarkan stok'
AND sm.kegiatan != 'mengubah barang'
AND sm.tanggal BETWEEN '2022-07-01' AND '2022-07-31'
GROUP BY
sb.nama
) s2 ON s1.nama = s2.nama
WHERE
s1.nama IS NULL
ORDER BY
nama;
This is my second query.
SELECT COALESCE(sm.kode_barang, s1.id) AS kode_barang,
COALESCE(sb.nama, s1.nama) AS nama,
SUM(sm.jumlah) AS keluar_JULI,
s2.sisa AS sisa_JULI
FROM ws_fh.simas_mutasi sm
LEFT JOIN ws_fh.simas_barang sb ON sb.id = sm.kode_barang
LEFT JOIN (
SELECT sm1.kode_barang, sm1.tanggal AS max_tanggal, sm1.created_at, sm1.sisa
FROM ws_fh.simas_mutasi sm1
JOIN (
SELECT kode_barang, MAX(tanggal) AS max_tanggal, created_at
FROM ws_fh.simas_mutasi
WHERE tanggal BETWEEN '2022-07-01' AND '2022-07-31'
GROUP BY kode_barang
) sm2 ON sm2.kode_barang = sm1.kode_barang AND sm2.max_tanggal = sm1.tanggal
GROUP BY sm1.kode_barang, sm1.created_at, sm1.sisa
ORDER BY sm1.kode_barang, sm1.created_at DESC
) s2 ON s2.kode_barang = sb.id
RIGHT JOIN ws_fh.simas_barang s1 ON s1.id = s2.kode_barang
WHERE COALESCE(sb.jenis, s1.jenis) = 'ATK DAN SEJENISNYA'
GROUP BY COALESCE(sb.nama, s1.nama)
HAVING keluar_JULI IS NOT NULL AND sisa_JULI IS NOT NULL
ORDER BY COALESCE(sb.nama, s1.nama);
I wanted to make it all into one query. Use group by for "nama" so the same "nama" from first query and second query would be in the same row. If "nama" from the first query is not matching with "nama" in the second query then display it in the table.
答案1
得分: 1
以下是翻译好的部分:
对于这个,你将需要使用FULL OUTER JOIN,MySQL不支持这个,但在MySQL 8中,它至少变得更加可读。
WITH CTE1 AS(选择
COALESCE(s1.kode_barang,s2.kode_barang)作为kode_barang,
COALESCE(s1.nama,s2.nama)作为nama,
COALESCE(s1.sisa,0)作为sisa_JUNI,
COALESCE(s2.total_jumlah,0)作为masuk_JULI,
COALESCE(s1.sisa,0)+ COALESCE(s2.total_jumlah,0)作为总和
从
(
选择
s2.max_tanggal,
s2.kode_barang,
s1.nama,
s2.sisa
从
ws_fh.simas_barang s1
LEFT JOIN(
选择
sm1.kode_barang,
sm1.tanggal AS max_tanggal,
sm1.created_at,
sm1.sisa
从
ws_fh.simas_mutasi sm1
加入(
选择
kode_barang,
MAX(tanggal)AS max_tanggal,
created_at
从
ws_fh.simas_mutasi
WHERE
tanggal BETWEEN '2022-06-01' AND '2022-06-30'
GROUP BY
kode_barang
)sm2 ON sm2.kode_barang = sm1.kode_barang
和sm2.max_tanggal = sm1.tanggal
GROUP BY
sm1.kode_barang,
sm1.created_at,
sm1.sisa
ORDER BY
sm1.kode_barang,
sm1.created_at DESC
)s2 ON s2.kode_barang = s1.id
WHERE
s1.jenis = 'ATK DAN SEJENISNYA'
GROUP BY
s2.kode_barang
)s1
左连接(
选择
sm.tanggal,
sm.kode_barang,
sb.nama,
SUM(sm.jumlah)作为总数
从
ws_fh.simas_mutasi sm
JOIN ws_fh.simas_barang sb ON sb.id = sm.kode_barang
WHERE
sb.jenis = 'ATK DAN SEJENISNYA'
和sm.kegiatan!= 'mengeluarkan stok'
和sm.kegiatan!= 'mengubah barang'
和sm.tanggal BETWEEN '2022-07-01' AND '2022-07-31'
GROUP BY
sb.nama
)s2 ON s1.nama = s2.nama
UNION
选择
COALESCE(s1.kode_barang,s2.kode_barang)作为kode_barang,
COALESCE(s1.nama,s2.nama)作为nama,
COALESCE(s1.sisa,0)作为sisa,
COALESCE(s2.total_jumlah,0)作为总数,
COALESCE(s1.sisa,0)+ COALESCE(s2.total_jumlah,0)作为总和
从
(
选择
s2.max_tanggal,
s2.kode_barang,
s1.nama,
s2.sisa
从
ws_fh.simas_barang s1
RIGHT JOIN(
选择
sm1.kode_barang,
sm1.tanggal AS max_tanggal,
sm1.created_at,
sm1.sisa
从
ws_fh.simas_mutasi sm1
JOIN(
选择
kode_barang,
MAX(tanggal)AS max_tanggal,
created_at
从
ws_fh.simas_mutasi
WHERE
tanggal BETWEEN '2022-06-01' AND '2022-06-30'
GROUP BY
kode_barang
)sm2 ON sm2.kode_barang = sm1.kode_barang
和sm2.max_tanggal = sm1.tanggal
GROUP BY
sm1.kode_barang,
sm1.created_at,
sm1.sisa
ORDER BY
sm1.kode_barang,
sm1.created_at DESC
)s2 ON s2.kode_barang = s1.id
WHERE
s1.jenis = 'ATK DAN SEJENISNYA'
GROUP BY
s2.kode_barang
)s1
右连接(
选择
sm.tanggal,
sm.kode_barang,
sb.nama,
SUM(sm.jumlah)作为总数,
sm.kegiatan
从
ws_fh.simas_mutasi sm
JOIN ws_fh.simas_barang sb ON sb.id = sm.kode_barang
WHERE
sb.jenis = 'ATK DAN SEJENISNYA'
和sm.kegiatan!= 'mengeluarkan stok'
和sm.kegiatan!= 'mengubah barang'
和sm.tanggal BETWEEN '2022-07-01' AND '2022-07-31'
GROUP BY
sb.nama
)s2 ON s1.nama = s2.nama
WHERE
s1.nama IS NULL
ORDER BY
nama),
CTE2 AS(
SELECT COALESCE(sm.kode_barang,s1.id)作为kode_barang,
COALESCE(sb.nama,s1.nama)作为nama,
SUM(sm.jumlah)作为keluar_JULI,
s2.sisa AS sisa_JULI
从ws_fh.simas_mutasi sm
LEFT JOIN ws_fh.simas_barang sb ON sb.id = sm.kode_barang
LEFT JOIN(
选择sm1.kode_barang,sm1.tanggal AS max_tanggal,sm1.created_at,sm1.sisa
从ws_fh.simas_mutasi sm1
<details>
<summary>英文:</summary>
For that you will need a FULL OUTER JOIN, which MySQL doesn't support, with MySQL 8 it gets at least somewhat more readable
WITH CTE1 AS (SELECT
COALESCE(s1.kode_barang, s2.kode_barang) AS kode_barang,
COALESCE(s1.nama, s2.nama) AS nama,
COALESCE(s1.sisa, 0) AS sisa_JUNI,
COALESCE(s2.total_jumlah, 0) AS masuk_JULI,
COALESCE(s1.sisa, 0) + COALESCE(s2.total_jumlah, 0) AS total
FROM
(
SELECT
s2.max_tanggal,
s2.kode_barang,
s1.nama,
s2.sisa
FROM
ws_fh.simas_barang s1
LEFT JOIN (
SELECT
sm1.kode_barang,
sm1.tanggal AS max_tanggal,
sm1.created_at,
sm1.sisa
FROM
ws_fh.simas_mutasi sm1
JOIN (
SELECT
kode_barang,
MAX(tanggal) AS max_tanggal,
created_at
FROM
ws_fh.simas_mutasi
WHERE
tanggal BETWEEN '2022-06-01' AND '2022-06-30'
GROUP BY
kode_barang
) sm2 ON sm2.kode_barang = sm1.kode_barang
AND sm2.max_tanggal = sm1.tanggal
GROUP BY
sm1.kode_barang,
sm1.created_at,
sm1.sisa
ORDER BY
sm1.kode_barang,
sm1.created_at DESC
) s2 ON s2.kode_barang = s1.id
WHERE
s1.jenis = 'ATK DAN SEJENISNYA'
GROUP BY
s2.kode_barang
) s1
LEFT JOIN (
SELECT
sm.tanggal,
sm.kode_barang,
sb.nama,
SUM(sm.jumlah) AS total_jumlah
FROM
ws_fh.simas_mutasi sm
JOIN ws_fh.simas_barang sb ON sb.id = sm.kode_barang
WHERE
sb.jenis = 'ATK DAN SEJENISNYA'
AND sm.kegiatan != 'mengeluarkan stok'
AND sm.kegiatan != 'mengubah barang'
AND sm.tanggal BETWEEN '2022-07-01' AND '2022-07-31'
GROUP BY
sb.nama
) s2 ON s1.nama = s2.nama
UNION
SELECT
COALESCE(s1.kode_barang, s2.kode_barang) AS kode_barang,
COALESCE(s1.nama, s2.nama) AS nama,
COALESCE(s1.sisa, 0) AS sisa,
COALESCE(s2.total_jumlah, 0) AS total_jumlah,
COALESCE(s1.sisa, 0) + COALESCE(s2.total_jumlah, 0) AS total
FROM
(
SELECT
s2.max_tanggal,
s2.kode_barang,
s1.nama,
s2.sisa
FROM
ws_fh.simas_barang s1
RIGHT JOIN (
SELECT
sm1.kode_barang,
sm1.tanggal AS max_tanggal,
sm1.created_at,
sm1.sisa
FROM
ws_fh.simas_mutasi sm1
JOIN (
SELECT
kode_barang,
MAX(tanggal) AS max_tanggal,
created_at
FROM
ws_fh.simas_mutasi
WHERE
tanggal BETWEEN '2022-06-01' AND '2022-06-30'
GROUP BY
kode_barang
) sm2 ON sm2.kode_barang = sm1.kode_barang
AND sm2.max_tanggal = sm1.tanggal
GROUP BY
sm1.kode_barang,
sm1.created_at,
sm1.sisa
ORDER BY
sm1.kode_barang,
sm1.created_at DESC
) s2 ON s2.kode_barang = s1.id
WHERE
s1.jenis = 'ATK DAN SEJENISNYA'
GROUP BY
s2.kode_barang
) s1
RIGHT JOIN (
SELECT
sm.tanggal,
sm.kode_barang,
sb.nama,
SUM(sm.jumlah) AS total_jumlah,
sm.kegiatan
FROM
ws_fh.simas_mutasi sm
JOIN ws_fh.simas_barang sb ON sb.id = sm.kode_barang
WHERE
sb.jenis = 'ATK DAN SEJENISNYA'
AND sm.kegiatan != 'mengeluarkan stok'
AND sm.kegiatan != 'mengubah barang'
AND sm.tanggal BETWEEN '2022-07-01' AND '2022-07-31'
GROUP BY
sb.nama
) s2 ON s1.nama = s2.nama
WHERE
s1.nama IS NULL
ORDER BY
nama),
CTE2 AS(
SELECT COALESCE(sm.kode_barang, s1.id) AS kode_barang,
COALESCE(sb.nama, s1.nama) AS nama,
SUM(sm.jumlah) AS keluar_JULI,
s2.sisa AS sisa_JULI
FROM ws_fh.simas_mutasi sm
LEFT JOIN ws_fh.simas_barang sb ON sb.id = sm.kode_barang
LEFT JOIN (
SELECT sm1.kode_barang, sm1.tanggal AS max_tanggal, sm1.created_at, sm1.sisa
FROM ws_fh.simas_mutasi sm1
JOIN (
SELECT kode_barang, MAX(tanggal) AS max_tanggal, created_at
FROM ws_fh.simas_mutasi
WHERE tanggal BETWEEN '2022-07-01' AND '2022-07-31'
GROUP BY kode_barang
) sm2 ON sm2.kode_barang = sm1.kode_barang AND sm2.max_tanggal = sm1.tanggal
GROUP BY sm1.kode_barang, sm1.created_at, sm1.sisa
ORDER BY sm1.kode_barang, sm1.created_at DESC
) s2 ON s2.kode_barang = sb.id
RIGHT JOIN ws_fh.simas_barang s1 ON s1.id = s2.kode_barang
WHERE COALESCE(sb.jenis, s1.jenis) = 'ATK DAN SEJENISNYA'
GROUP BY COALESCE(sb.nama, s1.nama)
HAVING keluar_JULI IS NOT NULL AND sisa_JULI IS NOT NULL
ORDER BY COALESCE(sb.nama, s1.nama))
SELECT COALESCE(CTE1.kode_barang,CTE1.kode_barang) kode_barang,COALESCE(CTE1.nama,CTE1.nama) nama,
sisa_JUNI,masuk_JULI, keluar_JULI,sisa_JULI,total
FROM CTE1 LEFT JOIN CTE2 ON CTE1.kode_barang = CTE2.kode_barang
UNION
SELECT COALESCE(CTE1.kode_barang,CTE1.kode_barang),COALESCE(CTE1.nama,CTE1.nama),
sisa_JUNI,masuk_JULI, keluar_JULI,sisa_JULI,total
FROM CTE2 LEFT JOIN CTE1 ON CTE1.kode_barang = CTE2.kode_barang
ORDER BY 2
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论