英文:
How to pivot/transform table?
问题
我有一个表格。
示例:
run_ts | user | rnk |
---|---|---|
1234 | A | 1 |
1235 | A | 2 |
123 | B | 1 |
124 | B | 2 |
我想将它转换成
first_run_ts | last_run_ts | user |
---|---|---|
1234 | 1235 | A |
123 | 124 | B |
我该如何做?
英文:
I have a table.
Example:
run_ts | user | rnk |
---|---|---|
1234 | A | 1 |
1235 | A | 2 |
123 | B | 1 |
124 | B | 2 |
I want to transform it into
first_run_ts | last_run_ts | user |
---|---|---|
1234 | 1235 | A |
123 | 124 | B |
How can i do it?
答案1
得分: 1
对于通用解决方案,我认为您可以使用MIN
和MAX
与GROUP BY
的组合...请参见以下内容:
SELECT
MIN(run_ts) AS first_run_ts,
MAX(run_ts) AS last_run_ts,
[user]
FROM
your_table
GROUP BY
[user]
英文:
For a generic solution, I think you can use a combination of MIN
and MAX
with GROUP BY
...see below:
SELECT
MIN(run_ts) AS first_run_ts,
MAX(run_ts) AS last_run_ts,
[user]
FROM
your_table
GROUP BY
[user]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论