SQL语句中列名的表示
如果想表示成差占%,中占%,良占%,优占%,以下SQL语句要怎么修改一下:
SELECT apart_name As 楼栋名称,percent_bad As 差,percent_mid As 中,percent_good As 良,percent_best As 优 FROM percents
------解决方案----------------------try
SELECT apart_name As 楼栋名称,[差占%]=ltrim((cast(percent_bad as decimal(18,6)))/(percent_bad+percent_mid+percent_good+percent_best)*100)+ '% ',
[中占%]=ltrim((cast(percent_mid as decimal(18,6)))/(percent_bad+percent_mid+percent_good+percent_best)*100)+ '% ',
[良占%]=ltrim((cast(percent_good as decimal(18,6)))/(percent_bad+percent_mid+percent_good+percent_best)*100)+ '% ',
[优占%]=ltrim((cast(percent_best as decimal(18,6)))/(percent_bad+percent_mid+percent_good+percent_best)*100)+ '% '
FROM percents
------解决方案--------------------SELECT '楼栋名称 ', '差% ', '中% ', '良% ', '优% '
union all
SELECT apart_name As 楼栋名称,cast (percent_bad as varchar(4000)),cast(percent_mid as varchar(4000)) ,cast (percent_good as varchar(4000)),cast(percent_best as varchar(4000)) FROM percents
测试效果..楼主将就一下吧。
SELECT '楼栋名称 ', '差% ', '中% ', '良% ', '优% '
union all
select name, '65.45 ', '55.32 ', '66.36 ', '55.35 ' from tb
---------- ---- ---- ---- ----
楼栋名称 差% 中% 良% 优%
张三 65.45 55.32 66.36 55.35
李四 65.45 55.32 66.36 55.35
张三 65.45 55.32 66.36 55.35
嘿嘿 65.45 55.32 66.36 55.35
哈哈 65.45 55.32 66.36 55.35
呵呵 65.45 55.32 66.36 55.35
(所影响的行数为 7 行)