我试图展示制造过程的重要日期。有10个房间执行相同的过程。每次过程开始时,都会分配一个新的循环编号。我想显示每个房间当前(即最大)周期的重要日期。
到目前为止,我已经汇总了一个查询,它将显示整个最大周期数的重要日期(我的代码如下),但我想添加一个额外的标准,以便我看到最大周期数的信息。每个房间
SELECT
[dbo_batch_overview5].[rm],
[dbo_batch_overview5].[cyc],
[dbo_batch_overview5].[bpr],
[dbo_batch_overview5].[plug_date],
[dbo_batch_overview5].[trig_date],
[dbo_batch_overview5].[flush_date],
[dbo_batch_overview5].[harv_date]
FROM dbo_batch_overview5
WHERE ((([dbo_batch_overview5].[cyc])=(SELECT Max([dbo_batch_overview5].[cyc])
FROM [dbo_batch_overview5]
)));
我想我需要添加一个GROUP BY语句来指定我想要查看房间[rm]字段中每个唯一条目的最大循环数,这是我在我认为需要的语句中尝试的代码包括:
SELECT
[dbo_batch_overview5].[rm],
[dbo_batch_overview5].[cyc],
[dbo_batch_overview5].[bpr],
[dbo_batch_overview5].[plug_date],
[dbo_batch_overview5].[trig_date],
[dbo_batch_overview5].[flush_date],
[dbo_batch_overview5].[harv_date]
FROM dbo_batch_overview5
WHERE ((([dbo_batch_overview5].[cyc])=(SELECT Max([dbo_batch_overview5].[cyc])
FROM [dbo_batch_overview5]
GROUP BY [dbo_batch_overview5].[rm]
)));
当我尝试上面的代码时,我得到一个错误,说我的子查询返回了多个值。谁能告诉我我做错了什么?
根据要求,这里有一些示例数据
rm cyc bpr clone_date plug_date trig_date harv_date
1 13 20161031-OP 10/31/2016 11/16/2016 11/22/2016 1/12/2017
1 13 20161101-EV 11/1/2016 11/16/2016 11/22/2016 1/13/2017
1 13 20161031-CG 10/31/2016 11/16/2016 11/22/2016 1/13/2017
1 13 20161101-CB 11/1/2016 11/16/2016 11/22/2016 1/12/2017
1 13 20161031-VO 10/31/2016 11/16/2016 11/22/2016 1/13/2017
1 14 20170104-CG 1/4/2017 1/23/2017 1/28/2017
1 14 20170104-CB 1/4/2017 1/23/2017 1/28/2017
1 14 20170106-AV 1/6/2017 1/23/2017 1/28/2017
1 14 20170106-MN 1/6/2017 1/23/2017 1/28/2017
2 7 20150925-ST 9/25/2015 10/10/2015 10/19/2015 12/16/2015
2 7 20150924-AL 9/24/2015 10/10/2015 10/19/2015 12/16/2015
2 7 20150924-EA 9/24/2015 10/10/2015 10/19/2015 12/21/2015
2 7 20150928-LM 9/28/2015 10/10/2015 10/19/2015 12/22/2015
2 7 20150928-HM 9/28/2015 10/10/2015 10/19/2015 12/19/2015
2 8 20151214-CG 12/14/2015 12/30/2015 1/7/2016 3/14/2016
2 8 20151214-RM 12/14/2015 12/30/2015 1/7/2016 3/15/2016
2 8 20151215-CB 12/15/2015 12/30/2015 1/7/2016 3/8/2016
在上面的例子中,我只希望看到与房间1中的第14周期和第2房间的第8周期相关的记录
答案 0 :(得分:3)
这样的事情会解决你的问题吗?
SELECT
a.[rm],
a.[cyc],
a.[bpr],
a.[plug_date],
a.[trig_date],
a.[flush_date],
a.[harv_date]
FROM dbo_batch_overview5 a
INNER JOIN (SELECT Max([cyc]) AS maxcyc,
rm as rm2
FROM [dbo_batch_overview5]
GROUP BY [rm])c
ON a.rm = c.rm2 AND a.cyc = c.maxcyc