2014-02-07 48 views
0

我需要一些幫助。SQL CASE當我想要一個「包括」行時

可以說我有一個表

ID Mark Transmition 
1 Ford A 
2 Ford A 
3 Ford M 
4 BMW M 
5 BMW M 
6 Ford A 

,現在我需要做的時候的情形。

CASE WHEN mark = 'Ford' then 'Ford' 
    WHEN mark = 'Ford' and Transmition = 'A' then ' including Fords with automatic transmitions' 

而我必須這樣做的情況下,而不是當時存在。因爲我需要在OBIEE報告中使用它。

結果我需要的是這樣的:

Mark         Count 
Ford         4 
inc Ford with automatic transmition 3 

但結果評估爲TRUE的情況下bith ...

期待您的迴音。

+0

那麼,有什麼問題嗎? –

+0

評估爲TRUE了時間......不知何故,我需要有這樣的行: 福特4 包括自動傳歷程3 – andrepaal

回答

1

查詢:

SELECT CASE WHEN mark = 'Ford' THEN 'Ford' END AS Mark, 
COUNT(*) 
FROM Table1 t 
WHERE mark = 'Ford' 
GROUP BY mark 
UNION ALL 
SELECT CASE WHEN mark = 'Ford' AND Transmition = 'A' 
       THEN 'including Fords with automatic transmitions' END AS Mark, 
COUNT(*) 
FROM Table1 t 
WHERE mark = 'Ford' 
AND Transmition = 'A' 
GROUP BY CASE WHEN mark = 'Ford' AND Transmition = 'A' 
       THEN 'including Fords with automatic transmitions' END 

結果:

|          MARK | COUNT(*) | 
|---------------------------------------------|----------| 
|          Ford |  4 | 
| including Fords with automatic transmitions |  3 | 
0

不確定你的問題,但似乎你試圖通過指出case whencase when exists來避免null行。

什麼

SELECT 
    ' including Fords with automatic transmitions' AS "DESCRIPTION", 
    COUNT("ID") AS "POCET" 
FROM some_table 
WHERE "Mark" = 'Ford' AND "Transmition" = 'A' 
GROUP BY "Mark" 

UNION ALL 

SELECT 
    "Mark", 
    COUNT("ID") AS "POCET" 
FROM some_table 
WHERE "Mark" = 'Ford' 
GROUP BY "Mark" 
+0

Sorri,我編輯的問題,一點點,使其更加清晰 – andrepaal

+0

更新答案渡口,因爲問題改變。 –

0

你可以不用CASE這樣的:

SELECT Mark, count(1) FROM car GROUP By Mark 
UNION 
SELECT 'Including '||Mark||' with automatic transmitions' as MM, count(1) FROM car WHERE Transmition = 'A' GROUP By MM; 

結果:

|          MARK | COUNT(*) | 
|---------------------------------------------|----------| 
|           BMW |  2 | 
|          Ford |  4 | 
| including Fords with automatic transmitions |  3 | 
0

只要試試這個

SELECT MARK,Count(1) AS Count FROM 
(
    SELECT CASE WHEN mark = 'Ford' and Transmition = 'A' then ' including Fords with automatic transmitions' 
      WHEN mark = 'Ford' and Transmition <> 'A' then 'Ford' 
      Else 'BMW' 
      END Mark 
    FROM Table1 
) AS T Group By T.Mark 

O/P:

MARK           COUNT 
including Fords with automatic transmitions  3 
BMW            2 
Ford           1 

FIDDLE DEMO