Convert columns to rows in SQL(在 SQL 中将列转换为行)
本文介绍了在 SQL 中将列转换为行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要编写一个查询,它接受行并将其转换为列 - 这是我的表:
I need to write a query which takes rows and converts it into columns - here's my table:
Count fname lname id
-----------------------------
1 abc def 20
2 pqr 20
3 abc xyz 20
4 xyz xyz 20
1 abc def 21
1 pqr xyz 22
2 abc abc 22
这是我试图产生的输出:
This is the output I'm trying to produce:
id fname lname fname lname fname lname fname lname
-------------------------------------------------------------
20 abc def pqr NULL abc xyz xyz xyz
21 abc def NULL NULL NULL NULL NULL NULL
22 abc abc NULL NULL NULL NULL NULL NULL
每个 id 的 count 最大值为 4.我使用的是 Oracle 9i.
The max value of count for each id is 4. I'm using Oracle 9i.
推荐答案
这是另一个你可能会遇到的问题.我喜欢@ThinkJet,但不确定解码成本是多少(如果多于或少于以下.
Here's another one you might have some luck with. I like @ThinkJet's but not sure how much decode costs (if more or less than this below.
SELECT
T1.ID,
T1.fname,
T1.lname,
T2.fname,
T2.lname,
T3.fname,
T3.lname,
T4.fname,
T4.lname
FROM
table T1
LEFT JOIN
table T2
ON
T1.ID = T2.ID
AND T2.count = 2
LEFT JOIN
table T3
ON
T1.ID = T3.ID
AND T3.count = 3
LEFT JOIN
table T4
ON
T1.ID = T4.ID
AND T4.count = 4
WHERE
T1.count = 1
这篇关于在 SQL 中将列转换为行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:在 SQL 中将列转换为行


基础教程推荐
猜你喜欢
- oracle区分大小写的原因? 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 在多列上分布任意行 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01