枢轴内的窗口函数,可能吗?
我有这个表
我需要创建一个数据透视表,将情感显示为列,并按平均情感级别分组按用户 ID、用户日期、情感。例如,对于 user_id = 1、user_date = 2011-07-13 且情感 = 'Anger',平均情感级别应为 4.0。
我创建了一个枢轴:
select USER_ID, user_date,
AVG(case emotion when 'Anger' then convert(float, emotion_level) else 0 end) as Anger,
AVG(case emotion when 'Sadness' then convert(float, emotion_level) else 0 end) as Sadness,
AVG(case emotion when 'Interest' then convert(float, emotion_level) else 0 end) as Interest
from emotions group by USER_ID, user_date;
which half work,但计算所有情绪中的平均情感水平,但不计算按用户、日期和情绪分组的情绪。
我对第一个用户的结果 + 情感 = '愤怒' = 2,但它应该是 4。
我猜,我应该使用窗口函数(over(partition by user_id,user_date,emotion)),但无法运行语法。
有可能吗?
我在产品中使用 PostgreSQL 9,但上面的示例是用 SQL Server 编写的。
I have this table
And I need to create a pivot, that displays emotions as columns, with average emotion_level grouped by user_id, user_date, emotion. For example, for user_id = 1, user_date = 2011-07-13 and emotion = 'Anger', the average emotion_level should be 4.0.
I create a pivot:
select USER_ID, user_date,
AVG(case emotion when 'Anger' then convert(float, emotion_level) else 0 end) as Anger,
AVG(case emotion when 'Sadness' then convert(float, emotion_level) else 0 end) as Sadness,
AVG(case emotion when 'Interest' then convert(float, emotion_level) else 0 end) as Interest
from emotions group by USER_ID, user_date;
Which half-works, but calculates average emotion_level among all emotions, but not for emotions grouped by user, date and emotion.
My result for first user + emotion = 'Anger' = 2, but it should be 4.
I guess, I should use the window function (over (partition by user_id, user_date, emotion)), but can't get the syntax run.
Is it possible at all?
I'm using PostgreSQL 9 in prod, but the above example is written in SQL Server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题在于您最初使用的表达式:
对给定日期给定用户的所有记录进行平均,并将非
兴趣
条目视为0
,而它们应该被视为NULL
因此它们不会对Interest
平均值做出贡献。The problem was that the expressions you originally used:
averaged over all records for a given user at given date and treated non-
Interest
entries as0
, while they should be treated asNULL
so they would not contribute toInterest
average.我首先将表中“emotion_level”的数据类型更新为“float”,然后使用pivot执行操作。如果数据类型未更新,则所需值为 int 数据类型。
以下链接中上述查询的结果
I first updated the datatype of 'emotion_level' to 'float' in the table and then performed the operation using pivot. If the datatype is not updated, the required values are of int datatype.
The result from the above query in the link below
我会定义一个 CTE,然后
加入
它 - 您的主要问题可能是您没有在emotion_level
上分组
。试试这个:然后,每次需要新列时,只需添加额外的
左连接
即可。有多种方法可以对其进行重组,主要与数据模型的其余部分和所需的输出有关。
I'd define a CTE, then
join
to it - your main problem is probably the fact that you're notgroup
ing onemotion_level
. Try this:Then, just add additional
left join
s every time you need a new column.There's a number of ways to restructure this, mostly relating to the rest of your data model, and desired output.