sql 选择表的多列

发布于 2024-12-24 21:45:55 字数 353 浏览 3 评论 0原文

我正在尝试从 Oracle DB 中的表中进行选择。

如果我在单列上的 select 语句中执行不同操作,则效果很好,但对于多列,这似乎不起作用。

例如。

select DISTINCT col1, col2, col3, col4, col5, col6 from table1

返回相同的结果。

select * from table1

第 2 列、第 3 列有重复值。我想要的是上面所有列值的值,但不是column2和column3的重复值。

你能帮忙吗?我们将不胜感激您的帮助。

谢谢

I am trying to a select from a table in oracle DB.

If I do distinct in select statement on single column it works fine but for multiple columns this doesn't seem to work.

for example.

select DISTINCT col1, col2, col3, col4, col5, col6 from table1

returns the same result as.

select * from table1

Column 2, Column3 has duplicate values. What I want is values of all the above column values but not the duplicate values of column2 and column3.

Can you please help. Your help will be appreciated.

Thanks

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

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

发布评论

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

评论(2

骄兵必败 2024-12-31 21:45:55

DISTINCT 消除了所选每行和列的冗余数据。因此,如果您使用 DISTINCT 选择表的所有列,则只有列包含完全相同数据的行才会被聚合。

DISTINCT eliminates redundant data for each row and the columns selected. So if you select all columns of a table using DISTINCT, only rows which columns contain the exact same data are aggregated.

べ映画 2024-12-31 21:45:55

尝试以下操作:

SELECT DISTINCT COL1 AS COL1, NULL AS COL2, NULL AS COL3, NULL AS COL4,
       NULL AS COL5, NULL AS COL6 FROM TABLE1
UNION ALL
SELECT NULL AS COL1, DCOL2 AS COL2, NULL AS COL3, NULL AS COL4,
       NULL AS COL5, NULL AS COL6 FROM (SELECT DISTINCT COL2 AS DCOL2
                                          FROM TABLE1)
UNION ALL
SELECT NULL AS COL1, NULL AS COL2, DCOL3 AS COL3, NULL AS COL4,
       NULL AS COL5, NULL AS COL6 FROM (SELECT DISTINCT COL3 AS DCOL3
                                          FROM TABLE1)
UNION ALL
SELECT NULL AS COL1, NULL AS COL2, NULL AS COL3, DCOL4 AS COL4,
       NULL AS COL5, NULL AS COL6 FROM (SELECT DISTINCT COL4 AS DCOL4
                                          FROM TABLE1)
UNION ALL
SELECT NULL AS COL1, NULL AS COL2, NULL AS COL3, NULL AS COL4,
       DCOL5 AS COL5, NULL AS COL6 FROM (SELECT DISTINCT COL5 AS DCOL5
                                           FROM TABLE1)
UNION ALL
SELECT NULL AS COL1, NULL AS COL2, NULL AS COL3, NULL AS COL4,
       NULL AS COL5, DCOL6 AS COL6 FROM (SELECT DISTINCT COL6 AS DCOL6
                                           FROM TABLE1);

分享并享受。

Try the following:

SELECT DISTINCT COL1 AS COL1, NULL AS COL2, NULL AS COL3, NULL AS COL4,
       NULL AS COL5, NULL AS COL6 FROM TABLE1
UNION ALL
SELECT NULL AS COL1, DCOL2 AS COL2, NULL AS COL3, NULL AS COL4,
       NULL AS COL5, NULL AS COL6 FROM (SELECT DISTINCT COL2 AS DCOL2
                                          FROM TABLE1)
UNION ALL
SELECT NULL AS COL1, NULL AS COL2, DCOL3 AS COL3, NULL AS COL4,
       NULL AS COL5, NULL AS COL6 FROM (SELECT DISTINCT COL3 AS DCOL3
                                          FROM TABLE1)
UNION ALL
SELECT NULL AS COL1, NULL AS COL2, NULL AS COL3, DCOL4 AS COL4,
       NULL AS COL5, NULL AS COL6 FROM (SELECT DISTINCT COL4 AS DCOL4
                                          FROM TABLE1)
UNION ALL
SELECT NULL AS COL1, NULL AS COL2, NULL AS COL3, NULL AS COL4,
       DCOL5 AS COL5, NULL AS COL6 FROM (SELECT DISTINCT COL5 AS DCOL5
                                           FROM TABLE1)
UNION ALL
SELECT NULL AS COL1, NULL AS COL2, NULL AS COL3, NULL AS COL4,
       NULL AS COL5, DCOL6 AS COL6 FROM (SELECT DISTINCT COL6 AS DCOL6
                                           FROM TABLE1);

Share and enjoy.

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