英文:
How to change all values to the first in the sequence?
问题
我有3列:UserID,Channel和Order_Sequence。我希望根据用户ID的第一个序列来保留Channel列的第一个通道。我如何在SQL中获取输出列WANT?
英文:
I have 3 columns: UserID, Channel, and Order_Sequence. I want the Channel column to keep the first channel based on the first sequence of the user id. How can I get it to output the column WANT in sql?
答案1
得分: 2
你可以使用 first_value()
函数:
select t.*,
first_value(channel) over (partition by userid order by sequence) as want
from t;
英文:
You can use first_value()
:
select t.*,
first_value(channel) over (partition by userid order by sequence) as want
from t;
答案2
得分: 0
Using the good old correlated subquery
approach
select t.*, (select channel from test where userid=t.userid and order_sequence=1) as want
from test t;
英文:
Using the good old correlated subquery
approach
select t.*, (select channel from test where userid=t.userid and order_sequence=1) as want
from test t;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论