在另一列的某些条件下复制具有空值的列
Replicate the a column which has null values on certain conditions on another column
使用的数据库是 SQL 服务器。
我的 table 中有以下值:
ID SESSION_ID UID CREATED_DATE MESSAGE_TYPE MESSAGE QID
A10001 s1 U1 2020-05-25 06:14:23.430 U fjdlasjfljdlajfljda NULL
A10002 s1 U1 2020-05-25 06:14:29.480 B fjdlasjfljdlajfljda NULL
A10003 s1 U1 2020-05-25 06:14:32.317 U fjdlasjfljdlajfljda NULL
A10004 s1 U1 2020-05-25 06:14:39.297 B fjdlasjfljdlajfljda NULL
A10005 s1 U1 2020-05-25 06:14:43.123 B fjdlasjfljdlajfljda NULL
A10006 s1 U1 2020-05-25 06:14:47.420 U fjdlasjfljdlajfljda NULL
A10007 s1 U1 2020-05-25 06:15:19.237 B fjdlasjfljdlajfljda NULL
A10008 s1 U1 2020-05-25 06:15:29.467 B fjdlasjfljdlajfljda NULL
A10009 s1 U1 2020-05-25 06:15:29.467 B fjdlasjfljdlajfljda NULL
A10010 s2 U1 2020-05-25 06:15:37.040 U fjdlasjfljdlajfljda NULL
A10011 s2 U1 2020-05-25 06:15:53.583 B fjdlasjfljdlajfljda NULL
A10012 s2 U1 2020-05-25 06:15:56.520 B fjdlasjfljdlajfljda NULL
A10013 s3 U2 2020-05-25 06:16:00.213 U fjdlasjfljdlajfljda NULL
A10014 s3 U2 2020-05-25 06:16:03.547 B fjdlasjfljdlajfljda NULL
A10015 s3 U2 2020-05-25 06:16:06.430 U fjdlasjfljdlajfljda NULL
我正在尝试根据以下条件将 Id 的值插入到我的 QID 列中:
- Message_type 是 U - QID 为空
- 对于所有后续消息类型 "B",QID 将具有来自消息类型为 "U"
的 ID 列的值
下面是示例输出:
ID SESSION_ID UID CREATED_DATE MESSAGE_TYPE MESSAGE QID
A10001 s1 U1 2020-05-25 06:14:23.430 U fjdlasjfljdlajfljda NULL
A10002 s1 U1 2020-05-25 06:14:29.480 B fjdlasjfljdlajfljda A10001
A10003 s1 U1 2020-05-25 06:14:32.317 U fjdlasjfljdlajfljda NULL
A10004 s1 U1 2020-05-25 06:14:39.297 B fjdlasjfljdlajfljda A10003
A10005 s1 U1 2020-05-25 06:14:43.123 B fjdlasjfljdlajfljda A10003
A10006 s1 U1 2020-05-25 06:14:47.420 U fjdlasjfljdlajfljda NULL
A10007 s1 U1 2020-05-25 06:15:19.237 B fjdlasjfljdlajfljda A10006
A10008 s1 U1 2020-05-25 06:15:29.467 B fjdlasjfljdlajfljda A10006
A10009 s1 U1 2020-05-25 06:15:29.467 B fjdlasjfljdlajfljda A10006
A10010 s2 U1 2020-05-25 06:15:37.040 U fjdlasjfljdlajfljda NULL
A10011 s2 U1 2020-05-25 06:15:53.583 B fjdlasjfljdlajfljda A10010
A10012 s2 U1 2020-05-25 06:15:56.520 B fjdlasjfljdlajfljda A10010
A10013 s3 U2 2020-05-25 06:16:00.213 U fjdlasjfljdlajfljda NULL
A10014 s3 U2 2020-05-25 06:16:03.547 B fjdlasjfljdlajfljda A10013
A10015 s3 U2 2020-05-25 06:16:06.430 U fjdlasjfljdlajfljda NULL
我试过使用row_number()
、ranking
。但没有运气。
请帮我解决这个问题。
您可以为此使用 window 函数。我建议使用每次满足 message_type
'U'
时递增的 window 总和来定义组,然后 first_value()
:
select
id, session_id, uid, created_date, message_type, message,
case when message_type = 'B'
then first_value(id) over(partition by grp order by created_date)
end qid
from (
select
t.*,
sum(case when message_type = 'U' then 1 else 0 end)
over(order by created_date) grp
from mytable t
) t
id | session_id | uid | created_date | message_type | message | qid
:----- | :--------- | :-- | :---------------------- | :----------- | :------------------ | :-----
A10001 | s1 | U1 | 2020-05-25 06:14:23.430 | U | fjdlasjfljdlajfljda | null
A10002 | s1 | U1 | 2020-05-25 06:14:29.480 | B | fjdlasjfljdlajfljda | A10001
A10003 | s1 | U1 | 2020-05-25 06:14:32.317 | U | fjdlasjfljdlajfljda | null
A10004 | s1 | U1 | 2020-05-25 06:14:39.297 | B | fjdlasjfljdlajfljda | A10003
A10005 | s1 | U1 | 2020-05-25 06:14:43.123 | B | fjdlasjfljdlajfljda | A10003
A10006 | s1 | U1 | 2020-05-25 06:14:47.420 | U | fjdlasjfljdlajfljda | null
A10007 | s1 | U1 | 2020-05-25 06:15:19.237 | B | fjdlasjfljdlajfljda | A10006
A10008 | s1 | U1 | 2020-05-25 06:15:29.467 | B | fjdlasjfljdlajfljda | A10006
A10009 | s1 | U1 | 2020-05-25 06:15:29.467 | B | fjdlasjfljdlajfljda | A10006
A10010 | s2 | U1 | 2020-05-25 06:15:37.040 | U | fjdlasjfljdlajfljda | null
A10011 | s2 | U1 | 2020-05-25 06:15:53.583 | B | fjdlasjfljdlajfljda | A10010
A10012 | s2 | U1 | 2020-05-25 06:15:56.520 | B | fjdlasjfljdlajfljda | A10010
A10013 | s3 | U2 | 2020-05-25 06:16:00.213 | U | fjdlasjfljdlajfljda | null
A10014 | s3 | U2 | 2020-05-25 06:16:03.547 | B | fjdlasjfljdlajfljda | A10013
A10015 | s3 | U2 | 2020-05-25 06:16:06.430 | U | fjdlasjfljdlajfljda | null
使用的数据库是 SQL 服务器。
我的 table 中有以下值:
ID SESSION_ID UID CREATED_DATE MESSAGE_TYPE MESSAGE QID
A10001 s1 U1 2020-05-25 06:14:23.430 U fjdlasjfljdlajfljda NULL
A10002 s1 U1 2020-05-25 06:14:29.480 B fjdlasjfljdlajfljda NULL
A10003 s1 U1 2020-05-25 06:14:32.317 U fjdlasjfljdlajfljda NULL
A10004 s1 U1 2020-05-25 06:14:39.297 B fjdlasjfljdlajfljda NULL
A10005 s1 U1 2020-05-25 06:14:43.123 B fjdlasjfljdlajfljda NULL
A10006 s1 U1 2020-05-25 06:14:47.420 U fjdlasjfljdlajfljda NULL
A10007 s1 U1 2020-05-25 06:15:19.237 B fjdlasjfljdlajfljda NULL
A10008 s1 U1 2020-05-25 06:15:29.467 B fjdlasjfljdlajfljda NULL
A10009 s1 U1 2020-05-25 06:15:29.467 B fjdlasjfljdlajfljda NULL
A10010 s2 U1 2020-05-25 06:15:37.040 U fjdlasjfljdlajfljda NULL
A10011 s2 U1 2020-05-25 06:15:53.583 B fjdlasjfljdlajfljda NULL
A10012 s2 U1 2020-05-25 06:15:56.520 B fjdlasjfljdlajfljda NULL
A10013 s3 U2 2020-05-25 06:16:00.213 U fjdlasjfljdlajfljda NULL
A10014 s3 U2 2020-05-25 06:16:03.547 B fjdlasjfljdlajfljda NULL
A10015 s3 U2 2020-05-25 06:16:06.430 U fjdlasjfljdlajfljda NULL
我正在尝试根据以下条件将 Id 的值插入到我的 QID 列中:
- Message_type 是 U - QID 为空
- 对于所有后续消息类型 "B",QID 将具有来自消息类型为 "U" 的 ID 列的值
下面是示例输出:
ID SESSION_ID UID CREATED_DATE MESSAGE_TYPE MESSAGE QID
A10001 s1 U1 2020-05-25 06:14:23.430 U fjdlasjfljdlajfljda NULL
A10002 s1 U1 2020-05-25 06:14:29.480 B fjdlasjfljdlajfljda A10001
A10003 s1 U1 2020-05-25 06:14:32.317 U fjdlasjfljdlajfljda NULL
A10004 s1 U1 2020-05-25 06:14:39.297 B fjdlasjfljdlajfljda A10003
A10005 s1 U1 2020-05-25 06:14:43.123 B fjdlasjfljdlajfljda A10003
A10006 s1 U1 2020-05-25 06:14:47.420 U fjdlasjfljdlajfljda NULL
A10007 s1 U1 2020-05-25 06:15:19.237 B fjdlasjfljdlajfljda A10006
A10008 s1 U1 2020-05-25 06:15:29.467 B fjdlasjfljdlajfljda A10006
A10009 s1 U1 2020-05-25 06:15:29.467 B fjdlasjfljdlajfljda A10006
A10010 s2 U1 2020-05-25 06:15:37.040 U fjdlasjfljdlajfljda NULL
A10011 s2 U1 2020-05-25 06:15:53.583 B fjdlasjfljdlajfljda A10010
A10012 s2 U1 2020-05-25 06:15:56.520 B fjdlasjfljdlajfljda A10010
A10013 s3 U2 2020-05-25 06:16:00.213 U fjdlasjfljdlajfljda NULL
A10014 s3 U2 2020-05-25 06:16:03.547 B fjdlasjfljdlajfljda A10013
A10015 s3 U2 2020-05-25 06:16:06.430 U fjdlasjfljdlajfljda NULL
我试过使用row_number()
、ranking
。但没有运气。
请帮我解决这个问题。
您可以为此使用 window 函数。我建议使用每次满足 message_type
'U'
时递增的 window 总和来定义组,然后 first_value()
:
select
id, session_id, uid, created_date, message_type, message,
case when message_type = 'B'
then first_value(id) over(partition by grp order by created_date)
end qid
from (
select
t.*,
sum(case when message_type = 'U' then 1 else 0 end)
over(order by created_date) grp
from mytable t
) t
id | session_id | uid | created_date | message_type | message | qid :----- | :--------- | :-- | :---------------------- | :----------- | :------------------ | :----- A10001 | s1 | U1 | 2020-05-25 06:14:23.430 | U | fjdlasjfljdlajfljda | null A10002 | s1 | U1 | 2020-05-25 06:14:29.480 | B | fjdlasjfljdlajfljda | A10001 A10003 | s1 | U1 | 2020-05-25 06:14:32.317 | U | fjdlasjfljdlajfljda | null A10004 | s1 | U1 | 2020-05-25 06:14:39.297 | B | fjdlasjfljdlajfljda | A10003 A10005 | s1 | U1 | 2020-05-25 06:14:43.123 | B | fjdlasjfljdlajfljda | A10003 A10006 | s1 | U1 | 2020-05-25 06:14:47.420 | U | fjdlasjfljdlajfljda | null A10007 | s1 | U1 | 2020-05-25 06:15:19.237 | B | fjdlasjfljdlajfljda | A10006 A10008 | s1 | U1 | 2020-05-25 06:15:29.467 | B | fjdlasjfljdlajfljda | A10006 A10009 | s1 | U1 | 2020-05-25 06:15:29.467 | B | fjdlasjfljdlajfljda | A10006 A10010 | s2 | U1 | 2020-05-25 06:15:37.040 | U | fjdlasjfljdlajfljda | null A10011 | s2 | U1 | 2020-05-25 06:15:53.583 | B | fjdlasjfljdlajfljda | A10010 A10012 | s2 | U1 | 2020-05-25 06:15:56.520 | B | fjdlasjfljdlajfljda | A10010 A10013 | s3 | U2 | 2020-05-25 06:16:00.213 | U | fjdlasjfljdlajfljda | null A10014 | s3 | U2 | 2020-05-25 06:16:03.547 | B | fjdlasjfljdlajfljda | A10013 A10015 | s3 | U2 | 2020-05-25 06:16:06.430 | U | fjdlasjfljdlajfljda | null