有没有一种方法可以使用等级函数在表中平坦值?
我在SQL中有一个表格,看起来像这样
-Item | activity_id | activity_type | activation_date |
---|---|---|---|
项目1 | 活动a | 呼叫 | Jan -1-2022 |
项目1 | 活动B | 邮件 | Jan -10-2022 |
项目1 | 活动C | PRITIST C PRINT | JAN -12-2022 |
类似,有成千上万个项目和每个都可以进行一个或多个活动(最多5个)。 我想运行一个SQL查询以使所有记录的项目级别在项目级别上弄平数据,而所需的输出就是这样 -
项目 | 活动1 | 活动2 | 活动3 | 活动4 | 活动5 | 活动1日期 | 活动2日期活动3日期 | 活动3日期 | 活动4日期 | 活动5日期 |
---|---|---|---|---|---|---|---|---|---|---|
项目1 | 通话 | 邮件 | 打印 | 1月1日至1-2022 | JAN -10-2022 | JAN -12-2022 |
根据活动日期的上升顺序,活动列(1-5)填充了活动列(1-5)。
有办法实现这一目标吗?另外,我可以在python中导入原始数据,如果有一种优雅的方法可以使用熊猫来进行转换。
请注意,列值仍然是列值,并且与PANDAS中的Undivot操作并不相同。我看到了关于熊猫中未分散的答案,但无法使用那里的答案来解决这个特殊的问题
谢谢,
I have a table in SQL which looks like this -
ITEM | ACTIVITY_ID | ACTIVITY_TYPE | ACTIVITY_DATE |
---|---|---|---|
Item 1 | Activity A | Call | Jan - 1 - 2022 |
Item 1 | Activity B | Jan - 10 - 2022 | |
Item 1 | Activity C | Jan - 12 - 2022 |
Similarly, there are thousands of Items and each can have one or more activities (up to 5).
I want to run a SQL query to flatten the data at the Item level for all the records and the output desired is something like this -
ITEM | ACTIVITY 1 | ACTIVITY 2 | ACTIVITY 3 | ACTIVITY 4 | ACTIVITY 5 | ACTIVITY 1 DATE | ACTIVITY 2 DATE | ACTIVITY 3 DATE | ACTIVITY 4 DATE | ACTIVITY 5 DATE |
---|---|---|---|---|---|---|---|---|---|---|
Item 1 | Call | Jan - 1 - 2022 | Jan - 10 - 2022 | Jan - 12 - 2022 |
The activity columns (1-5) are populated based on the ascending order of activity date.
Is there a way to achieve this? Also, I can import the raw data in Python and can do the transformation there as well if there's an elegant way to do it using Pandas.
Please note that the column values still remain as column values and it is not identical to the unpivot operation in pandas. I saw the answer on unpivoting in pandas but was not able to solve this particular problem using the answers there
Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模式:
PS。
activity_date
列中数据的格式似乎是非标准的,并且可能需要转换到日期数据类型。Pattern:
PS. The format of the data in
activity_date
column seems to be non-standard, and the convertion to DATE datatype may be required.您正在寻找枢轴,而不是不散文。
但是就您而言,N Path也有效:
You're looking for PIVOT, not UNPIVOT.
But in your case NPath works, too: