我的源表看起来像这样
cust_id cust_name
1 John Smith
1 John K Smith
2 Mary B Snow
2 Mary Snow
我想返回
cust_id cust_name
1 John Smith
2 Mary B Snow
其中一个cust_name值不优先于另一个;我只想要每个cust_id一行,并附加一个任意的cust_name。
我正在使用
select cust_id, cust_name from customers
qualify 1 = row_number() over (partition by cust_id order by cust_name desc)
但由于order by
子句的原因,我无法在子查询中使用它。
有没有办法可以在没有(概念上不必要的)排序的情况下做到这一点?
答案 0 :(得分:1)
您必须在SELECT NULL
子句中使用ORDER BY
。因此它不会改变顺序,Row_Number将按预期工作
试试这个
SELECT cust_id, cust_name FROM
(
SELECT cust_id, cust_name ,ROW_NUMBER() OVER (PARTITION BY cust_id ORDER BY (SELECT NULL)) RN
FROM customers
)Tmp WHERE RN = 1
答案 1 :(得分:0)
select cust_id, min(cust_name)
from customers
group by cust_id
答案 2 :(得分:0)
如果不需要订购,只需运行:
SELECT cust_id, MAX(cust_name)
FROM customers
GROUP BY cust_id
答案 3 :(得分:0)
这应该有效
select cust_id,
min(cust_name) as cust_name
from customers
group by cust_id