SELECT 'WITH sched2 AS (
 SELECT name,';
-- (1)↓日付け候補の数だけのCASE文列挙
SELECT group_concat(
         printf('  CASE date WHEN ''%s'' THEN okng END "%s"', date, date),
	 X'2c0a')		-- 「カンマ(0x2c)+改行(0x0a)」で連結する
FROM   datelist;

SELECT ' FROM sched
) SELECT name,';
-- (2)↓maxの列挙
SELECT group_concat(printf('   max("%s") "%s"', date, date), X'2c0a')
FROM   datelist;

SELECT ' FROM sched2
 GROUP BY name

 UNION ALL

 SELECT ''○の数'',';
-- (3)↓count関数の列挙
SELECT group_concat(printf('    count("%s")', date), X'2c0a')
FROM   datelist;
SELECT ' FROM sched2 WHERE ''○'' IN (';
-- (4)↓INに対する値の列挙
SELECT group_concat(printf('"%s"', date)) FROM datelist;

SELECT ');';
