我需要展平一个表,但这里是棘手的部分,clomuns 是动态的,当添加包含新 ID 的新记录时,查询应该可以工作。
这是我的工作查询(简化了 IN 这实际上是数百个值):
SELECT *
FROM (select qv.respnr, sq.question_id, qv.question_value
from survey_question sq, question_values qv
where qv.question_id = sq.question_id
and sq.survey_id = 1
order by qv.respnr, page, ranked)
PIVOT (
MAX(question_value) --<-- pivot_clause
FOR question_id --<-- pivot_for_clause
IN (346 as c346,347 as c347)
)
我想IN (346 as c346,347 as c347)
用这样的东西替换:
SELECT mq.question_id
FROM meta_question mq, survey_question sq2
WHERE sq2.survey_id = 1
AND mq.question_id = sq2.question_id
ORDER BY page, ranked
知道怎么做吗?
我注意到 IN 不能简单地使用 select 语句进行 fileld,所以这不起作用:
IN (SELECT mq.question_id
FROM meta_question mq, survey_question sq2
WHERE sq2.survey_id = 1
AND mq.question_id = sq2.question_id
ORDER BY page, ranked)