[常用脚本] 各产品型号连续三个月合格率低于0.98,sql怎么写

18187787596铂金一 显示全部楼层 发表于 2023-8-8 23:50:47 |阅读模式 打印 上一主题 下一主题
大佬们,请问一下      各产品型号,连续三月合格率低于0.98,自动预警,SQL怎么写?
谢谢!
QQ图片20230808234713.jpg

最佳答案

yhdata_yzm 关注Ta

2023-08-08 23:50:48

SELECT t1.month, t1.product, t1.rate FROM detection t1 JOIN detection t2 ON t1.product = t2.product AND t1.month = t2.month + 1 JOIN detection t3 ON t1.product = t3.product AND t1.month = t3.month + 2 WHERE t1.rate < 0.98 AND t2.rate < 0.98 AND t3.rate < 0.98; 这个你参考下呢
查看完整内容
回复

使用道具 举报

精彩评论6

yhdata_yzm 显示全部楼层 发表于 2023-8-8 23:50:48
SELECT t1.month, t1.product, t1.rate
FROM detection t1
JOIN detection t2 ON t1.product = t2.product AND t1.month = t2.month + 1
JOIN detection t3 ON t1.product = t3.product AND t1.month = t3.month + 2
WHERE t1.rate < 0.98 AND t2.rate < 0.98 AND t3.rate < 0.98;
这个你参考下呢
回复

使用道具 举报

jeff_lee钻石三 显示全部楼层 发表于 2023-8-9 00:19:35
自动预警?sql没这功能
回复

使用道具 举报

yanieye 显示全部楼层 发表于 2023-8-9 08:55:53
思路:
1. 第一个子查询,查询出图片中展示的数据;
2. 第二个子查询,增加一列colA, 将图片中的月份减一个月;
3. 第三个子查询,增加一列colB, 将图片中的月份减两个月;
4. 将三个查询通过left join关联起来,关联的列是 第一个子查询中的月份 第二个子查询中的colA,第三个子查询中的colB, 以及每个子查询中的产品型号列;
5. 在这个查询的基础上,加一个计算列,三个合格率的列都是低于0.98则返回1, 否则返回0.
回复

使用道具 举报

yh_L59CUoX6白银三 显示全部楼层 发表于 2023-8-9 20:39:28
可以试下这个思路思路:可以使用avg()+group by +having来判断,avg()+group by用于求每组产品的平均值,having用于判断每组的平均值是否低于0.98.
回复

使用道具 举报

yhdata_yzm 显示全部楼层 发表于 2023-8-10 10:30:36
写sql:
假设您的数据表为 `product_quality`,包含字段 `product_model`、`year`、`month` 和 `qualified_rate`,您可以使用以下 SQL 查询来标识各产品型号连续三个月合格率低于0.98的情况:

```sql
SELECT
    product_model,
    CASE WHEN COUNT(*) >= 3 THEN 0 ELSE 1 END AS flag
FROM (
    SELECT
        product_model,
        year,
        month,
        AVG(qualified_rate) AS avg_qualified_rate
    FROM product_quality
    GROUP BY product_model, year, month
) subquery
WHERE avg_qualified_rate < 0.98
GROUP BY product_model;
```

在这个查询中,我们首先使用子查询计算每个产品型号每个月的平均合格率。然后,在外部查询中,我们筛选出连续三个月合格率低于0.98的产品型号,并使用 `CASE` 表达式对结果进行标识,如果满足条件(低于0.98三个月及以上),则标识为 0,否则标识为 1。
然后用标识为0做预警
回复

使用道具 举报

18187787596铂金一 显示全部楼层 发表于 2023-8-16 21:48:41
本帖最后由 yhdata_aqziecpj 于 2023-8-16 21:49 编辑
yhdata_yzm 发表于 2023-8-10 10:30
写sql:
假设您的数据表为 `product_quality`,包含字段 `product_model`、`year`、`month` 和 `qualified_ ...

抱歉,之前没表达清楚。我想要的是产品合格率连续三月小于0.98的所有信息,不是平均合格率。如下:
比如甲产品,2,3,4连续三月合格率小于0.98,我要月份、产品、合格率三个字段,下图标黄部分


img


创表语句如下
create table detection(
    month int comment'月份',
    product char(2) comment'类型',
    rate double comment'合格率'
) comment'抽查表';


insert into detection values (1,'甲', 0.98);
insert into detection values (1,'乙', 0.99);
insert into detection values (1,'丙', 0.96);
insert into detection values (2,'甲', 0.97);
insert into detection values (2,'乙', 0.95);
insert into detection values (2,'丙', 0.96);
insert into detection values (3,'甲', 0.94);
insert into detection values (3,'乙', 0.99);
insert into detection values (3,'丙', 0.90);
insert into detection values (4,'甲', 0.97);
insert into detection values (4,'乙', 0.96);
insert into detection values (4,'丙', 0.89);
insert into detection values (5,'甲', 0.99);
insert into detection values (5,'乙', 0.92);
insert into detection values (5,'丙', 0.96);
insert into detection values (6,'甲', 0.97);
insert into detection values (6,'乙', 0.96);
insert i detectintoon values (6,'丙', 0.97);
251305701296158.png
回复

使用道具 举报

高级模式
您需要登录后才可以回帖 登录 | 免费注册

  • 官方微信

    欢迎关注永洪服务号!收费为0,价值无限

    扫码关注
  • 新浪微博

    让每位用户轻松挖掘数据价值!

    访问新浪微博
  • 智能客服
50W

会员等你来哦

Copyright   ©2012-2024  北京永洪商智科技有限公司  (京ICP备12050607) 京公网安备110110802011451号 |联系社区管理员|《永洪社区协议》
返回顶部