如何将所有值更改为序列中的第一个值?

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

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;

huangapple
  • 本文由 发表于 2020年1月4日 00:08:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581734.html
匿名

发表评论

匿名网友

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

确定