1

カウントも取得できますか?私のUNIONは、4つの列すべてですべての個別の値を取得しますが、4つの列すべてで各値が何回表示されるかを知る必要があります。可能であれば、ストックSQLを使用する必要があります。

ここに画像の説明を入力してください

(SELECT DISTINCT classify1 AS classified FROM class) UNION
(SELECT DISTINCT classify2 AS classified FROM class) UNION
(SELECT DISTINCT classify3 AS classified FROM class) UNION
(SELECT DISTINCT classify4 AS classified FROM class)
ORDER BY classified

戻り値:

A
B
C
D
E
F
H

必要:

A | 3
B | 3
C | 4
D | 3
E | 1
F | 1
H | 1

4

2 に答える 2

4

SQLフィドル

SELECT a.classified, COUNT(*)
FROM
(
  (SELECT classify1 AS classified FROM class) UNION ALL
  (SELECT classify2 AS classified FROM class) UNION ALL
  (SELECT classify3 AS classified FROM class) UNION ALL
  (SELECT classify4 AS classified FROM class)) a
GROUP BY a.classified

結果

| クラシファイド| COLUMN_1 |
-------------------------
| A | 3 |
| B | 3 |
| C | 4 |
| D | 3 |
| E | 1 |
| F | 1 |
| H | 1 |

使用するときはDISTINCT、余分な「A」を削除しますclassify3

于 2012-08-17T14:43:13.637 に答える
0

の代わりUNION ALLUNION、集計を実行するサブクエリに結果を埋め込みます。

SELECT
  classified,
  COUNT(*)
FROM
(
  (SELECT classify1 AS classified FROM class) UNION ALL
  (SELECT classify2 AS classified FROM class) UNION ALL
  (SELECT classify3 AS classified FROM class) UNION ALL
  (SELECT classify4 AS classified FROM class)
)
  AS unified_data
GROUP BY
  classified
ORDER BY
  classified 
于 2012-08-17T14:36:36.097 に答える