Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

mysql - Compare values of timestamps and assign a value to each of them in case they have changed

DB-Fiddle

CREATE TABLE operations (
    id int auto_increment primary key,
    time_stamp DATE,
    product VARCHAR(255),
    plan_week VARCHAR(255)
);

INSERT INTO operations (time_stamp, product, plan_week)
VALUES 
("2020-01-01", "Product_A", "CW01"),
("2020-01-01", "Product_B", "CW01"),
("2020-01-01", "Product_C", "CW01"),

("2020-03-15", "Product_A", "CW01"),
("2020-03-15", "Product_B", "CW02"),
("2020-03-15", "Product_C", "CW02"),
("2020-03-15", "Product_D", "CW01");

Expected Result:

    time_stamp   |   product     |   plan_week   |   week_switch
  ---------------|---------------|---------------|----------------- 
    2020-01-01   |   Product_A   |     CW01      |       no
    2020-01-01   |   Product_B   |     CW01      |       yes
    2020-01-01   |   Product_C   |     CW01      |       yes
                 |               |               |
    2020-03-15   |   Product_A   |     CW01      |       no
    2020-03-15   |   Product_B   |     CW02      |       yes
    2020-03-15   |   Product_C   |     CW02      |       yes
    2020-03-15   |   Product_D   |     CW01      |       no

In the above result I want to list all products from the table and compare the two time_stamps to each other.
If the plan_week of one product has switched between the both time_stamps I want that in the additional column called week_switch the word yes gets inserted and if not the word no gets inserted.

I tried to go with this query but could not make it work:

SELECT time_stamp, product, plan_week,
        (CASE WHEN MIN(plan_week) <> MAX(plan_week) THEN 'yes' ELSE 'no' END) AS week_switch
FROM operations
GROUP BY 1,2;

What do I need to change to get the expected result?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I understand your requirement correctly, your week_switch result row depends on just your product, not its timestamp.

So compute it in a subquery.

  SELECT product,
         (CASE 
          WHEN MIN(plan_week) <> MAX(plan_week) THEN 'yes' 
          ELSE 'no' END)  AS week_switch
    FROM operations
   GROUP BY product

Then JOIN that subquery to your details (fiddle).

WITH switched AS (
  SELECT product,
         CASE
         WHEN MIN(plan_week) <> MAX(plan_week) THEN 'yes'
         ELSE 'no' END AS week_switch
    FROM operations
   GROUP BY product
)
SELECT
operations.time_stamp,
operations.product,
operations.plan_week,
switched.week_switch
FROM operations
LEFT JOIN switched ON operations.product = switched.product
WHERE time_stamp in ('2020-01-01', '2020-03-15')
GROUP BY 1,2;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...