|
▼Mr.Child さん:
半ば強引に(!?)クエリーにまとめて見ました^^;;
SELECT UQ_2.id, data AS 結果, UQ_2.回数
FROM (
SELECT 顧客id AS id, [1回目] AS data, "1回目" AS 回数
FROM テーブルA WHERE ([1回目] Is Not Null)
union
SELECT 顧客id AS id, [2回目] AS data, "2回目" AS 回数
FROM テーブルA WHERE ([2回目] Is Not Null)
union
SELECT 顧客id AS id, [3回目] AS data, "3回目" AS 回数
FROM テーブルA WHERE ([3回目] Is Not Null)
union
SELECT 顧客id AS id, [4回目] AS data, "4回目" AS 回数
FROM テーブルA WHERE ([4回目] Is Not Null)
union
SELECT 顧客id AS id, [5回目] AS data, "5回目" AS 回数
FROM テーブルA WHERE ([5回目] Is Not Null)
) AS UQ_2
INNER JOIN (
SELECT id, MAX(回数) AS 回
FROM (
SELECT 顧客id AS id, [1回目] AS data, "1回目" AS 回数
FROM テーブルA WHERE ([1回目] Is Not Null)
union
SELECT 顧客id AS id, [2回目] AS data, "2回目" AS 回数
FROM テーブルA WHERE ([2回目] Is Not Null)
union
SELECT 顧客id AS id, [3回目] AS data, "3回目" AS 回数
FROM テーブルA WHERE ([3回目] Is Not Null)
union
SELECT 顧客id AS id, [4回目] AS data, "4回目" AS 回数
FROM テーブルA WHERE ([4回目] Is Not Null)
UNION
SELECT 顧客id AS id, [5回目] AS data, "5回目" AS 回数
FROM テーブルA WHERE ([5回目] Is Not Null)
) AS UQ
GROUP BY id
) AS SQ ON
UQ_2.id=SQ.id And UQ_2.回数=SQ.回
見づらくなってすみません^^;
UQ、UQ_2、SQ をそれぞれ別クエリにすると
少しは見やすくなるかも!?
|
|