如何将两列数据转为一列数据

本文讨论如何在SQL中将用户和其所属的组从两列转换为一列。原始数据包含用户和组信息,每个用户只属于一个组。目标是创建一个结果集,首先显示组名,然后按组列出所有用户。问题中提到的系统是DB2 9.5 Enterprise on Linux。解决方案提及使用集算器通过分组和创建排序字段来实现这个转换,这种方法比纯SQL更简洁易懂。
摘要由CSDN通过智能技术生成

【问题】

how to move two column data to one column data

Hi,
I have users that belongs to groups. I need to make both data in one column and specify if this is group or user.

Source data in “mytable” are:

Code:

ColGroup ColUser
Group1 User1
Group1 User2
Group2 User3
Group2 User4
...

Note: list of groups and users is longer than this simple sample. Each user can belong only to one group (ColUser is PK in table). Groups can have multiple users.

Result of SQL should be:

Code:

ColGroupUser UserType
Group1 group
User1 user
User2 user
Group2 group
User3 user
User4 user
...

Note: first is Group1, then there are listed all users that belongs to Group1 in our sample there are User1 and User2. Then must be displayed Group2 and after that all users that belongs to Group2 that are User3 and User4.

Question: how to write such an SQL to move

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值