2

我正在尝试运行以下查询(针对 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); 

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

4

7 回答 7

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通过以下方式使用 @Kaf's 的一个版本:

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

于 2012-12-04T12:21:02.813 回答
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 ;
于 2012-12-04T12:26:56.050 回答
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

于 2012-12-04T12:29:02.923 回答
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进行测试。

于 2012-12-04T12:41:39.223 回答
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

于 2012-12-04T12:46:05.283 回答
0

Well, first of all, thanks for all the suggestions. All of you were right, I was missing an alias. Also, as it turns out, I had to add a "use foodmart2000" before the statement. Stupid me :)

于 2012-12-04T12:46:12.060 回答
0

@bluefeet and @bummi are extremely close. The issue is that you can't use the as keyword with table aliases in 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;

You will probably get better performance from @kaf's example, however.

于 2012-12-04T12:46:31.473 回答