将MySQL转换为SQL Server

时间:2012-12-04 12:12:03

标签: mysql sql-server

我正在尝试运行以下查询(针对Foodmart 2000),它在MySQL上运行正常。在SQL Server 2005上,我得到的是“语法错误附近”消息。

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); 

究竟是什么导致了这个问题?

7 个答案:

答案 0 :(得分: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

必须在所有派生表和子查询上都有别名。

您还可以使用以下内容{@ 1}}使用@ Kaf的版本:

count

请参阅SQL Fiddle with Demo

答案 1 :(得分: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 ;

答案 2 :(得分:1)

试试这个;使用SELECT代替SUM

的单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

答案 3 :(得分: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进行测试。

答案 4 :(得分: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

答案 5 :(得分:0)

嗯,首先,感谢所有的建议。你们所有人都是对的,我错过了一个别名。此外,事实证明,我必须在声明之前添加“使用foodmart2000”。 愚蠢的我:)。

答案 6 :(得分:0)

@bluefeet和@bummi非常接近。问题是您不能在SQL Server中使用as关键字和表别名:

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的例子中获得更好的表现。