Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6144677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:41:04+00:00 2026-05-23T18:41:04+00:00

I have this table And I need to create a pivot, that displays emotions

  • 0

I have this table

enter image description here

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.

enter image description here

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:41:04+00:00Added an answer on May 23, 2026 at 6:41 pm
    WITH    q (id, user_id, user_date, emotion, emotion_level) AS
            (
            VALUES
            (1, 1, '2011-07-13'::DATE, 'Anger', 3),
            (2, 1, '2011-07-13'::DATE, 'Anger', 5),
            (3, 1, '2011-07-13'::DATE, 'Sadness', 2),
            (4, 1, '2011-07-13'::DATE, 'Interest', 2),
            (5, 2, '2011-07-13'::DATE, 'Anger', 1),
            (6, 2, '2011-07-13'::DATE, 'Sadness', 4),
            (7, 2, '2011-07-13'::DATE, 'Sadness', 5),
            (8, 2, '2011-07-13'::DATE, 'Interest', 3),
            (9, 3, '2011-07-13'::DATE, 'Anger', 1),
            (10, 3, '2011-07-13'::DATE, 'Sadness', 3),
            (11, 3, '2011-07-13'::DATE, 'Interest', 4),
            (12, 3, '2011-07-13'::DATE, 'Interest', 5)
            ) 
    SELECT  user_id, user_date,
            AVG(CASE emotion WHEN 'Anger' THEN emotion_level END)::numeric(3, 2) AS Anger,
            AVG(CASE emotion WHEN 'Sadness' THEN emotion_level END)::numeric(3, 2) AS Sadness,
            AVG(CASE emotion WHEN 'Interest' THEN emotion_level END)::numeric(3, 2) AS Interest
    FROM    q
    GROUP BY
            user_id, user_date
    ORDER BY
            user_id, user_date
    

    The problem was that the expressions you originally used:

    AVG(case emotion when 'Interest' then convert(float, emotion_level) else 0 end)
    

    averaged over all records for a given user at given date and treated non-Interest entries as 0, while they should be treated as NULL so they would not contribute to Interest average.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a pivot table in R that will have text values
So I have this SQL table that I need to update. Now we use
I need some help with a query. I have this simple table: CREATE TABLE
Hey... I have this database and I need to create a function that returns
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
I have this table: CREATE TABLE `test` ( `ID` int(11) NOT NULL auto_increment, `text`
I have this table: CREATE TABLE `point` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `siteid`
I have this table: id feed_id ... Let's say that I have 500 rows
I have a table called 'notes', on this table I need to track who

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.