使用 Oracle 转置选择结果

发布于 2024-08-11 20:46:01 字数 505 浏览 4 评论 0 原文

我的问题是,有一些背景:

我必须根据表元数据(列格式)生成一些sql查询,结果类似于:(

TABLENAME1|COL1
TABLENAME1|COL2
TABLENAME2|COL1
TABLENAME2|COL2
TABLENAME2|COL3
TABLENAME3|COL1
TABLENAME4|COL1
TABLENAME4|COL2
... /*some other 1800 rows */

是的,它是有序的。) 我需要的是根据第一列转置这些数据,因此预期输出将是:

TABLENAME1|COL1|COL2|NULL
TABLENAME2|COL1|COL2|COL3
TABLENAME3|COL1|NULL|NULL
TABLENAME4|COL1|COL2|NULL
/* less then 1800 rows ;-) */

Is it possible using Oracle SQL?

提前致谢!

my question is, with some background:

I have to generate some sql queries based on the table metadata (column format), and the result is something like:

TABLENAME1|COL1
TABLENAME1|COL2
TABLENAME2|COL1
TABLENAME2|COL2
TABLENAME2|COL3
TABLENAME3|COL1
TABLENAME4|COL1
TABLENAME4|COL2
... /*some other 1800 rows */

(Yeah, it's ordered.)
What I need is to transpose this data, based on the first column, so the expected output would be:

TABLENAME1|COL1|COL2|NULL
TABLENAME2|COL1|COL2|COL3
TABLENAME3|COL1|NULL|NULL
TABLENAME4|COL1|COL2|NULL
/* less then 1800 rows ;-) */

Is it possible using Oracle SQL?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空名 2024-08-18 20:46:01

如果您想为每个调用生成查询或使用硬编码的最大列计数,那么您可以执行类似的操作:

WITH tab AS
(
  SELECT table_name, column_name FROM user_tab_cols WHERE column_id <= 4
) -- user_tab_cols used to provide test data, use your table instead
SELECT MAX(c1) c1,
       MAX(c2) c2,
       MAX(c3) c3,
       MAX(c4) c4
  FROM (SELECT table_name,
               DECODE( column_id, 1, column_name ) c1,
               DECODE( column_id, 2, column_name ) c2,
               DECODE( column_id, 3, column_name ) c3,
               DECODE( column_id, 4, column_name ) c4
          FROM ( SELECT table_name,
                        column_name,
                        ROW_NUMBER() OVER ( PARTITION BY table_name ORDER BY column_name ) column_id
                   FROM tab
               )
       )
 GROUP BY table_name
 ORDER BY table_name

如果以这种形式获取它就足够了,

TABLENAME1|COL1,COL2
TABLENAME2|COL1,COL2,COL3

请查看 Tom Kyte 的 stragg

If you want to generate the query for each call or use a hardcoded max-column-count, then you can do something like that:

WITH tab AS
(
  SELECT table_name, column_name FROM user_tab_cols WHERE column_id <= 4
) -- user_tab_cols used to provide test data, use your table instead
SELECT MAX(c1) c1,
       MAX(c2) c2,
       MAX(c3) c3,
       MAX(c4) c4
  FROM (SELECT table_name,
               DECODE( column_id, 1, column_name ) c1,
               DECODE( column_id, 2, column_name ) c2,
               DECODE( column_id, 3, column_name ) c3,
               DECODE( column_id, 4, column_name ) c4
          FROM ( SELECT table_name,
                        column_name,
                        ROW_NUMBER() OVER ( PARTITION BY table_name ORDER BY column_name ) column_id
                   FROM tab
               )
       )
 GROUP BY table_name
 ORDER BY table_name

If it is sufficient to get it in that form

TABLENAME1|COL1,COL2
TABLENAME2|COL1,COL2,COL3

have a look at Tom Kyte's stragg.

安静被遗忘 2024-08-18 20:46:01

您要查找的关键字是pivot这是其使用示例。其语法通常与 MSSQL 相同,并且由于我不知道 Oracle 文档的 URL,因此我将放弃 MSDN 链接,希望比我更了解 Oracle 页面布局的人能够编辑它以指向他们的文档。

编辑:Oracle 文档,搜索旋转足够多次,您就会了解详细信息。

The keyword that you're looking for is pivot. Here's an example of its use. The syntax appears generally the same as MSSQL, and since I don't know the url for Oracle's documentation I'll toss up the MSDN link and hope that someone that knows more about Oracle's page layout than me will edit it to point to their documentation.

EDIT: Oracle documentation, search for pivot enough times and you'll get to the details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文