假设有下表
ID Description Platform
1 aaa C
2 bbb W
3 ccc C
4 ddd W
5 eee W
要求写出sql语句,呈现这样的结果,凡是
platform是C的,打印出CS-(Description)
是W的,打印出WEB-(Description)
结果基本上是这样
CS-aaa
WEB-bbb
CS-ccc
WEB-ddd
WEB-eee
该怎样写呢?谢谢
------解决方案--------------------
- SQL code
select case when Platform='C' then 'CS-'+Description when Platform='W' then 'Web-'+Description endfrom tb
------解决方案--------------------
- SQL code
declare @t table (ID int,Description varchar(3),Platform varchar(1))insert into @tselect 1,'aaa','C' union allselect 2,'bbb','W' union allselect 3,'ccc','C' union allselect 4,'ddd','W' union allselect 5,'eee','W'select ID,C1+Description as Description from @t a left join (select 'CS-' AS C1 UNION SELECT 'WEB-') b on a.Platform=left(b.C1,1)/*ID Description----------- -----------1 CS-aaa2 WEB-bbb3 CS-ccc4 WEB-ddd5 WEB-eee*/