2013-03-12 50 views
0

您好我在Oracle 11g下調用SQL查詢時遇到問題。在MySQL下工作正常。爲什麼MySQL查詢不在Oracle 11g中編譯?

只要看看SQLFIDDLE

(SELECT IF(c.name IS NULL, '- Unknow -', c.name) as name, 
    v.site, 
    SUM(CASE WHEN v.status_id = 1 THEN 1 ELSE 0 END) Total_SuspectedViolations, 
    SUM(CASE WHEN v.status_id = 2 THEN 1 ELSE 0 END) Total_ConfirmedViolations, 
    SUM(CASE WHEN v.status_id = 3 THEN 1 ELSE 0 END) Total_ConfirmedNoViolations, 
    SUM(CASE WHEN v.status_id = 4 THEN 1 ELSE 0 END) Total_NotDetermined, 
    COUNT(*) Total, 
    0 'isAll' 
FROM violations v LEFT JOIN country c ON c.name = v.country 
GROUP BY c.name, v.site) 
union(
SELECT IF(c.name IS NULL, '- Unknow -', c.name) as name, 
    '- All -', 
    SUM(CASE WHEN v.status_id = 1 THEN 1 ELSE 0 END) Total_SuspectedViolations, 
    SUM(CASE WHEN v.status_id = 2 THEN 1 ELSE 0 END) Total_ConfirmedViolations, 
    SUM(CASE WHEN v.status_id = 3 THEN 1 ELSE 0 END) Total_ConfirmedNoViolations, 
    SUM(CASE WHEN v.status_id = 4 THEN 1 ELSE 0 END) Total_NotDetermined, 
    COUNT(*) Total, 
    1 'isAll' 
FROM violations v LEFT JOIN country c ON c.name = v.country 
GROUP BY c.name) 
UNION (
SELECT '- All -', 
    '- All -', 
    SUM(CASE WHEN v.status_id = 1 THEN 1 ELSE 0 END) Total_SuspectedViolations, 
    SUM(CASE WHEN v.status_id = 2 THEN 1 ELSE 0 END) Total_ConfirmedViolations, 
    SUM(CASE WHEN v.status_id = 3 THEN 1 ELSE 0 END) Total_ConfirmedNoViolations, 
    SUM(CASE WHEN v.status_id = 4 THEN 1 ELSE 0 END) Total_NotDetermined, 
    COUNT(*) Total, 
    1 'isAll' 
FROM violations v LEFT JOIN country c ON c.name = v.country) 
ORDER BY name, isAll DESC, site 

你有任何想法與此代碼的問題?

+0

您需要用引號(「)代替撇號(')。 – 2013-03-12 19:46:23

+0

從」更改爲「後沒有區別。你可以在我的文章(http://www.sqlfiddle.com/#!4/93c94/24) – Roman 2013-03-12 19:49:43

+0

鏈接下看到它的sql小提琴是的,我明白了。另外我的評論只是爲了列名而不是字符串。 – 2013-03-12 19:55:29

回答

2

有幾件事情錯了...

首先,IF()不是甲骨文有效語法。您需要將其更改爲CASE聲明。

其次,您不能使用''命名列。所以你需要從'isAll'去掉'''。

最後,您按網站進行排序,但您並未實際命名該列。

最後的查詢應該是這個樣子:

(SELECT CASE WHEN c.name IS NULL THEN '- Unknow -' ELSE c.name END as name, 
    v.site as site, 
    SUM(CASE WHEN v.status_id = 1 THEN 1 ELSE 0 END) Total_SuspectedViolations, 
    SUM(CASE WHEN v.status_id = 2 THEN 1 ELSE 0 END) Total_ConfirmedViolations, 
    SUM(CASE WHEN v.status_id = 3 THEN 1 ELSE 0 END) Total_ConfirmedNoViolations, 
    SUM(CASE WHEN v.status_id = 4 THEN 1 ELSE 0 END) Total_NotDetermined, 
    COUNT(*) Total, 
    0 isAll 
FROM violations v LEFT JOIN country c ON c.name = v.country 
GROUP BY c.name, v.site) 
UNION (
SELECT CASE WHEN c.name IS NULL THEN '- Unknow -' ELSE c.name END as name, 
    '- All -' as site, 
    SUM(CASE WHEN v.status_id = 1 THEN 1 ELSE 0 END) Total_SuspectedViolations, 
    SUM(CASE WHEN v.status_id = 2 THEN 1 ELSE 0 END) Total_ConfirmedViolations, 
    SUM(CASE WHEN v.status_id = 3 THEN 1 ELSE 0 END) Total_ConfirmedNoViolations, 
    SUM(CASE WHEN v.status_id = 4 THEN 1 ELSE 0 END) Total_NotDetermined, 
    COUNT(*) Total, 
    1 isAll 
FROM violations v LEFT JOIN country c ON c.name = v.country 
GROUP BY c.name) 
UNION (
SELECT '- All -', 
    '- All -' as site, 
    SUM(CASE WHEN v.status_id = 1 THEN 1 ELSE 0 END) Total_SuspectedViolations, 
    SUM(CASE WHEN v.status_id = 2 THEN 1 ELSE 0 END) Total_ConfirmedViolations, 
    SUM(CASE WHEN v.status_id = 3 THEN 1 ELSE 0 END) Total_ConfirmedNoViolations, 
    SUM(CASE WHEN v.status_id = 4 THEN 1 ELSE 0 END) Total_NotDetermined, 
    COUNT(*) Total, 
    1 isAll 
FROM violations v LEFT JOIN country c ON c.name = v.country) 
ORDER BY name, isAll DESC, site 

您可以在此SQLFiddle測試:http://sqlfiddle.com/#!4/93c94/55

1

繼格蘭特的回答,幾個其他的優化的。

你可能想更換:

CASE WHEN c.name IS NULL THEN '- Unknow -' ELSE c.name END 

有:

Coalesce(c.name,'- Unknow -') 

此外,您在多個級別聚集,而且也應該是一個語法,這將是更有效和更羅嗦:GROUP BY ROLLUP()

蒂姆·霍爾在這裏先進的小組討論上寫了很好的文章:http://www.oracle-base.com/articles/misc/rollup-cube-grouping-functions-and-grouping-sets.php

如果您決定不使用ROLLUP,那麼將UNION更改爲UNION ALL - UNION具有隱式的區別,它在此處不會執行任何操作。

1

這是您的更新SQL Fiddle運行。請確保它仍然會產生您期望的結果。