SQL:选择不同的记录,同时忽略一个列。

huangapple go评论61阅读模式
英文:

SQL: Select distinct while ignoring only one column

问题

我有这样的一个表格。输入表格

name  age  amount  xyz
dip    3     12    22a
dip    3     12    23a
oli    4     34    23b
mou    5     56    23b
mou    5     56    23a
maa    7     68    24c

我想要查找不同的行,但在查找不同行时要忽略“xyz”列。
输出应该如下所示。输出表格

  name  age  amount  xyz
dip    3      12     22a
oli    4      34     23b
mou    5      56     23b
maa    7      68     24c
SELECT DISTINCT * EXCEPT (xyz)
FROM table

但它没有起作用。

英文:

I have a table like this. input table

name  age  amount  xyz
dip    3     12    22a
dip    3     12    23a
oli    4     34    23b
mou    5     56    23b
mou    5     56    23a
maa    7     68    24c

I want to find distinct rows but want to ignore the column 'xyz' while finding distinct.
The output should look like this. output table

  name  age  amount  xyz
dip    3      12     22a
oli    4      34     23b
mou    5      56     23b
maa    7      68     24c

SELECT
DISTINCT * EXCEPT (xyz)
FROM table

but it didn't work.

答案1

得分: 1

翻译好的部分如下:

few more options     

    select any_value(t).*
    from your_table t
    group by t.name, t.age, t.amount        

with output     

[![enter image description here][1]][1]

if you need MIN or MAX  - you can use below 

    select any_value(t having min xyz).*
    from your_table t
    group by t.name, t.age, t.amount   

or    

    select * from your_table
    qualify xyz = min(xyz) over(partition by name, age, amount)


with output       

[![enter image description here][2]][2]

希望这个翻译对您有所帮助。如果您有任何其他问题,请随时提问。

英文:

few more options

select any_value(t).*
from your_table t
group by t.name, t.age, t.amount        

with output

SQL:选择不同的记录,同时忽略一个列。

if you need MIN or MAX - you can use below

select any_value(t having min xyz).*
from your_table t
group by t.name, t.age, t.amount   

or

select * from your_table
qualify xyz = min(xyz) over(partition by name, age, amount)

with output

SQL:选择不同的记录,同时忽略一个列。

答案2

得分: 0

你需要使用 GROUP BY,请尝试以下代码,看看是否有效:

SELECT name, age, amount, MIN(xyz) AS xyz
FROM table
GROUP BY name, age, amount
英文:

You need to use group by, try the below and see if it works:

SELECT name, age, amount, MIN(xyz) AS xyz
FROM table
GROUP BY name, age, amount

huangapple
  • 本文由 发表于 2023年2月26日 20:50:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定