How can I sum a group of a custom calculated field in SQL?

nootskej

I have many shopper records who have a unique ID, there will be instances where a shopper with the same ID exists and I would like to sum up their shoppable_rated score and only return 1 result of the total sum of shoppable_rated for those IDs, so there will be no duplicate rows for the ID and their total score will be added up. Please can you advise how I would change my query? I got a little stuck trying to use HAVING and couldn't figure it out.

Many thanks!

SELECT 
    id, 
    shoppable,
    day,
    case when shoppable in ('Good','Very Good','Amazing') then 1 else 0 end as shoppable_rated
FROM 
    shoppers
WHERE
    shoppable != 'Unknown'
GROUP BY 
    id
ORDER BY 
    shoppable_rated DESC;

Example of my data:

id, shoppable, day, shoppable_rated
1, Good, 26, 1
2, Very Good, 14, 1
3, Not Great, 3, 0
1, Bad, 16, 0
3, Amazing, 30, 1
Gordon Linoff

First, remove all the unaggregated columns that are not needed in the result set.

Second, use an aggregation function:

SELECT id,
       SUM(case when shoppable in ('Good','Very Good','Amazing') then 1 else 0 end) as shoppable_rated
FROM shoppers
WHERE shoppable <> 'Unknown'
GROUP BY id
ORDER BY shoppable_rated DESC;

If you need the data by id and day, then include both columns in both the SELECT and the GROUP BY:

SELECT id, day,
       SUM(case when shoppable in ('Good','Very Good','Amazing') then 1 else 0 end) as shoppable_rated
FROM shoppers
WHERE shoppable <> 'Unknown'
GROUP BY id, day
ORDER BY shoppable_rated DESC;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I reference a calculated field in a subquery?

SQL Query to Sum Calculated Field

How can I add a custom method to auth_user model class? I need to return a calculated field along with results

How can use count/{$sum: 1} in other custom field with $group in mongoDB?

Access query is asking for parameters for calculated field, how can I stop this?

How can I implement Auto calculated field in mongodb?

How can I group mysql results based on a calculated colum?

How can i write sum query with linq to sql to calculate sum of the nchar field value?

How can I add a custom fee in woocommerce that is calculated after tax

How can I group by one field and sum fields in one object(output structure preserved) in MongoDB?

how can I sum by a group in xpath

How can I "group by" and sum a column in excel?

How do I copy an Advanced Custom Field field group in wordpress?

Aggregate and Sum on calculated field

How can I group by a field emitted events?

How can I update a column with PL\SQL by using a calculated value

How can I sum strings in one column into one row in Microsoft SQL? Group by does not work since it is MSSQL

How can I achieve a similar result to a SQL SUM and GROUP BY query in JavaScript and ReactJS for an array of objects?

How can I calculate the cumulative sum of a column for each group of rows in SQL?

How can I group by a field by a condition of that specific field?

How to use order by on a calculated field in SQL?

How to group by and sum in sql

Google Sheets : How can I sum two calculated fields in a pivot table

how can I calculated point of each user per day with sum all the points from beginning to that day in clickhouse

Can I "GROUP BY" a partial field value in Access/SQL?

SQL Sum with calculated column

How to correctly sum summarised Fields in a PivotTable with a calculated field

How can I pass an extra calculated field in a Rails 7 controller response in this context

In Tableau how can I create a calculated field that categorizes data based on data across rows?