SQL SERVER中纵向的数据横向显示出来
selectc.ContractCodeas合同编号,d.ProjNameas项目名称,b.UsedAmountas使用金额fromcb_BudgetUsealeftjo...
select
c.ContractCode as 合同编号 ,
d.ProjName as 项目名称,
b.UsedAmount as 使用金额
from cb_BudgetUse a
left join cb_Budget b on b.BudgetGUID=a.BudgetGUID
left join cb_Contract c on c.ContractGUID=a.ContractGUID
left join p_Project d on d.ProjGUID=b.ProjectGUID
where c.projtype ='多项目' and CfTypeName = '签约金额' and c.BUGUID='10B22C6E-3D69-E311-805B-6EAE8B3B49A1'
ORDER BY CostCode
想要实现横向显示项目名称和对应的使用金额 展开
c.ContractCode as 合同编号 ,
d.ProjName as 项目名称,
b.UsedAmount as 使用金额
from cb_BudgetUse a
left join cb_Budget b on b.BudgetGUID=a.BudgetGUID
left join cb_Contract c on c.ContractGUID=a.ContractGUID
left join p_Project d on d.ProjGUID=b.ProjectGUID
where c.projtype ='多项目' and CfTypeName = '签约金额' and c.BUGUID='10B22C6E-3D69-E311-805B-6EAE8B3B49A1'
ORDER BY CostCode
想要实现横向显示项目名称和对应的使用金额 展开
展开全部
使用case when 或是 pivot进行处理。
下面是示例,具体的值你根据实际改一下。
select
c.ContractCode as 合同编号 ,
sum(case when d.ProjName = '海峡一期' then b.UsedAmount else 0 end) as [海峡一期],
sum(case when d.ProjName = '海峡二期' then b.UsedAmount else 0 end) as [海峡二期],
sum(case when d.ProjName = '海峡三期' then b.UsedAmount else 0 end) as [海峡三期],
from cb_BudgetUse a
left join cb_Budget b on b.BudgetGUID=a.BudgetGUID
left join cb_Contract c on c.ContractGUID=a.ContractGUID
left join p_Project d on d.ProjGUID=b.ProjectGUID
where c.projtype ='多项目' and CfTypeName = '签约金额' and c.BUGUID='10B22C6E-3D69-E311-805B-6EAE8B3B49A1'
ORDER BY CostCode
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询