gpt4 book ai didi

postgresql - 在 PostgreSQL 中抑制重复的列值?

转载 作者:行者123 更新时间:2023-11-29 11:41:34 25 4
gpt4 key购买 nike

例如,如果我们有以下值:

username             Category
-------------------- ----------
Brian cs
Tom cs
Joe cs
Allen cs
Bill ts
Steven ts
Fred ts
Ted ts

我需要一个输出作为

username             Category
-------------------- ----------
Brian cs
Tom
Joe
Allen
Bill ts
Steven
Fred
Ted

我试过

1) T-SQL How to select rows without duplicate values from one column?

2) http://www.rajapet.com/2010/04/suppressing-repeated-column-value-in.html

3) How to select records without duplicate on just one field in SQL?

没有什么效果很好(我所期望的)

还有其他办法吗?

最佳答案

您可以使用 window function为此:

select username, 
case when rn = 1 then cat else '' end as category
from (
select username,
category as cat,
row_number() over (partition by category order by username) as rn
from the_table
) t
order by cat, username;

SQLFiddle:http://sqlfiddle.com/#!15/423ed/4

关于postgresql - 在 PostgreSQL 中抑制重复的列值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28108682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com