我现有的查询工作正常:
SELECT data_tool.name as tool,
MIN(data_cst.date_time) "start",
MAX(data_cst.date_time) "end",
data_cst.recipe_id,
data_target.name as target,
data_lot.name as lot,
data_wafer.name as wafer,
data_measparams.name as mp
FROM data_cst
INNER JOIN data_tool ON data_tool.id = data_cst.tool_id
INNER JOIN data_target ON data_target.id = data_cst.target_name_id
INNER JOIN data_lot ON data_lot.id = data_cst.lot_id
INNER JOIN data_wafer ON data_wafer.id = data_cst.wafer_id
INNER JOIN data_measparams ON data_measparams.id = data_cst.meas_params_name_id
WHERE data_target.id IN (130, 539)
AND data_cst.date_time BETWEEN '2010-01-11 00:00:00' AND '2013-01-11 23:59:59'
AND data_cst.tool_id IN (14,16)
GROUP BY wafer_id, data_cst.lot_id, data_file_id, target_name_id
HAVING count(*) < 100
ORDER BY start, tool
现在我需要添加一些东西。我有另一张桌子叫
event_message_idx
列recipe_id
,lot_id
,tool_id
,
date_time
和message_idx
。
我需要找出该表中有多少行message_idx = 'OM'
以及有多少人message_idx = 'SEM'
加入了上述查询
recipe_id, lot_id, tool_id
并且date_time between start and end
。
我无法弄清楚如何在一个查询中执行此操作(其中 我真的更喜欢子查询,因为这些表非常大 过去,该系统的子查询性能一直不佳。)
我一直在玩这样的左连接:
SELECT data_tool.name as tool,
MIN(data_cst.date_time) "start",
MAX(data_cst.date_time) "end",
data_cst.recipe_id,
data_target.name as target,
data_lot.name as lot,
data_wafer.name as wafer,
data_measparams.name as mp,
event_message_idx.message_idx,
COUNT(event_message_idx.message_idx)
FROM data_cst
LEFT JOIN event_message_idx
ON event_message_idx.recipe_id = data_cst.recipe_id
AND event_message_idx.message_idx IN ('OM', 'SEM')
AND event_message_idx.lot_id = data_cst.lot_id
AND event_message_idx.tool_id = data_cst.tool_id
INNER JOIN data_tool ON data_tool.id = data_cst.tool_id
INNER JOIN data_target ON data_target.id = data_cst.target_name_id
INNER JOIN data_lot ON data_lot.id = data_cst.lot_id
INNER JOIN data_wafer ON data_wafer.id = data_cst.wafer_id
INNER JOIN data_measparams ON data_measparams.id = data_cst.meas_params_name_id
WHERE data_target.id IN (130, 539)
AND data_cst.date_time BETWEEN '2010-01-11 00:00:00' AND '2013-01-11 23:59:59'
AND data_cst.tool_id IN (14,16)
GROUP BY wafer_id, data_cst.lot_id, data_file_id, target_name_id,
event_message_idx.message_idx
HAVING count(*) < 100
ORDER BY start, tool
但这里有两个问题:
我不希望这样 - 我只想知道有多少OM和SEM行 有(真的我只是想知道是否有0或更多0 - 实际数量并不重要。)
我没有考虑日期范围 - 我只想数数 event_message_idx中的行在开始和结束之间,我不能 弄清楚如何做到这一点。
这可能吗?我认为它不是,我必须使用2个查询(这将使应用程序真正复杂化)或子查询(我也在努力编写)。
答案 0 :(得分:2)
计算子查询中的那些,例如
SELECT data_tool.name as tool,
MIN(data_cst.date_time) "start",
MAX(data_cst.date_time) "end",
data_cst.recipe_id,
data_target.name as target,
data_lot.name as lot,
data_wafer.name as wafer,
data_measparams.name as mp,
COALESCE(a.totalOM, 0) totalOM,
COALESCE(a.totalSEM, 0) totalSEM
FROM data_cst
INNER JOIN data_tool
ON data_tool.id = data_cst.tool_id
INNER JOIN data_target
ON data_target.id = data_cst.target_name_id
INNER JOIN data_lot
ON data_lot.id = data_cst.lot_id
INNER JOIN data_wafer
ON data_wafer.id = data_cst.wafer_id
INNER JOIN data_measparams
ON data_measparams.id = data_cst.meas_params_name_id
LEFT JOIN
(
SELECT recipe_id, lot_id, tool_id,
SUM(CASE WHEN message_idx = 'OM' THEN 1 ELSE 0 END) totalOM,
SUM(CASE WHEN message_idx = 'SEM' THEN 1 ELSE 0 END) totalSEM
FROM event_message_idx
WHERE date_time BETWEEN '2010-01-11 00:00:00' AND '2013-01-11 23:59:59'
GROUP BY recipe_id, lot_id, tool_id
) a ON data_cst.recipe_id = a.recipe_id AND
data_cst.lot_id = a.lot_id AND
data_cst.tool_id = a.tool_id
WHERE data_target.id IN (130, 539) AND
(data_cst.date_time BETWEEN '2010-01-11 00:00:00' AND '2013-01-11 23:59:59') AND
data_cst.tool_id IN (14,16)
GROUP BY wafer_id, data_cst.lot_id, data_file_id, target_name_id
HAVING COUNT(*) < 100
ORDER BY `start`, tool