gpt4 book ai didi

c# - 使用 pivot 将行转换为列

转载 作者:太空宇宙 更新时间:2023-11-03 11:58:11 25 4
gpt4 key购买 nike

我的目标是将这个表的行变成列。

这是我的 table :

 select  tipoDocumento_id,nome,resultado
from Campos

tipoDocumento_id nome resultado
---------------- ------------------------------ ------------------------------
62 Data: 2019-05-20
62 N.I.F. fornecedor: 501062327
62 Total: 31,14
62 Número fatura: FT19/04006
63 Data: 2019-05-23
63 N.I.F. fornecedor: 501062327
63 Total: 39,14
63 Número fatura: FT19/04007

现在我有这个查询,但它返回 NULL

select Data,NIF , Total, Numero fatura
from
(
select tipoDocumento_id,nome,resultado
from Campos
) d
pivot
(
max(tipoDocumento_id)
for resultado in (Data,NIF , Total, Numero fatura)
) piv;

可能是我的问题在于使用 max() 进行聚合

我想要这样的结果

ID    Data:          N.I.F. fornecedor:    Total:      Número fatura:
62 2019-05-20 501062327 31,14 FT19/04006
63 2019-05-23 501062327 39,14 FT19/04007

有人能帮我一点忙吗?

最佳答案

你正围着它转

select *
from
(
select id=tipoDocumento_id
,nome
,resultado
from Campos
) d
pivot
(
max(resultado)
for nome in ([Data:],[N.I.F. fornecedor:],[Total:],[Número fatura:])
) piv;

关于c# - 使用 pivot 将行转换为列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58546958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com