主頁(yè) > 知識(shí)庫(kù) > mysql 行列轉(zhuǎn)換的示例代碼

mysql 行列轉(zhuǎn)換的示例代碼

熱門標(biāo)簽:怎樣在地圖標(biāo)注銷售區(qū)域 啥是企業(yè)400電話辦理 外呼系統(tǒng)打電話上限是多少 南昌三維地圖標(biāo)注 武漢網(wǎng)絡(luò)外呼系統(tǒng)服務(wù)商 地圖標(biāo)注費(fèi)用是多少 曲靖移動(dòng)外呼系統(tǒng)公司 電話外呼系統(tǒng)改號(hào) 百應(yīng)電話機(jī)器人優(yōu)勢(shì)

一、需求

我們有三張表,我們需要分類統(tǒng)計(jì)一段時(shí)間內(nèi)抗生素的不同藥敏結(jié)果,即 report_item_drugs 表的 drugs_result, 在不同項(xiàng)目project_name 和不同抗生素 antibiotic_dict_name 下的占比,并將藥敏結(jié)果顯示在行上,效果如下:

三張?jiān)急恚▋H取需要的字段示例),分別是:

報(bào)告表

項(xiàng)目表

抗生素表(藥敏結(jié)果drugs_result為一列值)

二、實(shí)現(xiàn)

1、按照項(xiàng)目、抗生素分組求出檢出的總數(shù)

SELECT 
 A.project_name,A.antibiotic_dict_name,SUM(nums) AS 檢出總數(shù)
FROM 
(
      SELECT i.project_name,d.antibiotic_dict_name,d.drugs_result,COUNT(d.id) AS nums FROM `report` r
       RIGHT JOIN report_item i ON r.id=i.report_id
       RIGHT JOIN report_item_drugs d ON d.report_item_id=i.id
       WHERE r.report_status=2 AND r.add_date BETWEEN '2020-01-01' AND '2020-12-30' 
       GROUP BY i.project_id,d.antibiotic_dict_id,d.drugs_result
 )  A
 GROUP BY A.project_name,A.antibiotic_dict_name

2、按照項(xiàng)目、抗生素、藥敏結(jié)果求出不同藥敏結(jié)果數(shù)量

SELECT i.project_name,d.antibiotic_dict_name,IF(d.drugs_result>'', d.drugs_result, '未填寫') AS drugs_result,COUNT(d.id) AS 數(shù)量 
FROM `report` r
RIGHT JOIN report_item i ON r.id=i.report_id
RIGHT JOIN report_item_drugs d ON d.report_item_id=i.id
WHERE r.report_status=2 AND r.add_date BETWEEN '2020-01-01' AND '2020-12-30' 
GROUP BY i.project_id,d.antibiotic_dict_id,d.drugs_result  

3、將兩個(gè)結(jié)果關(guān)聯(lián)到一起

SELECT 
      BB.project_name,BB.antibiotic_dict_name,BB.drugs_result,BB.`數(shù)量`,AA.`檢出總數(shù)`
    FROM 
        (
              SELECT 
                A.project_name,A.antibiotic_dict_name,SUM(nums) AS 檢出總數(shù)
              FROM 
              (
                    SELECT i.project_name,d.antibiotic_dict_name,d.drugs_result,COUNT(d.id) AS nums FROM `report` r
                    RIGHT JOIN report_item i ON r.id=i.report_id
                    RIGHT JOIN report_item_drugs d ON d.report_item_id=i.id
                    WHERE r.report_status=2 AND r.add_date BETWEEN '2020-01-01' AND '2020-12-30' 
                    GROUP BY i.project_id,d.antibiotic_dict_id,d.drugs_result
              )  A
              GROUP BY A.project_name,A.antibiotic_dict_name
        ) AA 
        RIGHT JOIN 
        (
              SELECT i.project_name,d.antibiotic_dict_name,IF(d.drugs_result>'', d.drugs_result, '未填寫') AS drugs_result,COUNT(d.id) AS 數(shù)量 
              FROM `report` r
              RIGHT JOIN report_item i ON r.id=i.report_id
              RIGHT JOIN report_item_drugs d ON d.report_item_id=i.id
              WHERE r.report_status=2 AND r.add_date BETWEEN '2020-01-01' AND '2020-12-30' 
              GROUP BY i.project_id,d.antibiotic_dict_id,d.drugs_result            
        )BB ON AA.project_name=BB.project_name AND AA.antibiotic_dict_name=BB.antibiotic_dict_name
    WHERE AA.`檢出總數(shù)`>''

4、一般來(lái)說(shuō),到上一步不同藥敏數(shù)量和總數(shù)都有了,可以直接求比例了

但是,我們需要的是將藥敏顯示到行上,直接求比不符合需求,所以我們需要將列轉(zhuǎn)換為行

我們借助于case when實(shí)現(xiàn)行列轉(zhuǎn)換,并將藥敏結(jié)果根據(jù)字典轉(zhuǎn)為方便閱讀的漢字


SELECT
  C.project_name 項(xiàng)目名稱,C.antibiotic_dict_name 抗生素名稱,C.`檢出總數(shù)`,
  SUM(CASE C.`drugs_result` WHEN 'D' THEN C.`數(shù)量` ELSE 0 END ) AS '劑量依賴性敏感',
  CONCAT(SUM(CASE C.`drugs_result` WHEN 'D' THEN FORMAT(C.`數(shù)量`/C.`檢出總數(shù)`*100,2) ELSE 0 END),'%') AS '劑量依賴性敏感比率',
  SUM(CASE C.`drugs_result` WHEN 'R' THEN C.`數(shù)量` ELSE 0 END ) AS '耐藥',
  CONCAT(SUM(CASE C.`drugs_result` WHEN 'R' THEN FORMAT(C.`數(shù)量`/C.`檢出總數(shù)`*100,2) ELSE 0 END),'%') AS '耐藥比率',
  SUM(CASE C.`drugs_result` WHEN 'S' THEN C.`數(shù)量` ELSE 0 END ) AS '敏感',
  CONCAT(SUM(CASE C.`drugs_result` WHEN 'S' THEN FORMAT(C.`數(shù)量`/C.`檢出總數(shù)`*100,2) ELSE 0 END),'%') AS '敏感比率',
  SUM(CASE C.`drugs_result` WHEN 'I' THEN C.`數(shù)量` ELSE 0 END ) AS '中介',
  CONCAT(SUM(CASE C.`drugs_result` WHEN 'I' THEN FORMAT(C.`數(shù)量`/C.`檢出總數(shù)`*100,2) ELSE 0 END),'%') AS '中介比率',
  SUM(CASE C.`drugs_result` WHEN 'n1' THEN C.`數(shù)量` ELSE 0 END ) AS '非敏感',
  CONCAT(SUM(CASE C.`drugs_result` WHEN 'n1' THEN FORMAT(C.`數(shù)量`/C.`檢出總數(shù)`*100,2) ELSE 0 END),'%') AS '非敏感比率',
  SUM(CASE C.`drugs_result` WHEN 'N' THEN C.`數(shù)量` ELSE 0 END ) AS '無(wú)',
  CONCAT(SUM(CASE C.`drugs_result` WHEN 'N' THEN FORMAT(C.`數(shù)量`/C.`檢出總數(shù)`*100,2) ELSE 0 END),'%') AS '無(wú)比率',
  SUM(CASE C.`drugs_result` WHEN '未填寫' THEN C.`數(shù)量` ELSE 0 END ) AS '未填寫',
  CONCAT(SUM(CASE C.`drugs_result` WHEN '未填寫' THEN FORMAT(C.`數(shù)量`/C.`檢出總數(shù)`*100,2) ELSE 0 END),'%') AS '未填寫比率'
FROM
(
    SELECT 
      BB.project_name,BB.antibiotic_dict_name,BB.drugs_result,BB.`數(shù)量`,AA.`檢出總數(shù)`
    FROM 
        (
              SELECT 
                A.project_name,A.antibiotic_dict_name,SUM(nums) AS 檢出總數(shù)
              FROM 
              (
                    SELECT i.project_name,d.antibiotic_dict_name,d.drugs_result,COUNT(d.id) AS nums FROM `report` r
                    RIGHT JOIN report_item i ON r.id=i.report_id
                    RIGHT JOIN report_item_drugs d ON d.report_item_id=i.id
                    WHERE r.report_status=2 AND r.add_date BETWEEN '2020-01-01' AND '2020-12-30' 
                    GROUP BY i.project_id,d.antibiotic_dict_id,d.drugs_result
              )  A
              GROUP BY A.project_name,A.antibiotic_dict_name
        ) AA 
        RIGHT JOIN 
        (
              SELECT i.project_name,d.antibiotic_dict_name,IF(d.drugs_result>'', d.drugs_result, '未填寫') AS drugs_result,COUNT(d.id) AS 數(shù)量 
              FROM `report` r
              RIGHT JOIN report_item i ON r.id=i.report_id
              RIGHT JOIN report_item_drugs d ON d.report_item_id=i.id
              WHERE r.report_status=2 AND r.add_date BETWEEN '2020-01-01' AND '2020-12-30' 
              GROUP BY i.project_id,d.antibiotic_dict_id,d.drugs_result            
        )BB ON AA.project_name=BB.project_name AND AA.antibiotic_dict_name=BB.antibiotic_dict_name
    WHERE AA.`檢出總數(shù)`>''                                        
) C
GROUP BY C.project_name,C.antibiotic_dict_name;

5、查看結(jié)果,成功轉(zhuǎn)換


到此這篇關(guān)于mysql 行列轉(zhuǎn)換的示例代碼的文章就介紹到這了,更多相關(guān)mysql 行列轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 數(shù)據(jù)庫(kù)實(shí)現(xiàn)行列轉(zhuǎn)換(mysql示例)
  • mysql 行列動(dòng)態(tài)轉(zhuǎn)換的實(shí)現(xiàn)(列聯(lián)表,交叉表)

標(biāo)簽:黑河 錦州 甘南 滄州 吉林 隨州 荊州 資陽(yáng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《mysql 行列轉(zhuǎn)換的示例代碼》,本文關(guān)鍵詞  mysql,行列,轉(zhuǎn)換,的,示例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《mysql 行列轉(zhuǎn)換的示例代碼》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于mysql 行列轉(zhuǎn)換的示例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章