在 Firebird 中将列转换为行(unpivot 命令) [英] Convert columns to rows in Firebird (unpivot command)

查看:33
本文介绍了在 Firebird 中将列转换为行(unpivot 命令)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebird 中 unpivot 的类似命令是什么?提前TKS...

Which is the similar command for unpivot in Firebird? TKS in advance...

我的桌子

 Id  |  Name    |  Land  |   Water
-----|----------|--------|---------
 1   |  John    |  300m  |    100m
-----|----------|--------|---------
 2   |  Mark    |  100m  |     0m
-----------------------------------

想要的结果

 Id   |  Name  |   Category | Surface
 -----|--------|------------|--------
 1    |  John  |    Land    |   300m
 -----|--------|------------|--------
 1    |  John  |    Water   |   100m
 -----|--------|------------|--------
 2    |  Mark  |    Land    |   100m
 -----|--------|------------|--------
 2    |  Mark  |    Water   |    0m

推荐答案

可以使用union all:

select id, col1 as col
from t
union all
select id, col2 as col
from t;

像这样的东西应该适用于大多数目的.

Something like this should work for most purposes.

对于您的特定数据:

select id, name, 'Land' as category, land as surface
from mytable
union all
select id, name, 'Water' as category, water as surface
from mytable;

这篇关于在 Firebird 中将列转换为行(unpivot 命令)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆