2

我有这样的表:

Code    label    Zone   Price
----    -----    ----   -----
049     able      A       5
100     hui       A       6
049     ddf       B       10
100     dsqf      B       34

我怎样才能像这样转换它:

Code       A         B
----      ---      ----
049        5         10
100        6         34 
4

3 回答 3

3
select Code, A, B
from
  (
    select Code, Zone, Price 
    from YourTable
  ) T
pivot
  (
    min(Price) for Zone in (A, B)
  ) P
于 2012-04-24T14:28:32.413 回答
1

像这样的东西应该可以工作(假设一个名为't'的表)

  select codeA, A, B from 
   (select code as codeA, price as A from t where zone='A'), 
   (select code as codeB, price as B from t where zone='B') 
   where codeA=codeB
于 2012-04-24T14:21:19.703 回答
0

抱歉,我没有对此进行测试,但是您应该可以执行以下操作:

SELECT CODE, 
SUM(CASE ZONE WHEN A THEN PRICE ELSE NULL END) AS A,
SUM(CASE ZONE WHEN B THEN PRICE ELSE NULL END) AS B
GROUP BY CODE
于 2012-04-24T14:21:04.670 回答