sql查询结果横向显示
FL表中 字段名称:A(台号),B(名称),C(金额),D(排序),E(日期)
我使用的横向查询显示语句:
declare @sql varchar(8000)
set @sql = 'select A AS 台号'
select @sql = @sql + ' , max(case B when ''' + B + ''' then C else 0 end) [' + B + ']'
from (select distinct B,D from fl ) as a order by D
set @sql = @sql + ' from fl where 日期='+quotename('2012-5-7','''')+' group by A' exec(@SQL)
问题:C字段中的金额如果是负数就被忽略不统计了 ,如:c字段中显示“-0.4” 查询后”-0.4“被忽略不统计了
谢谢 帮我看下
------解决方案--------------------用这个吧,我减少了很多空格
DECLARE @sql VARCHAR(8000)
SET @sql = 'select A 台号'
SELECT @sql = @sql + ',max(case B when ''' + LTRIM(RTRIM(B))
+ ''' then C else null end) ['+LTRIM(RTRIM(B))+']'
FROM ( SELECT DISTINCT
B ,
D
FROM fl
) AS a
ORDER BY D
SET @sql = @sql + ' from fl where 日期=' + QUOTENAME('2012-5-7', '''')
+ ' group by A'
print(@SQL)