在一行中获取多行的列 [英] Fetching Columns of a multiple rows in one row
本文介绍了在一行中获取多行的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有记录的数据库表,如下所示,
I have a DB table with records as shown below,
ID ATTR_NAME ATTR_VALUE
1 ABC DEF
1 GHI JKL
1 MNO PQR
我想获得一行
ID ABC GHI MNO
1 DEF JKL PQR
推荐答案
它可能有点脆弱,并且没有经过验证的未来,但是Pivot可以为您提供所需的东西:
It may be a little fragile and not that future proofed, but Pivot can give you what you want:
SELECT *
FROM (
SELECT attr_name, attr_value
FROM test
)
PIVOT
( MIN(attr_value)
FOR attr_name IN ( 'ABC','GHI','MNO' )
)
但是,我建议您考虑是否真的需要这种格式,并查看是否可以更自然的格式获取它.
However, I'd advise that you consider if you really need it in that format and see if you can get it out in a more natural format.
这篇关于在一行中获取多行的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文