mysql列轉(zhuǎn)行以及年月分組的示例代碼分享
發(fā)表時(shí)間:2023-09-04 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]下面小編就為大家?guī)硪黄猰ysql列轉(zhuǎn)行以及年月分組實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧如下所示:SELECT count(DISTINCT(a.rect_id)) zcount, a.job_dept, DATE_FORMAT(submit_date...
下面小編就為大家?guī)硪黄?a target="_blank">mysql列轉(zhuǎn)行以及年月
分組實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
如下所示:
SELECT count(DISTINCT(a.rect_id)) zcount, a.job_dept,
DATE_FORMAT(submit_date, '%Y-%m') zsubmit_date
FROM
表名 a
WHERE
a.statu = 3
AND a.rstatu = 2
AND a.job_dept IN ('19', '20', '21')
GROUP BY
a.job_dept,
DATE_FORMAT(submit_date, '%Y-%m')
其中關(guān)鍵在于DATE_FORMAT(submit_date, '%Y-%m')對(duì)時(shí)間年月進(jìn)行了分組排序
SELECT
zsubmit_date,
MAX(CASE WHEN job_dept = '19' THEN zcount ELSE 0 END ) 19zcount,
MAX(CASE WHEN job_dept = '20' THEN zcount ELSE 0 END ) 20zcount,
MAX(CASE WHEN job_dept = '21' THEN zcount ELSE 0 END ) 21zcount
FROM
(
SELECT
count(DISTINCT(a.rect_id)) zcount, a.job_dept,
DATE_FORMAT(submit_date, '%Y-%m') zsubmit_date
FROM
表名 a
WHERE
a.statu = 3
AND a.rstatu = 2
AND a.job_dept IN ('19', '20', '21')
GROUP BY
a.job_dept,
DATE_FORMAT(submit_date, '%Y-%m')
) q GROUP BY
zsubmit_date
以上就是mysql列轉(zhuǎn)行以及年月分組的示例代碼分享的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
學(xué)習(xí)教程快速掌握從入門到精通的SQL知識(shí)。