创建列,从另一列中获取连续的数值。

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

Create columns taking successive values from another column

问题

下面是您要的翻译内容:

| date                | message | rnk_id | mess1               | mess2               | mess3               |
|---------------------|---------|--------|---------------------|---------------------|---------------------|
| 2022-12-19 10:48:51 | mess1   | 8      | 2022-12-19 10:48:51 | 2022-12-19 10:57:13 | 2022-12-19 10:57:23 |
| 2022-12-19 10:57:13 | mess2   | 8      | 2022-12-19 10:48:51 | 2022-12-19 10:57:13 | 2022-12-19 10:57:23 |
| 2022-12-19 10:57:23 | mess3   | 8      | 2022-12-19 10:48:51 | 2022-12-19 10:57:13 | 2022-12-19 10:57:23 |
| 2022-12-19 10:57:49 | mess4   | 8      | 2022-12-19 10:48:51 | 2022-12-19 10:57:13 | 2022-12-19 10:57:23 |
| 2022-12-19 10:57:58 | mess5   | 8      | 2022-12-19 10:48:51 | 2022-12-19 10:57:13 | 2022-12-19 10:57:23 |
| 2022-12-19 10:58:07 | mess6   | 8      | 2022-12-19 10:48:51 | 2022-12-19 10:57:13 | 2022-12-19 10:57:23 |
| 2022-12-19 11:00:36 | mess7   | 8      | 2022-12-19 10:48:51 | 2022-12-19 10:57:13 | 2022-12-19 10:57:23 |
| 2023-02-06 11:17:55 | mess1   | 5      | 2023-02-06 11:17:55 | 2023-02-06 11:18:02 | 2023-02-06 11:20:08 |
| 2023-02-06 11:18:02 | mess2   | 5      | 2023-02-06 11:17:55 | 2023-02-06 11:18:02 | 2023-02-06 11:20:08 |
| 2023-02-06 11:20:08 | mess3   | 5      | 2023-02-06 11:17:55 | 2023-02-06 11:18:02 | 2023-02-06 11:20:08 |
| 2023-02-06 11:20:19 | mess4   | 5      | 2023-02-06 11:17:55 | 2023-02-06 11:18:02 | 2023-02-06 11:20:08 |
| 2023-02-06 11:20:37 | mess5   | 5      | 2023-02-06 11:17:55 | 2023-02-06 11:18:02 | 2023-02-06 11:20:08 |
| 2023-02-06 11:20:40 | mess6   | 5      | 2023-02-06 11:17:55 | 2023-02-06 11:18:02 | 2023-02-06 11:20:08 |
| 2023-02-06 11:22:12 | mess7   | 5      | 2023-02-06 11:17:55 | 2023-02-06 11:18:02 | 2023-02-06 11:20:08 |

希望这可以满足您的需求。如果您有其他问题,请随时告诉我。

英文:
date message rnk_id
2022-12-19 10:48:51 mess1 8
2022-12-19 10:57:13 mess2 8
2022-12-19 10:57:23 mess3 8
2022-12-19 10:57:49 mess4 8
2022-12-19 10:57:58 mess5 8
2022-12-19 10:58:07 mess6 8
2022-12-19 11:00:36 mess7 8
2023-02-06 11:17:55 mess1 5
2023-02-06 11:18:02 mess2 5
2023-02-06 11:20:08 mess3 5
2023-02-06 11:20:19 mess4 5
2023-02-06 11:20:37 mess5 5
2023-02-06 11:20:40 mess6 5
2023-02-06 11:22:12 mess7 5

each new column must take the value of the date corresponding to the message and reproduce it for each rnk_id group.

Each new column mess1, mess2, mess3, etc... takes the value of the column message. For column 1, I take the date that corresponds to mess1 (2022-12-19 10:48:51.5470000) and I copy it for each group rnk_id, for the second column I take the value mess 2 and I take the date of mess2 (2022-12-19 10:57:13.4230000) and I copy it for each group rnk_id... and so on

expected output :

date message rnk_id mess1 mess2 mess3
2022-12-19 10:48:51 mess1 8 2022-12-19 10:48:51 2022-12-19 10:57:13 2022-12-19 10:57:23
2022-12-19 10:57:13 mess2 8 2022-12-19 10:48:51 2022-12-19 10:57:13 2022-12-19 10:57:23
2022-12-19 10:57:23 mess3 8 2022-12-19 10:48:51 2022-12-19 10:57:13 2022-12-19 10:57:23
2022-12-19 10:57:49 mess4 8 2022-12-19 10:48:51 2022-12-19 10:57:13 2022-12-19 10:57:23
2022-12-19 10:57:58 mess5 8 2022-12-19 10:48:51 2022-12-19 10:57:13 2022-12-19 10:57:23
2022-12-19 10:58:07 mess6 8 2022-12-19 10:48:51 2022-12-19 10:57:13 2022-12-19 10:57:23
2022-12-19 11:00:36 mess7 8 2022-12-19 10:48:51 2022-12-19 10:57:13 2022-12-19 10:57:23
2023-02-06 11:17:55 mess1 5 2023-02-06 11:17:55 2023-02-06 11:18:02 2023-02-06 11:20:08
2023-02-06 11:18:02 mess2 5 2023-02-06 11:17:55 2023-02-06 11:18:02 2023-02-06 11:20:08
2023-02-06 11:20:08 mess3 5 2023-02-06 11:17:55 2023-02-06 11:18:02 2023-02-06 11:20:08
2023-02-06 11:20:19 mess4 5 2023-02-06 11:17:55 2023-02-06 11:18:02 2023-02-06 11:20:08
2023-02-06 11:20:37 mess5 5 2023-02-06 11:17:55 2023-02-06 11:18:02 2023-02-06 11:20:08
2023-02-06 11:20:40 mess6 5 2023-02-06 11:17:55 2023-02-06 11:18:02 2023-02-06 11:20:08
2023-02-06 11:22:12 mess7 5 2023-02-06 11:17:55 2023-02-06 11:18:02 2023-02-06 11:20:08

for the first column no worries:
first_value(date) OVER (PARTITION BY rnk_id ORDER BY date) as mess1

I am unable to use the ROWS Clause to achieve this

答案1

得分: 1

使用ctefirst_value()以及lead(),我们正确获取第一行,然后将这一行扩展到所有行上:

with cte as (
  select t.*, 
    first_value(case when message = 'mess1' then date end)
      over (partition by rnk_id order by date) as mess1,
    lead(date)
      over (partition by rnk_id order by date) as mess2,
    lead(date,2)
      over (partition by rnk_id order by date) as mess3
  from mytable t
)
select date, message, rnk_id, mess1,
  first_value(mess2) over (partition by rnk_id order by date) as mess2,
  first_value(mess3) over (partition by rnk_id order by date) as mess3
from cte
order by rnk_id desc

演示在此处

英文:

with cte and using first_value() and lead() we get the first row correctly then we spread this row over all the rows :

with cte as (
select t.*, 
first_value(case when message = 'mess1' then date end)
over (partition by rnk_id order by date) as mess1,
lead(date)
over (partition by rnk_id order by date) as mess2,
lead(date,2)
over (partition by rnk_id order by date) as mess3
from mytable t
)
select date, message, rnk_id, mess1,
first_value(mess2) over (partition by rnk_id order by date) as mess2,
first_value(mess3) over (partition by rnk_id order by date) as mess3
from cte
order by rnk_id desc

Demo here

答案2

得分: -1

你可以转换它,但是结果中会失去主日期和消息列。

[Fiddle][1]

SELECT * FROM
(
SELECT rnk_id,
message,
_date
FROM mess
) src
PIVOT
(
MAX(_date)
FOR message IN (
[mess1],
[mess2],
[mess3],
[mess4],
[mess5],
[mess6],
[mess7])
) pvt

结果

rnk_id mess1 mess2 mess3 mess4 mess5 mess6 mess7
5 2022-12-19 11:17:55.2360000 2022-12-19 11:18:02.6220000 2022-12-19 11:20:08.4910000 2022-12-19 11:20:19.2010000 2022-12-19 11:20:37.1190000 2022-12-19 11:20:40.2950000 2022-12-19 11:22:12.6280000
8 2022-12-19 10:48:51.5470000 2022-12-19 10:57:13.4230000 2022-12-19 10:57:23.6540000 2022-12-19 10:57:49.1290000 2022-12-19 10:57:58.0850000 2022-12-19 10:58:07.0980000 2022-12-19 11:00:36.0690000

[1]: https://dbfiddle.uk/_lWLstcC

英文:

You could pivot it but you lose the leading date and message columns in the result.

Fiddle

SELECT * FROM
(
SELECT rnk_id,
message,
_date
FROM mess
) src
PIVOT
(
MAX(_date)
FOR message IN (
[mess1],
[mess2],
[mess3],
[mess4],
[mess5],
[mess6],
[mess7])
) pvt

Result

rnk_id mess1 mess2 mess3 mess4 mess5 mess6 mess7
5 2022-12-19 11:17:55.2360000 2022-12-19 11:18:02.6220000 2022-12-19 11:20:08.4910000 2022-12-19 11:20:19.2010000 2022-12-19 11:20:37.1190000 2022-12-19 11:20:40.2950000 2022-12-19 11:22:12.6280000
8 2022-12-19 10:48:51.5470000 2022-12-19 10:57:13.4230000 2022-12-19 10:57:23.6540000 2022-12-19 10:57:49.1290000 2022-12-19 10:57:58.0850000 2022-12-19 10:58:07.0980000 2022-12-19 11:00:36.0690000

huangapple
  • 本文由 发表于 2023年2月24日 04:01:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549764.html
匿名

发表评论

匿名网友

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

确定