MySQL - 然后选择案例

时间:2017-05-01 12:56:10

标签: mysql

我必须执行SELECT并格式化输出列以显示下面的结果,我不能使用XML PATH,因为我使用的是MySQL,也不能使用WHERE,因为它正在执行另一个条件。 / p>

我有这张桌子:

enter image description here

我想要这个结果:

enter image description here

我开始思考这样的事情:

, CASE
   WHEN csc.NAME LIKE ('Microsoft Office%')
    AND csc.NAME NOT IN ('Microsoft Office Shortcut Bar 2000','Office 2011 for Mac') THEN ???
   ELSE '-'
  END AS `sw_microsoft_office`

但我不知道如何在THEN之后显示条件结果。

这是原始查询:

SELECT DATE_FORMAT(om.BIOS_RELEASE_DATE, '%Y/%m/%d') AS `Ship_Date`
 , CASE
    WHEN ksc.CLIENT_CONNECTED = '0' THEN 'Not connected'
    WHEN ksc.CLIENT_CONNECTED = '1' THEN 'Conected'
    ELSE 'Not identified'
   END AS St
 , DATE_FORMAT(om.LAST_INVENTORY, '%Y/%m/%d') AS `Last_Inventory`
 , CASE
    WHEN (DATEDIFF(CURDATE() , DATE(om.LAST_INVENTORY)) >= 30) THEN 'Não'
    ELSE 'Sim'
   END AS `Inv_Less_Than_30`
 , om.NAME `Hostname`
 , om.IP `IP`
 , om.OS_NAME `OS`
 , CASE
    WHEN om.USER IS NULL THEN '-'
    WHEN om.USER = '' THEN '-'
    ELSE om.USER
   END AS `Usr`
 , CASE
    WHEN om.USER_FULLNAME IS NULL THEN '-'
    WHEN om.USER_FULLNAME = '' THEN '-'
    ELSE om.USER_FULLNAME
   END AS `User_Fullname`
 , CASE
    WHEN oaDepartment.Name IS NULL THEN '-'
    WHEN oaDepartment.Name = '' THEN '-'
    ELSE oaDepartment.Name
   END AS `Department`
 , CASE
    WHEN oad5.FIELD_10104 IS NULL THEN '-'
    WHEN oad5.FIELD_10104 = '' THEN '-'
    WHEN oad5.FIELD_10104 = 'Não tem' THEN '-'
    WHEN oad5.FIELD_10104 LIKE ('001%') THEN 'Company1'
    WHEN oad5.FIELD_10104 LIKE ('002%') THEN 'Company2'
    WHEN oad5.FIELD_10104 LIKE ('003%') THEN 'Company3'
    WHEN oad5.FIELD_10104 LIKE ('004%') THEN 'Company4'
    WHEN oad5.FIELD_10104 LIKE ('005%') THEN 'Company5'
    WHEN oad5.FIELD_10104 LIKE ('006%') THEN 'Company6'
    WHEN oad5.FIELD_10104 LIKE ('014%') THEN 'Company7'
    ELSE oad5.FIELD_10104
   END AS `Company`
 , CASE
    WHEN oaLocation.NAME IS NULL THEN '-'
    WHEN oaLocation.NAME = 'Unassigned' THEN '-'
    ELSE oaLocation.NAME
   END AS `Location`
 , CASE
    WHEN oaMachine.NAME IS NULL THEN '-'
    WHEN oaMachine.NAME = 'Unassigned' THEN '-'
    ELSE oaMachine.NAME
   END AS `Assets - St`
 , CASE
    WHEN oad5.FIELD_10231 IS NULL THEN '-'
    WHEN oad5.FIELD_10231 = '' THEN '-'
    ELSE oad5.FIELD_10231
   END AS `Situation`
 , CASE
    WHEN csc.NAME LIKE ('Microsoft Office%')
     AND csc.NAME NOT IN ('Microsoft Office Shortcut Bar 2000','Office 2011 for Mac') THEN ???
    ELSE '-'
   END AS sw_microsoft_office
FROM ORG1.MACHINE om
 LEFT JOIN KBSYS.SMMP_CONNECTION ksc ON om.KUID = ksc.KUID
 LEFT JOIN ORG1.ASSET oa ON om.ID = oa.MAPPED_ID
  AND oa.ASSET_TYPE_ID = 5
 LEFT JOIN ORG1.ASSET oaLocation ON oaLocation.ID = oa.LOCATION_ID
 LEFT JOIN ORG1.ASSET_DATA_5 oad5 ON oad5.ID = oa.ASSET_DATA_ID
 LEFT JOIN ORG1.ASSET_ASSOCIATION oaa ON oa.ID = oaa.ASSET_ID
  AND oaa.ASSET_FIELD_ID = 10109
 LEFT JOIN ORG1.ASSET_ASSOCIATION J10112 ON J10112.ASSET_ID = oa.ID AND J10112.ASSET_FIELD_ID = 10112
 LEFT JOIN ORG1.ASSET oaDepartment ON oaDepartment.ID = J10112.ASSOCIATED_ASSET_ID
 LEFT JOIN ORG1.ASSET oaMachine ON oaMachine.ID = oaa.ASSOCIATED_ASSET_ID
 INNER JOIN ORG1.SAM_MACHINE_JT osmj ON om.ID = osmj.MACHINE_ID
 INNER JOIN CATALOG.SAM_CATALOG csc ON osmj.SAM_CATALOG_ID = csc.ID
 INNER JOIN ORG1.SAM_COMPLIANCE_DETAIL oscd ON csc.ID = oscd.SAM_CATALOG_ID
WHERE om.NAME NOT LIKE ('GZT%')
 AND om.NAME NOT LIKE ('SERVER%')
 AND om.CS_MANUFACTURER NOT LIKE ('Apple%')
 AND om.CS_MANUFACTURER NOT LIKE ('Parallels%')
 AND om.CS_MANUFACTURER NOT LIKE ('VM%')
 AND om.OS_NAME LIKE ('Microsoft Windows XP%')
GROUP BY om.NAME

2 个答案:

答案 0 :(得分:0)

我猜你想要

, CASE WHEN csc.NAME LIKE ('Microsoft Office%') AND csc.NAME NOT IN ('Microsoft Office Shortcut Bar 2000','Office 2011 for Mac') THEN csc.NAME ELSE '-' END AS sw_microsoft_office

答案 1 :(得分:0)

我要感谢大家帮助我解决这个问题!你的贡献让我得到了我需要的结果!非常感谢你!

, REPLACE(REPLACE(GROUP_CONCAT(
   CASE
    WHEN csc.NAME LIKE ('Microsoft Office%')
     AND csc.NAME NOT IN ('Microsoft Office Shortcut Bar 2000','Office 2011 for Mac') THEN csc.NAME
    ELSE '-'
   END
   ORDER BY CASE
             WHEN csc.NAME LIKE ('%2000%') THEN 1
             WHEN csc.NAME LIKE ('%2003%') THEN 2
             WHEN csc.NAME LIKE ('%2007%') THEN 3
             WHEN csc.NAME LIKE ('%2010%') THEN 4
             WHEN csc.NAME LIKE ('%2013%') THEN 5
             WHEN csc.NAME LIKE ('%2016%') THEN 6
             ELSE '7'
            END
    , CASE
       WHEN csc.NAME LIKE ('%Small Business%') THEN 1
       WHEN csc.NAME LIKE ('%Home and Business%') THEN 2
       WHEN csc.NAME LIKE ('%Standard%') THEN 3
       WHEN csc.NAME LIKE ('%Professional%') THEN 4
       ELSE '5'
      END
  ),',-',''),'-,','') AS sw_microsoft_office