2017-08-07 67 views
0

下面是兩個表(請參閱SQLFiddle)的示例,我試圖創建select語句,其中一個表是查找(Facility),另一個是數據表(WOENTITY) ,我想選擇從查找中獲取facitityname,並且每當它找到facilityID的多個條目時,只是將文本寫爲'Multiple locations',或者如果它找到null或零,然後寫'unknown',否則只需寫入facilityname,我試圖做一個用單個條目替換多個計數的情況。任何幫助表示讚賞。Case語句和加入多個表的SQL問題

連接到SQL小提琴:Click Here

下面是表看起來怎麼樣,表WOEntity截圖和表設施 Image showing the table schema and expected end result

我首先從WOEntity表,然後想多個位置ID的獲取計數創建一個選擇Case語句但無濟於事,這裏是草案代碼。

select e.WOID, count(e.ENTITYUID) as CNT, 
    (case 
      when e.ENTITYUID <> '0' and count(e.ENTITYUID) <=1 then 
       (Select FACILITYNAME from FACILITY l where e.ENTITYUID = 
        l.FACILITYID) 
      when count(e.ENTITYUID) > 2 then 'MULTIPLE FACILITIES' 
      else 'UNKNOWN LOCATIONS' 
    end) as facilityName 
From WOENTITY e 
group by e.WOID order by WOID; 
+0

什麼是您預期的結果? – Horaciux

+0

感謝您的回覆,預期的結果是我只想通過WOID進行分組,而不是實體進行分組。我希望看到兩列,不同的WOID(WOID的每個條目)和設施名稱(來自查找) – Abdul

+0

OP,當爲您的問題提供其他信息時,請編輯您的原始問題。謝謝,歡迎來到Stack Overflow。 – STLDeveloper

回答

0

我會親自做了JOIN而不是相關子查詢,然後在CASE表達式使用COUNT

SELECT e.WOID, 
     CASE WHEN COUNT(f.FACILITYID) = 0 THEN 'UNKNOWN LOCATIONS' 
      WHEN COUNT(f.FACILITYID) > 1 THEN 'MULTIPLE FACILITIES' 
      ELSE MAX(f.FACILITYNAME) 
     END AS facilityName 
FROM WOENTITY AS e 
     LEFT JOIN FACILITY AS f 
      ON f.FACILITYID = e.ENTITYUID 
GROUP BY e.WOID 
ORDER BY e.WOID; 

SQL Fiddle

既然你處理一個以上的,或零的情況下記錄與JOIN匹配,到達ELSE時,只有一條記錄,所以雖然使用MAX聚合函數,但只有一個輸入行,所以這沒有什麼作用,除了允許您選擇沒有gro的設施名稱由它提升。

編輯

我剛纔注意到,加拿大應顯示爲多個位置,即使只有其中一個存在於設備表,在這種情況下,你需要改變的情況下expresison咯,改變COUNT(f.FACILITYID)COUNT(*)用於第二謂詞:

SELECT e.WOID, 
     CASE WHEN COUNT(f.FACILITYID) = 0 THEN 'UNKNOWN LOCATIONS' 
      WHEN COUNT(*) > 1 THEN 'MULTIPLE FACILITIES' 
      ELSE MAX(f.FACILITYNAME) 
     END AS facilityName 
FROM WOENTITY AS e 
     LEFT JOIN FACILITY AS f 
      ON f.FACILITYID = e.ENTITYUID 
GROUP BY e.WOID 
ORDER BY e.WOID; 

SQL Fiddle

+0

感謝您的迴應,編輯看起來不錯,我正在測試實際數據(成千上萬條記錄)的結果,到目前爲止這麼好。我也發現@ Horaciux的解決方案也能正常工作。假設你和Horaciux都做同樣的事情是否安全?將在更少的測試中發佈結果 – Abdul

0

的語句,你說你隨參數錯過了一個gorup ...財產以後這樣的:

select e.WOID, count(e.ENTITYUID) as CNT, 
    (case 
      when e.ENTITYUID <> '0' and count(e.ENTITYUID) <=1 then 
       (Select FACILITYNAME from FACILITY l where e.ENTITYUID = 
        l.FACILITYID) 
      when count(e.ENTITYUID) > 2 then 'MULTIPLE FACILITIES' 
      else 'UNKNOWN LOCATIONS' 
    end) as facilityName 
From WOENTITY e 
group by e.WOID,e.ENTITYUID order by WOID; 
+0

感謝您的回覆,我不想通過EntityUid進行分組,因爲這會使獨特的WOID消失,我希望看到每個WOID及其設施名稱的單個條目。從上面的代碼中,我看到WOID重複了多次 – Abdul

0

這是考慮類似「加拿大

的情況下@GarethD答案的修改
SELECT e.WOID, 
     CASE WHEN COUNT(f.FACILITYID) = 0 THEN 'UNKNOWN LOCATIONS' 
      WHEN COUNT(f.FACILITYID) >0 and COUNT(coalesce(f.FACILITYID,'x'))>COUNT(f.FACILITYID) then 'MULTIPLE OFFICES' 
      WHEN COUNT(f.FACILITYID) > 1 then 'MULTIPLE OFFICES' 
      ELSE MAX(f.FACILITYNAME) 
     END AS facilityName 
FROM WOENTITY AS e 
     LEFT JOIN FACILITY AS f 
      ON f.FACILITYID = e.ENTITYUID 
GROUP BY e.WOID 
ORDER BY e.WOID; 
+0

@Horacius,感謝您的回覆,我不想通過EntityUid進行分組,因爲這會使獨特的WOID消失,我希望看到每個WOID及其設施名稱的單個條目。從你上面的代碼中,我看到WOID重複了多次 – Abdul

+0

@Abdul看到我的編輯,它現在帶給你預期的結果。 – Horaciux

0

請試試這個

Select FACILITYID, 
    CASE WHEN (SELECT COUNT(*) 
       FROM WOEnTITY 
       WHERE WOEnTITY.ENTITYUID = FACILITYID) > 0 
     THEN 'MULTIPLE LOCATIONS' 
     ELSE '' 
    END 
FROM FACILITY 
0

您可以按照以下查詢:如下

Select Row_Number() over(order by Id) as Id, Woid, Case when FACILITYID is null then 'UNKNOWN LOCATIONS' 
       when RowCnt > 1 then 'MULTIPLE OFFICES' else FACILITYNAME end as FacilityName, 
       RowCnt as CountFacilities 
       from (
    Select *, RowCnt = count(EntityUid) over(partition by woid), 
    RowN = Row_Number() over(Partition by woid order by Id) 
    from WOENTITY we 
    left join Facility f 
    on we.ENTITYUID = f.FACILITYID 
) a 
Where a.RowN = 1 

輸出:


+----+---------+-------------------+-----------------+ 
| Id | Woid | FacilityName | CountFacilities | 
+----+---------+-------------------+-----------------+ 
| 1 | Canada | MULTIPLE OFFICES |    2 | 
| 2 | Germany | GREEN    |    1 | 
| 3 | India | GREEN    |    1 | 
| 4 | UK  | RED    |    1 | 
| 5 | US  | MULTIPLE OFFICES |    3 | 
| 6 | JAPAN | UNKNOWN LOCATIONS |    1 | 
+----+---------+-------------------+-----------------+