日期:2014-05-19  浏览次数:20403 次

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

這樣?

另外,列名不能重複