- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试编写一个包含 2 个子查询的查询。这些查询在单独运行时有效,但是当我将它们放在一起时,我没有得到所需的结果集。我将尝试举一个最小的例子。
主要查询:
mysql> select target_name_id, ep, count(*), count(distinct wafer_id)
from data_cst
where target_name_id = 155609
and data_cst.date_time BETWEEN '2012-06-23 00:00:00' AND '2012-08-23 23:59:59'
GROUP BY target_name_id, data_cst.ep;
+----------------+------+----------+--------------------------+
| target_name_id | ep | count(*) | count(distinct wafer_id) |
+----------------+------+----------+--------------------------+
| 155609 | Line | 4799 | 215 |
+----------------+------+----------+--------------------------+
1 row in set (0.05 sec)
第一个子查询:
mysql> SELECT target_name_id,ep, wafer_id, AVG(bottom) as averages,
FROM data_cst
WHERE target_name_id = 155609
AND data_cst.date_time BETWEEN '2012-06-23 00:00:00' AND '2012-08-23 23:59:59'
GROUP BY target_name_id, ep, wafer_id;
+----------------+------+----------+------------+
| target_name_id | ep | wafer_id | averages |
+----------------+------+----------+------------+
| 155609 | Line | 401739 | 47.6236667 |
| 155609 | Line | 403041 | 47.3739167 |
| 155609 | Line | 408339 | 47.4901667 |
| 155609 | Line | 409683 | 48.3066250 |
| 155609 | Line | 409690 | 47.2402500 |
| 155609 | Line | 410249 | 47.3346667 |
| 155609 | Line | 410633 | 48.7373333 |
| 155609 | Line | 414000 | 48.1274167 |
.
.
.
215 rows in set (0.07 sec)
第二个子查询:
mysql> SELECT target_name_id, ep, data_file_id, lot_id, wafer_id,
date_time,
COUNT(DISTINCT target_name_id, ep, lot_id, data_file_id)-1 as reruns
FROM data_cst
WHERE target_name_id = 155609
AND data_cst.date_time BETWEEN '2012-06-23 00:00:00' AND '2012-08-23 23:59:59'
GROUP BY target_name_id, data_cst.ep, wafer_id
HAVING COUNT(DISTINCT target_name_id, ep, lot_id, data_file_id) > 1;
+----------------+------+--------------+--------+----------+---------------------+--------+
| target_name_id | ep | data_file_id | lot_id | wafer_id | date_time | reruns |
+----------------+------+--------------+--------+----------+---------------------+--------+
| 155609 | Line | 774 | 120804 | 403041 | 2012-07-06 03:51:50 | 1 |
| 155609 | Line | 6502 | 123109 | 409683 | 2012-07-16 05:10:04 | 1 |
| 155609 | Line | 749 | 120804 | 409690 | 2012-07-06 04:08:01 | 1 |
| 155609 | Line | 3319148 | 123484 | 410633 | 2012-07-07 09:12:20 | 5 |
| 155609 | Line | 8264 | 134609 | 414098 | 2012-07-03 11:34:12 | 5 |
| 155609 | Line | 3279867 | 124752 | 414245 | 2012-06-26 00:51:31 | 1
.
.
.
93 rows in set (0.06 sec)
现在,当我将它们放在一起时,我需要主查询的计数、第二个的平均值以及第三个重新运行列的总和。我已经搞砸了 3 天,我无法提出正确的连接来获得我想要的结果。我已经能够得出正确的总和、计数或平均值,但不是全部 3 个。这是我最近的尝试:
mysql> select data_cst.target_name_id, data_cst.ep, count(*) as count,
count(distinct data_cst.wafer_id) as wafers,
avg(averages) as average, sum(reruns) as rerun
from data_cst,
(SELECT target_name_id,ep, wafer_id, AVG(bottom) as averages
FROM data_cst
WHERE target_name_id = 155609
AND data_cst.date_time BETWEEN '2012-06-23 00:00:00' AND '2012-08-23 23:59:59'
GROUP BY target_name_id, ep, wafer_id) q1,
(SELECT target_name_id, ep, data_file_id, lot_id, wafer_id,
date_time,
COUNT(DISTINCT target_name_id, ep, lot_id, data_file_id)-1 as reruns
FROM data_cst
WHERE target_name_id = 155609
AND data_cst.date_time BETWEEN '2012-06-23 00:00:00' AND '2012-08-23 23:59:59'
GROUP BY target_name_id, data_cst.ep, wafer_id
HAVING COUNT(DISTINCT target_name_id, ep, lot_id, data_file_id) > 1) r
where data_cst.target_name_id = 155609
AND data_cst.date_time BETWEEN '2012-06-23 00:00:00' AND '2012-08-23 23:59:59'
and data_cst.wafer_id = q1.wafer_id
GROUP BY target_name_id, data_cst.ep;
+----------------+------+--------+--------+----------------+--------+
| target_name_id | ep | count | wafers | average | rerun |
+----------------+------+--------+--------+----------------+--------+
| 155609 | Line | 446307 | 215 | 48.12877962148 | 724649 |
+----------------+------+--------+--------+----------------+--------+
1 row in set (23.56 sec)
有了这个最外层的 where 子句,晶圆计数和平均值是正确的,但计数和重新运行是错误的。我可以使用不同的 where 子句并使重新运行正确,但计数和晶圆都是错误的。我还可以使用另一个不同的 where 子句并得到正确的计数,但是重新运行是错误的。
我已经搞砸了 3 天,我就是找不到适合我的 where 子句。
这是对我的问题的更新:
我按照 Gordon Linoff 的建议修改了我的查询,此后客户添加了很多新要求,我已经能够将这些要求合并到查询中。但现在他们添加了一些我不太清楚如何处理它。
我的查询现在看起来像这样:
SELECT data_target.name as Target,
q1.ep as EP,
COUNT(*) as Wafers,
Lots,
SUM(numonep)/(COUNT(*)+SUM(CASE WHEN reruns > 0 THEN reruns ELSE 0 END)) as 'Sites/Wafer',
MAX(LastRun) as "Last Run",
SUM(CASE WHEN reruns > 0 THEN reruns ELSE 0 END) as Rerun,
COUNT(*)+SUM(CASE WHEN reruns > 0 THEN reruns ELSE 0 END) as Runs,
avgbottom as "Avg Bottom",
3*stdbottom as "3 Sig",
maxbottom as Max,
minbottom as Min,
SUM(numonep) as Count,
SUM(numonep) - SUM(numbottoms) as NAs,
100-((SUM(numonep) - SUM(numbottoms))/SUM(numonep)*100) as "% Success",
3*stdbottom/avgbottom as "3Sig/Avg",
AVG(avgbottom) as 'Wafer Avg',
AVG(Wafer3Sigma) as 'Wafer 3 Sigma',
AVG(Ranges) as 'Avg Range',
3*STD(Ranges) as '3Sig of Ranges',
MAX(Ranges) as 'Max Range',
MIN(Ranges) as 'Min Range',
(SUM(numonep) - SUM(numbottoms))/COUNT(*) as 'NAs/Wafer'
FROM (SELECT target_name_id,
ep,
wafer_id,
COUNT(bottom) as numbottoms,
AVG(bottom) as avgbottom,
STD(bottom) as stdbottom,
MAX(bottom) as maxbottom,
MIN(bottom) as minbottom,
MAX(date_time) as "LastRun",
COUNT(*) as numonep,
COUNT(DISTINCT target_name_id, ep, lot_id, data_file_id)-1 as reruns,
COUNT(DISTINCT(lot_id)) as Lots,
3*STD(bottom) as Wafer3Sigma,
MAX(bottom) - MIN(bottom) as Ranges
FROM data_cst
WHERE target_name_id IN (775, 776, 777, 778, 779, 780, 45, 44, 116, 117, 118, 119, 120, 121)
AND data_cst.date_time BETWEEN '2010-03-04 00:00:00' AND '2010-03-04 23:59:59'
GROUP BY target_name_id, ep, wafer_id
HAVING count(*) < 999) q1,
data_target
WHERE data_target.id = target_name_id
GROUP BY q1.target_name_id, q1.ep;
这非常有效。但是现在他们要我得到一个特定的列(image_measurer_id),返回的每一行对应于具有 bottom = Min(bottom),bottom = Max(bottom),bottom 最接近 Avg(bottom)和date_time = Max(date_time) 行的底部。
这甚至可以通过这个查询实现吗?
最佳答案
如果您使用正确的连接语法,将会对您有很大帮助。加入 data_cst 似乎是多余的。如果您的两个子查询具有相同的目标和晶片,那么简单的连接或左外部连接应该可以工作:
select q1.target_name_id, q1.ep, count(*) as count,
count(distinct q1.wafer_id) as wafers,
avg(averages) as average, sum(reruns) as rerun
from (SELECT target_name_id,ep, wafer_id, AVG(bottom) as averages
FROM data_cst
WHERE target_name_id = 155609
AND data_cst.date_time BETWEEN '2012-06-23 00:00:00' AND '2012-08-23 23:59:59'
GROUP BY target_name_id, ep, wafer_id) q1 left outer join
(SELECT target_name_id, ep, data_file_id, lot_id, wafer_id,
date_time,
COUNT(DISTINCT target_name_id, ep, lot_id, data_file_id)-1 as reruns
FROM data_cst
WHERE target_name_id = 155609
AND data_cst.date_time BETWEEN '2012-06-23 00:00:00' AND '2012-08-23 23:59:59'
GROUP BY target_name_id, data_cst.ep, wafer_id
HAVING COUNT(DISTINCT target_name_id, ep, lot_id, data_file_id) > 1
) r
on r.wafer_id = q1.wafer_id and
r.target_name_id = q1.target_name_id and
r.ep = q1.ep
GROUP BY q1.target_name_id, q1.ep;
但是,您的查询非常相似,所以我认为您可以简化逻辑:
select q1.target_name_id, q1.ep, sum(numonep) as count,
count(*) as wafers,
avg(averages) as average,
sum(case when reruns > 0 then reruns else 0 end) as rerun
from (SELECT target_name_id, ep, wafer_id, AVG(bottom) as averages,
count(*) as numonep,
COUNT(DISTINCT target_name_id, ep, lot_id, data_file_id)-1 as reruns
FROM data_cst
WHERE target_name_id = 155609 and
data_cst.date_time BETWEEN '2012-06-23 00:00:00' AND '2012-08-23 23:59:59'
GROUP BY target_name_id, ep, wafer_id
) q1
group by q1.target_name_id, q1.ep
关于mysql - 从子查询中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12223305/
我有三张 table 。表 A 有选项名称(即颜色、尺寸)。表 B 有选项值名称(即蓝色、红色、黑色等)。表C通过将选项名称id和选项名称值id放在一起来建立关系。 我的查询需要显示值和选项的名称,而
在mysql中,如何计算一行中的非空单元格?我只想计算某些列之间的单元格,比如第 3-10 列之间的单元格。不是所有的列...同样,仅在该行中。 最佳答案 如果你想这样做,只能在 sql 中使用名称而
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
我正在为版本7.6进行Elasticsearch查询 我的查询是这样的: { "query": { "bool": { "should": [ {
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
是否可以编写一个查询来检查任一子查询(而不是一个子查询)是否正确? SELECT * FROM employees e WHERE NOT EXISTS (
我找到了很多关于我的问题的答案,但问题没有解决 我有表格,有数据,例如: Data 1 Data 2 Data 3
以下查询返回错误: 查询: SELECT Id, FirstName, LastName, OwnerId, PersonEmail FROM Account WHERE lower(PersonEm
以下查询返回错误: 查询: SELECT Id, FirstName, LastName, OwnerId, PersonEmail FROM Account WHERE lower(PersonEm
我从 EditText 中获取了 String 值。以及提交查询的按钮。 String sql=editQuery.getText().toString();// SELECT * FROM empl
我有一个或多或少有效的查询(关于结果),但处理大约需要 45 秒。这对于在 GUI 中呈现数据来说肯定太长了。 所以我的需求是找到一个更快/更高效的查询(几毫秒左右会很好)我的数据表大约有 3000
这是我第一次使用 Stack Overflow,所以我希望我以正确的方式提出这个问题。 我有 2 个 SQL 查询,我正在尝试比较和识别缺失值,尽管我无法将 NULL 字段添加到第二个查询中以识别缺失
什么是动态 SQL 查询?何时需要使用动态 SQL 查询?我使用的是 SQL Server 2005。 最佳答案 这里有几篇文章: Introduction to Dynamic SQL Dynami
include "mysql.php"; $query= "SELECT ID,name,displayname,established,summary,searchlink,im
我有一个查询要“转换”为 mysql。这是查询: select top 5 * from (select id, firstName, lastName, sum(fileSize) as To
通过我的研究,我发现至少从 EF 4.1 开始,EF 查询上的 .ToString() 方法将返回要运行的 SQL。事实上,这对我来说非常有用,使用 Entity Framework 5 和 6。 但
我在构造查询来执行以下操作时遇到问题: 按activity_type_id过滤联系人,仅显示最近事件具有所需activity_type_id或为NULL(无事件)的联系人 表格结构如下: 一个联系人可
如何让我输入数据库的信息在输入数据 5 分钟后自行更新? 假设我有一张 table : +--+--+-----+ |id|ip|count| +--+--+-----+ |
我正在尝试搜索正好是 4 位数字的 ID,我知道我需要使用 LENGTH() 字符串函数,但找不到如何使用它的示例。我正在尝试以下(和其他变体)但它们不起作用。 SELECT max(car_id)
我有一个在 mysql 上运行良好的 sql 查询(查询 + 连接): select sum(pa.price) from user u , purchase pu , pack pa where (
我是一名优秀的程序员,十分优秀!