样本数据:
表:PollingSettings
示例行:
ID | Frequency | Value
---+----------------+-------
1 | Frequency | 300
1 | Timeout | 500
2 | Frequency | 1200
2 | Timeout | 100
3 | LogFilesToKeep | 30
3 | DebugEnabled | False
我需要一条select语句,该语句将执行以下操作:
SELECT ID, key as Frequency, key as Timeout
CASE Freqency
WHEN Frequency = 'Frequency' THEN value
CASE Timeout
WHEN Timeout = 'Timeout' THEN value
END
FROM PollingSettings
基本上,我需要创建两个名为timeout和frequency的新列,但前提是ID具有一个键值为“ frequency”或“ timeout”的键列,并将该列称为“ frequency”或“ timeout”与键值对中的值。否则,仍应创建这些列,而应将它们设置为“ null”。最终结果是这样的:
ID | Frequency | Timeout
---+-------------+-------
1 | 300 | 500
2 | 1200 | 100
答案 0 :(得分:1)
您只需要创建两个包含case语句的列并获取最大值,这样每个ID仅获得1行:
select ID,
max(case when key='Frequency' then value end) as Frequency,
max(case when key='Timeout' then value end) as Timeout
from PollingSettings
group by ID
答案 1 :(得分:0)
请与CASE WHEN一起尝试:
SELECT ID, min(CASE WHEN key = 'Frequency' THEN value end) as Frequency,
min(CASE WHEN key = 'Timeout' THEN value end) as Timeout
FROM PollingSettings group by ID