Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 比较不同时间戳的值并确定它们是否已更改_Mysql_Sql_Group By_Max_Min - Fatal编程技术网

Mysql 比较不同时间戳的值并确定它们是否已更改

Mysql 比较不同时间戳的值并确定它们是否已更改,mysql,sql,group-by,max,min,Mysql,Sql,Group By,Max,Min,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&

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");
预期结果

product          week_switch
Product_A           no
Product_B           yes
Product_C           yes
Product_D           no 

在上面的结果中,我想检查产品的
计划周
是否已从一个
时间戳
切换到另一个
时间戳

如果满足条件,则应将
用作值。如果不是,则应插入
no

SELECT
product
FROM operations 
GROUP BY 1;
我不知道我需要什么样的查询来实现这一点。
你有什么想法吗?

看起来你想要:

select
    product,
    case when min(plan_week) <> max(plan_week) then 'yes' else 'no' end as week_switch
from operations
where time_stamp in ('2020-01-01', '2020-03-15')
group by product
选择
产品,,
当最小值(计划周)最大值(计划周)时,则“是”或“否”结束为“周”开关
从操作
时间戳在哪里('2020-01-01','2020-03-15')
按产品分组
这将按产品聚合行。然后,
案例
表达式检查
计划周
中是否有(至少)两个不同的值,并相应地设置标志


where
子句不是严格必需的。您可以删除它,这取决于您是要检查两个特定日期还是整个数据集。

我认为这只是使用
case
表达式的聚合:

select product,
       (case when min(plan_week) = max(plan_week) then 'no' else 'yes' end) as flag
from operations o
group by product;