需要帮助来产生价值

时间:2017-10-20 10:49:46

标签: sql sql-server sql-server-2008

我已经编辑了我的查询,如下所示:

SELECT
    ser.SERVER_COMPONENT_NAME, ITServ.IT_SERVICE_BUSINESS_CRITICALIT, 
    app.APPCROWNJEWEL,
    MAX(CASE
           WHEN ITServ.IT_SERVICE_BUSINESS_CRITICALIT = 'Tier o' THEN 1
           WHEN ITServ.IT_SERVICE_BUSINESS_CRITICALIT = 'Tier 1' THEN 3
           WHEN ITServ.IT_SERVICE_BUSINESS_CRITICALIT = 'Tier 2' THEN 5
           WHEN ITServ.IT_SERVICE_BUSINESS_CRITICALIT IS NULL THEN 5
           ELSE 0
        END) AS TierRatingValue,
    MAX(CASE   
           WHEN app.APPCROWNJEWEL = 'Yes' THEN 3
           WHEN app.APPCROWNJEWEL = 'No' THEN 2
           ELSE 1
        END) AS JewelsValue
FROM
    TDM_FACT_SERVER ser 
LEFT JOIN 
    TDM_RKT_SM_TO_SERVER smServer ON smServer.SERVER_COMPONENT_ID = Ser.SERVER_COMPONENT_ID
                                  AND smServer.SOFTWARE_MODULE_COMPONENT_ID IN (SELECT DISTINCT sm.SOFTWARE_MODULE_COMPONENT_ID 
                                                                                FROM software_module sm
                                                                                WHERE sm.SOFTWARE_MODULE_COMPONENT_NAME LIKE 'DBSM:%' OR  
                                                                                      sm.SOFTWARE_MODULE_COMPONENT_NAME LIKE 'MWSM:%' OR
                                                                                      sm.SOFTWARE_MODULE_COMPONENT_NAME like 'CMS:%')
LEFT JOIN 
    SOFTWARE_MODULE sm ON sm.SOFTWARE_MODULE_COMPONENT_ID = smServer.SOFTWARE_MODULE_COMPONENT_ID
LEFT JOIN 
    TDM_RKT_IT_SERVICE_TO_SM ITSERVICETOSM ON sm.SOFTWARE_MODULE_COMPONENT_ID = ITSERVICETOSM.SOFTWARE_MODULE_COMPONENT_ID
                                           AND ITSERVICETOSM.IT_SERVICE_COMPONENT_ID IN (SELECT DISTINCT Itserv.IT_SERVICE_COMPONENT_ID
                                                                                         FROM tdm_fact_it_srvc Itserv
                                                                                         WHERE Itserv.IT_SERVICE_SERVICE_RECONCILIAT IN ('Active in PLADA but not present in GSD', 
                                                                                                                                         'Active in GSD but not present in PLADA',
                                                                                                                                         'Active in GSD and PLADA',
                                                                                                                                         'Active in GSD but demised in PLADA',
                                                                                                                                         'Active in PLADA but demised in GSD'))    
LEFT JOIN 
    TDM_FACT_IT_SRVC ITServ ON ITSERVICETOSM.IT_SERVICE_COMPONENT_ID = ITServ.IT_SERVICE_COMPONENT_ID
LEFT JOIN 
    TDM_RKT_APP_TO_ITSERVICE ApptoITService ON ITServ.IT_SERVICE_COMPONENT_ID = ApptoITService.IT_SERVICE_COMPONENT_ID
LEFT JOIN  
    hsbc_ci_inventory_as app ON ApptoITService.APPLICATION_COMPONENT_ID = app.appid
WHERE 
    ser.SERVER_COMPONENT_NAME = 'GBL03427'
GROUP BY 
    ser.SERVER_COMPONENT_NAME, ITServ.IT_SERVICE_BUSINESS_CRITICALIT, 
    app.APPCROWNJEWEL

下面是输出,因为它按预期生成了两个新列。

Server          TierRating   Jewels         TierRatingValue      JewelsValue
---------       ----------   ----------     ----------------     -----------
GBL03427          Tier 0     No                 0                    2
GBL03427          Tier 1     Yes                3                    3
GBL03427          Tier 2     Undefined          5                    1
GBL03427          Tier 1     No                 3                    2
GBL03427          Tier 2     No                 5                    2
GBL03427          Tier 0     Yes                0                    3
GBL03427          Tier 1     NULL               3                    1
GBL03427          NULL       NULL               5                    1
GBL03427          Tier 1     Undefined          3                    1
GBL03427          Tier 0     Undefined          0                    1

但我希望将输出视为一行,其中TierRatingValue和JewelsValue的最大值如下所示。

Server      TierRating   Jewels         TierRatingValue      JewelsValue
---------   ----------   ----------     ----------------     -----------
GBL03427    Tier 2       Yes                5                   3

1 个答案:

答案 0 :(得分:0)

如果某个现有值='第0层'则替换5之类的值。你会使用case expression,它可能如下所示:

case
     when TierRating = 'Tier 0' then 1
     when TierRating = 'Tier 1' then 3
     when TierRating = 'Tier 2' then 5
     when TierRating IS NULL then 5
     when TierRating = 'Yes' then 3
     when TierRating = 'No' then 2
     else 1
end AS Tier_Rank

假设case表达式确实使用整数1,2等,那么为了达到最大值,你可以将整个case表达式放在MAX()函数中,如下所示:

MAX(
    case
         when TierRating = 'Tier o' then 1
         when TierRating = 'Tier 1' then 3
         when TierRating = 'Tier 2' then 5
         when TierRating IS NULL then 5
         when TierRating = 'Yes' then 3
         when TierRating = 'No' then 2
         else 1
    end
    ) AS Tier_Rank

并使用GROUP BY子句将其合并为查询的任何其他列。