日期:2014-05-17 浏览次数:20559 次
declare @sql varchar(8000)
select @sql = isnull(@sql + '],[' , '') + 银行名 from (SELECT '总额' AS '类型',总额 AS mon,银行名 FROM 银行表
UNION ALL
SELECT 类型,贷款金额 AS mon,贷款银行 AS 银行名 FROM 贷款记录表)T group by 银行名
set @sql = '[' + @sql + ']'
exec ('select * from (select * from (SELECT ''总额'' AS ''类型'',总额 AS mon,银行名 FROM 银行表
UNION ALL
SELECT 类型,贷款金额 AS mon,贷款银行 AS 银行名 FROM 贷款记录表)T
) a pivot (max(mon) for 银行名 in (' + @sql + ')) b')
/*
固贷 NULL 50 NULL
流贷 NULL NULL 50
其它 NULL NULL 100
总额 800 1000 1200*/