2012-12-04 190 views
2

我試圖運行以下查詢(針對Foodmart 2000),它在MySQL上運行良好。在SQL Server 2005上,我得到的只是一條「附近的語法錯誤」消息。將MySQL轉換爲SQL Server

SELECT * FROM ((SELECT COUNT(state_province) AS 'WA' 
       FROM customer c1 
       WHERE c1.state_province LIKE 'WA' 
       GROUP BY c1.state_province) as t1, 
       (SELECT COUNT(state_province) AS 'OR' 
       FROM customer c1 
       WHERE c1.state_province LIKE 'OR' 
       GROUP BY c1.state_province) as t2, 
       (SELECT COUNT(state_province) AS 'CA' 
       FROM customer c1 
       WHERE c1.state_province LIKE 'CA' 
       GROUP BY c1.state_province) as t3); 

究竟是什麼導致了這個問題?

+0

我認爲你應該爲主表提供一個別名。 'SELECT * FROM(.....)tbl' – arunes

+0

什麼是輸入以及該查詢的預期輸出是什麼? – Alex

回答

1

試試這個;使用SUMSELECT代替COUNT

SELECT SUM(CASE WHEN state_province LIKE 'WA' THEN 1 ELSE 0 END) AS 'WA', 
     SUM(CASE WHEN state_province LIKE 'OR' THEN 1 ELSE 0 END) AS 'OR', 
     SUM(CASE WHEN state_province LIKE 'CA' THEN 1 ELSE 0 END) AS 'CA' 
FROM customer --c1 
--GROUP BY c1.state_province 

編輯:工作演示,SQL Fiddle here

2

你缺少一個別名:

SELECT * 
FROM 
(
    select * 
    from 
    (SELECT COUNT(state_province) AS 'WA' 
    FROM customer c1 
    WHERE c1.state_province LIKE 'WA' 
    GROUP BY c1.state_province) as t1, 
    (SELECT COUNT(state_province) AS 'OR' 
    FROM customer c1 
    WHERE c1.state_province LIKE 'OR' 
    GROUP BY c1.state_province) as t2, 
    (SELECT COUNT(state_province) AS 'CA' 
    FROM customer c1 
    WHERE c1.state_province LIKE 'CA' 
    GROUP BY c1.state_province) as t3 
) src; -- < add this alias 

必須對所有派生表和子查詢的別名。

您也可以通過以下使用版本@卡夫的仍然有count

SELECT count(CASE WHEN state_province LIKE 'WA' THEN 1 ELSE null END) AS 'WA', 
     count(CASE WHEN state_province LIKE 'OR' THEN 1 ELSE null END) AS 'OR', 
     count(CASE WHEN state_province LIKE 'CA' THEN 1 ELSE null END) AS 'CA' 
FROM customer 

看到SQL Fiddle with Demo

+0

我只是試着運行,我仍然得到相同的錯誤。 –

+0

添加註釋並沒有幫助,請參閱[SQL小提琴](http://www.sqlfiddle.com/#!6/cc7b9/14/1)。 –

+0

@OlafDietsche你是對的,修好了 – Taryn

1
SELECT * FROM (Select (SELECT COUNT(state_province) AS 'WA' 
       FROM customer c1 
       WHERE c1.state_province LIKE 'WA' 
       GROUP BY c1.state_province) as t1, 
       (SELECT COUNT(state_province) AS 'OR' 
       FROM customer c1 
       WHERE c1.state_province LIKE 'OR' 
       GROUP BY c1.state_province) as t2, 
       (SELECT COUNT(state_province) AS 'CA' 
       FROM customer c1 
       WHERE c1.state_province LIKE 'CA' 
       GROUP BY c1.state_province) as t3) a ; 
0

剛剛從子選擇離開了括號圍繞你並刪除group by,他們是不必要的

SELECT * FROM (SELECT COUNT(state_province) AS 'WA' 
       FROM customer c1 
       WHERE c1.state_province LIKE 'WA') as t1, 
       (SELECT COUNT(state_province) AS 'OR' 
       FROM customer c1 
       WHERE c1.state_province LIKE 'OR') as t2, 
       (SELECT COUNT(state_province) AS 'CA' 
       FROM customer c1 
       WHERE c1.state_province LIKE 'CA') as t3; 

請參閱SQL Fiddle進行測試。

0

我寧願PIVOT我的數據

SELECT [WA] AS [WA], [OR] AS [OR], [CA] AS [CA] FROM 
(SELECT state_province FROM customer) C 
PIVOT 
(
    COUNT(C.state_province) FOR state_province IN ([WA],[OR],[CA]) 
) AS pivoted 

Fiddle here

0

嗯,首先,感謝所有的建議。你們都是對的,我錯過了別名。另外,事實證明,我必須在聲明前添加「使用foodmart2000」。 愚蠢的我:)

0

@bluefeet和@bummi是非常接近。問題是,你不能使用as關鍵字與表的別名在SQL Server:

SELECT * 
FROM 
(
    (SELECT COUNT(state_province) AS 'WA' 
    FROM customer c1 
    WHERE c1.state_province LIKE 'WA' 
    GROUP BY c1.state_province) t1, 
    (SELECT COUNT(state_province) AS 'OR' 
    FROM customer c1 
    WHERE c1.state_province LIKE 'OR' 
    GROUP BY c1.state_province) t2, 
    (SELECT COUNT(state_province) AS 'CA' 
    FROM customer c1 
    WHERE c1.state_province LIKE 'CA' 
    GROUP BY c1.state_province) t3 
) src; 

你可能會得到來自@ KAF的例子更好的性能,但是。