注册 登录
编程论坛 Delphi论坛

SQL如何查询到最大连续的数据?

chinateam 发布于 2020-07-14 09:03, 1767 次点击
AutoId       订单编号              不合格            时间
1                  A                            1                  2014-11-1
2                  B                            0                 2014-11-2
3                  A                            1                  2014-11-3
4                  A                            1                 2014-11-4
5                  A                           0                2014-11-5
6                  B                           1                2014-11-6


统计 同一订单的连续合格的最大个数的数据,结果如下:
编号             订单编号              不合格                    时间           
3                  A                            1                  2014-11-3
4                  A                            1                 2014-11-3
5                  A                           1                2014-11-4
6                  B                            1                2014-11-6
1 回复
#2
fhfanxin2020-07-19 16:14
不清楚您的具体需求

个人理解是这样的:

SELECT * FROM table_name WHERE [订单编号]='A' ORDER BY [不合格] LIMIT 1;

获取到同一订单合格数最大的第一条数据(有些数据库使用TOP,FETCH FIRST  [ count ]  ROW  ONLY,自行查询相关帮助文件);

然后使用以下SQL获取相同合格数的数据:

SELECT * FROM table_name WHERE [订单编号]='A' AND [不合格]=:v1;

:v1是使用控件获取,设置的变量,或者直接使用以下SQL命令:

SELECT * FROM table_name WHERE [订单编号]='A' AND [不合格]=(SELECT [不合格] FROM table_name WHERE [订单编号]='A' ORDER BY [不合格] LIMIT 1);
1