2

次のクエリを (Foodmart 2000 に対して) 実行しようとしていますが、これは MySQL で正常に動作します。SQL Server 2005 では、"Syntax error near '" メッセージしか表示されません。

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 の静止画のバージョンを使用することもできます。

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を使用SUMCOUNT

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フィドルはこちら

于 2012-12-04T12:29:02.923 に答える
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 に答える
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

ここでフィドル

于 2012-12-04T12:46:05.283 に答える
0

まず、すべての提案に感謝します。エイリアスがありませんでした。また、結果として、ステートメントの前に「foodmart2000 を使用」を追加する必要がありました。愚かな私:)

于 2012-12-04T12:46:12.060 に答える
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 の例からパフォーマンスが向上する可能性があります。

于 2012-12-04T12:46:31.473 に答える
0

from サブセレクトを囲む中かっこを省略して を削除するだけ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 に答える