首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql 语句,该如何处理

2012-02-20 
sql 语句表TABC1332473610SELECTA,BFROMTWHERECBETWEEN1TO8输出AB1324SELECTA,BFROMTWHERECBETWEEN4TO12输

sql 语句
表T
  A           B               C
  1           3               3
  2           4               7
  3           6               10
SELECT   A,B   FROM   T   WHERE   C   BETWEEN   1   TO   8
输出
  A         B
  1         3
  2         4
SELECT   A,B   FROM   T   WHERE   C   BETWEEN   4   TO   12
输出
  A         B
  2         4
  3         6
有没有办法使的用一个SQL语句使的输出为
输出
  A         B         A         B
  1         3         2         4
  2         4         3         6


[解决办法]
SELECT (case when C BETWEEN 1 TO 8 then A end ) as A,(case when C BETWEEN 1 TO 8 then B end ),(case when C BETWEEN 4 TO 12 then A end ) as A,(case when C BETWEEN 4 TO 12 then B end ) as B FROM T
[解决办法]
select A1 = max(case when C between 1 and 8 then A else null end),
B1 = max(case when C between 1 and 8 then B else null end),
A2 = max(case when C between 4 and 12 then A else null end),
B2 = max(case when C between 4 and 12 then B else null end)
from T

這樣?

另外,列名不能重複

热点排行