我有3个字段的表。 我想提取与类型和数量相对应的col_value,并将其作为单独的列类型和数量放入。我怎么做? 以下是表格。
*If
header = "OK" and value= "1"
header = "NOK" and value= "0"
then XML Valid
*If
header = "OK" and value= "1"
header = "NOK" and value= "1"
then XML Valid
*If
header = "OK" and value= "0"
header = "NOK" and value= "1"
then XML Valid
我想从sql查询得到的结果是
ID | col_name | col_value
1 | type | abcd
1 | amount | 1234
2 | type | adcd
2 | amount | 224
答案 0 :(得分:2)
您可以使用条件聚合执行此操作:
SELECT
MAX(CASE WHEN col_name = 'type' THEN col_value END) AS type,
MAX(CASE WHEN col_name = 'amount' THEN col_value END) AS amount
FROM SampleData
GROUP BY ID
您可以添加WHERE
子句以指定要返回的ID
。
正如ughai在下面评论的那样,最好保持GROUP BY
条款来考虑多个ID
。