2017-10-20 53 views
-3

我已經編輯我的查詢象下面這樣:在發生需要幫助值

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 
+2

請標記您正在使用的正確數據庫。MySQL,SQL-server和SQLite是不一樣的。 –

+0

如果您要提供樣本數據(例如插入語句或使用with子句),這也會有所幫助。你有沒有嘗試過「Group By」 - 激情?例如。選擇...按TierRating分組。 – Benjamin

+0

歡迎使用StackOverflow:如果您發佈代碼,XML或數據示例,請***在文本編輯器中突出顯示這些行,然後單擊編輯器工具欄上的「代碼示例」按鈕(「{}」)格式和語法突出顯示它! –

回答

0

要替換一個值,例如5如果一些現有的值=「0層」你可以使用一個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 

假設的情況下表達並使用整數1 ,2等然後在最大值到達可以放置這樣的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 

,並納入它像一個查詢的u任何其它列唱一個GROUP BY子句。

+0

感謝您的支持,我對查詢進行了更改並編輯了原始問題。你能幫我編輯一下這個問題嗎? – Vikas

+0

你的問題現在解決了嗎?你仍然有關於這個答案的問題嗎?要接受答案「[**點擊Tick **](https://ibb.co/ikqyO6)」以獲取更多信息,請參閱[help/accepting](https://stackoverflow.com/help/someone-answers) –