skip to Main Content

Suppose I have the following table

id | items   |  price  |
  1| item_1  |  15.0   |
  2| item_2  |  10.0   |
  3| item_1  |  15.0   |
  4| item_1  |  15.0   |
  5| item_2  |  10.0   |
  6| item_2  |  10.0   |
  7| item_3  |  25.0   |

and I want the following output

 items   |  price  | count | sum  |
 item_1  |  15.0   | 3     | 45.0 |
 item_2  |  10.0   | 3     | 30.9 |
 item_3  |  25.0   | 1     | 25.0 | 

2

Answers


  1. you have to do query like this

    select items, price, count(items) as count, sum (price) as sum from table1 group by items, price
    
    Login or Signup to reply.
  2. We can use analytic functions here:

    WITH cte AS (
        SELECT *, SUM(price) OVER (PARTITION BY items, price) sum,
                  COUNT(*) OVER (PARTITION BY items, price) count
        FROM yourTable
    )
    
    SELECT DISTINCT items, price, count, sum
    FROM cte
    ORDER BY items;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search