SQL - 几列到一列

时间:2017-03-08 23:18:00

标签: psql pervasive

是否可以在sql中执行查询,将多个列转换为仅一个?

示例,转换当前数据库结构:

**product_ID | month_A | month_B | month_C**
AAAAA      |      15 |      18 |      16
BBBBB      |      20 |      21 |      26
CCCCC      |      40 |      48 |      41

我想改变,所以我可以更好地在Excel中使用数据透视表:

**product_ID | sales_qt| month**
AAAAA      |      15 |     A
AAAAA      |      18 |     B
AAAAA      |      16 |     C
BBBBB      |      20 |     A
BBBBB      |      21 |     B
BBBBB      |      26 |     C
CCCCC      |      40 |     A
CCCCC      |      48 |     B
CCCCC      |      41 |     C

最好的问候!!!

1 个答案:

答案 0 :(得分:0)

我对Pervasive并不熟悉。但标准的SQL方法是:

select product_ID, month_A as sales_qt, 'A' as month
from t
union all
select product_ID, month_B as sales_qt, 'B' as month
from t
union all
select product_ID, month_C as sales_qt, 'C' as month
from t;