私はこのテーブルを持っています
そして、感情を列として表示するピボットを作成する必要があります。平均の感情レベルは、user_id、user_date、emotionでグループ化されています。たとえば、user_id = 1、user_date = 2011-07-13、emotion ='Anger'の場合、平均のemotion_levelは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;
これは半分は機能しますが、すべての感情の平均のemotion_levelを計算しますが、ユーザー、日付、感情でグループ化された感情については計算しません。
最初のユーザーの結果+感情='怒り'=2ですが、4になるはずです。
おそらく、ウィンドウ関数(over(user_id、user_date、emotionによるパーティション))を使用する必要がありますが、構文を実行できません。
それは可能ですか?
私はprodでPostgreSQL9を使用していますが、上記の例はSQLServerで記述されています。