我有一个基于这个主题的问题的后续问题:主查询
列中的SQL查询子表我已经设法得到了下面的表格,上面的主题回答了查询:
<表类>uuid 代码 title-en title-de tbody><<tr>111 -等 123 英语123 多伊奇123 222 -等 321 英语321 多伊奇321 表类>
可以在CROSS APPLY
内部进行pivot。
对于双列枢轴,通常使用条件聚合更容易MAX(CASE WHEN
select
t.uuid,
t.code,
[title-en] = len.title,
[title-de] = lde.title,
cond.*
from table_1 t
left join table_1_lang len on t.uuid = len.uuid and len.lang_code = 'en'
left join table_1_lang lde on t.uuid = lde.uuid and lde.lang_code = 'de'
CROSS APPLY (
SELECT
[condition-0-list_code] = MAX(CASE WHEN c.[order] = 0 THEN c.list_code END),
[condition-0-value] = MAX(CASE WHEN c.[order] = 0 THEN c.value END),
[condition-1-list_code] = MAX(CASE WHEN c.[order] = 1 THEN c.list_code END),
[condition-1-value] = MAX(CASE WHEN c.[order] = 1 THEN c.value END),
[condition-2-list_code] = MAX(CASE WHEN c.[order] = 2 THEN c.list_code END),
[condition-2-value] = MAX(CASE WHEN c.[order] = 3 THEN c.value END)
FROM table_2 c
WHERE c.uuid = t.uuid
) cond;
对于table_1_lang