Count how many times a certain value per user has changed

Michi

DB-Fiddle

CREATE TABLE sales (
    id SERIAL PRIMARY KEY,
    order_date DATE,
    order_ID VARCHAR(255),
    customer_ID VARCHAR(255),
    score VARCHAR(255)
);

INSERT INTO sales
(order_date, order_ID, customer_ID, score)
VALUES 
('2020-04-10', 'Order_01', 'user_01', '1'),
('2020-04-15', 'Order_02', 'user_01', '5'),
('2020-05-18', 'Order_03', 'user_01', '4'),
('2020-05-22', 'Order_04', 'user_02', '1'),
('2020-06-18', 'Order_05', 'user_03', '3'),
('2020-06-26', 'Order_06', 'user_03', '2'),
('2020-07-28', 'Order_07', 'user_04', '2'),
('2020-08-03', 'Order_08', 'user_05', '1'),
('2020-09-10', 'Order_09', 'user_05', '1'),
('2020-09-15', 'Order_10', 'user_05', '1'),
('2020-10-23', 'Order_11', 'user_06', '4');

Expected Result:

order_date  | customer_ID  |  order_ID    |   score  |  score_changed  | orders_per_customer |
------------|--------------|--------------|----------|-----------------|---------------------|--
2020-04-10  |    user_01   |   order_01   |     1    |         2       |         1           |
2020-04-15  |    user_01   |   order_02   |     4    |         2       |         2           |
2020-05-18  |    user_01   |   order_03   |     5    |         2       |         3           |
------------|--------------|--------------|----------|-----------------|---------------------|--
2020-05-22  |    user_02   |   order_04   |     1    |         0       |         1           | 
------------|--------------|--------------|----------|-----------------|---------------------|--    
2020-06-18  |    user_03   |   order_05   |     3    |         1       |         1           | 
2020-06-26  |    user_03   |   order_06   |     2    |         1       |         2           | 
------------|--------------|--------------|----------|-----------------|---------------------|--
2020-07-28  |    user_04   |   order_07   |     2    |         0       |         1           | 
------------|--------------|--------------|----------|-----------------|---------------------|--
2020-08-03  |    user_05   |   order_08   |     1    |         0       |         1           | 
2020-09-10  |    user_05   |   order_09   |     1    |         0       |         2           | 
2020-09-15  |    user_05   |   order_10   |     1    |         0       |         3           | 
------------|--------------|--------------|----------|-----------------|---------------------|--
2020-10-23  |    user_06   |   order_11   |     4    |         0       |         1           | 

In the results above I want to achieve two things:

(1) count the orders_per_customer
(2) count the changes of the score per customer

With the below query I was able to solve (1):

SELECT
s.order_date,
s.customer_ID AS customer_ID,
s.order_ID AS order_ID,
s.score AS score,
ROW_NUMBER() OVER (PARTITION BY s.customer_ID ORDER BY s.order_date) AS orders_per_customer
FROM sales s
GROUP BY 1,2,3,4
ORDER BY 1,2,3,5;

However, I have no clue how I need to change the query to also display the count of how many times the score per customer has changed as you can see in column score_changed.
Do you have any idea?

Serkan Arslan

After determines the changes, you can count them.

db_fiddle

SELECT order_date, customer_ID ,order_ID, score,
  SUM(score_changed) OVER (PARTITION BY customer_ID ) AS changes_of_the_score
  , orders_per_customer
FROM (
  SELECT
  s.order_date,
  s.customer_ID AS customer_ID,
  s.order_ID AS order_ID,
  s.score AS score,
  CASE WHEN score <> LAG(score) OVER (PARTITION BY s.customer_ID ORDER BY s.order_date) THEN 1 ELSE 0 END score_changed ,
  ROW_NUMBER() OVER (PARTITION BY s.customer_ID ORDER BY s.order_date) AS orders_per_customer
  FROM sales s
  GROUP BY order_date, customer_ID ,order_ID, score
) AS T
ORDER BY order_date, customer_ID ,order_ID, score;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count how many times a value in the database has changed?

From Dev

How do I count how many times a value has changed pertaining to a unique ID?

From Dev

Count how many times values has changed in column using R

From Dev

R: Count how many times value has occured before within certain range of rows

From Dev

Count how many times a value appears per month in dataframe

From Dev

Count how many times a column contains a certain value in Pandas

From Dev

Play Framework: Count how many times a key exists in a JSON tree and how many times is set to a certain value

From Dev

Count how many times value has occured in rows

From Dev

Count how many times certain pandas row has specific column value lower than another certain pandas row across many pandas dataframes

From Dev

Counting how many times variable has changed

From Dev

PromQL, Grafana - Count how many times metric's labels values has changed

From Dev

Count How Many Times Customer Has Purchases

From Dev

how to count how many times a user logs in?

From Dev

How to count how many times a user has clicked a button and how to save it. Android Studio

From Dev

Count how many times certain text combinations occurs in certain columns

From Dev

How to count how many times a meta_value appears in a column by certain meta_key?

From Dev

How to count how many times the user has typed the wrong answer (Python)

From Dev

How can I count how many rows since a value has changed in a column vector in R?

From Dev

Is it possible to count how many times a user has logged in MVC5 without a separate counter?

From Dev

Count How Many Times a Value Appears Php

From Dev

SQL COUNT how many times a value appears

From Dev

Count how many times a value appears in this array?

From Dev

Count how many number of times a value appears

From Dev

Find how many times the department of an employee has changed

From Dev

Check how many times a file has been changed

From Dev

Count how many times a word occurs in a HashMap per key

From Dev

Elasticsearch: I want to count how many times a particular value has appeared in particular field

From Python

Recursion function to count how many times a certain sequence appears

From Java

Look for a certain String inside another and count how many times it appears

Related Related

  1. 1

    Count how many times a value in the database has changed?

  2. 2

    How do I count how many times a value has changed pertaining to a unique ID?

  3. 3

    Count how many times values has changed in column using R

  4. 4

    R: Count how many times value has occured before within certain range of rows

  5. 5

    Count how many times a value appears per month in dataframe

  6. 6

    Count how many times a column contains a certain value in Pandas

  7. 7

    Play Framework: Count how many times a key exists in a JSON tree and how many times is set to a certain value

  8. 8

    Count how many times value has occured in rows

  9. 9

    Count how many times certain pandas row has specific column value lower than another certain pandas row across many pandas dataframes

  10. 10

    Counting how many times variable has changed

  11. 11

    PromQL, Grafana - Count how many times metric's labels values has changed

  12. 12

    Count How Many Times Customer Has Purchases

  13. 13

    how to count how many times a user logs in?

  14. 14

    How to count how many times a user has clicked a button and how to save it. Android Studio

  15. 15

    Count how many times certain text combinations occurs in certain columns

  16. 16

    How to count how many times a meta_value appears in a column by certain meta_key?

  17. 17

    How to count how many times the user has typed the wrong answer (Python)

  18. 18

    How can I count how many rows since a value has changed in a column vector in R?

  19. 19

    Is it possible to count how many times a user has logged in MVC5 without a separate counter?

  20. 20

    Count How Many Times a Value Appears Php

  21. 21

    SQL COUNT how many times a value appears

  22. 22

    Count how many times a value appears in this array?

  23. 23

    Count how many number of times a value appears

  24. 24

    Find how many times the department of an employee has changed

  25. 25

    Check how many times a file has been changed

  26. 26

    Count how many times a word occurs in a HashMap per key

  27. 27

    Elasticsearch: I want to count how many times a particular value has appeared in particular field

  28. 28

    Recursion function to count how many times a certain sequence appears

  29. 29

    Look for a certain String inside another and count how many times it appears

HotTag

Archive