我有一张客户表,他的联系方式如下:
adresse
adresseid, name, etc..
kommunication
adresseid, komm_artid, komm_text
1, telefon, +123456
1, handy, 12346
1, email, any@example.com
我想在一行中获取这些数据。
但是,我不能使用 LIST,有问题。
Firebird 中是否有任何方法可以获得如下结果:
地址,field_1...field_N
无需手动复制粘贴未知数量的字段..
只读基础。
在 MSSQL 中有一个类比:
DECLARE @colsUnpivot AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@colsPivot as NVARCHAR(MAX)
select @colsUnpivot = stuff((select ','+quotename(C.name)
from sys.columns as C
where C.object_id = object_id('yourtable') and
C.name <> 'color'
for xml path('')), 1, 1, '')
select @colsPivot = STUFF((SELECT ','
+ quotename(color)
from yourtable t
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query
= 'select name, '+@colsPivot+'
from
(
select color, name, value
from yourtable
unpivot
(
value for name in ('+@colsUnpivot+')
) unpiv
) src
pivot
(
sum(value)
for color in ('+@colsPivot+')
) piv'
exec(@query)