我有 2 个选择查询,我想将它们合并为一个没有重复项的查询。我还想使用在所有表中找到的 shiftindex 的主键加入它们。我该怎么做?
第一个选择语句是:
SELECT dbo.hist_exproot.year,
dbo.hist_exproot.[month#],
dbo.hist_exproot.month,
dbo.hist_exproot.day,
dbo.hist_exproot.[shift#],
dbo.hist_exproot.shift,
dbo.hist_loads.excav,
SUM(dbo.hist_loads.loadtons) AS 'TONS',
SUM(dbo.hist_loads.ex_tmcat00)/3600 AS 'TTOTAL',
SUM(dbo.hist_loads.ex_tmcat01+dbo.hist_loads.ex_tmcat02)/3600 AS 'T_EFECT'
FROM dbo.hist_exproot
INNER JOIN dbo.hist_loads
ON dbo.hist_exproot.shiftindex = dbo.hist_loads.shiftindex
WHERE (dbo.hist_exproot.year >= 16)
GROUP BY dbo.hist_exproot.year,
dbo.hist_exproot.[month#],
dbo.hist_exproot.month,
dbo.hist_exproot.day,
dbo.hist_exproot.[shift#],
dbo.hist_exproot.shift,
dbo.hist_loads.excav
二是:
SELECT dbo.hist_exproot.year,
dbo.hist_exproot.[month#],
dbo.hist_exproot.shiftdate,
dbo.hist_exproot.shift,
dbo.hist_statusevents.eqmt,
dbo.hist_exproot.crew,
dbo.hist_reasontable.category,
dbo.hist_statusevents.reason,
dbo.hist_reasontable.name,
dbo.hist_operlist.operid,
dbo.hist_statusevents.starttime,
dbo.hist_statusevents.endtime,
dbo.hist_statusevents.duration/3600 as 'Time',
dbo.hist_statusevents.comment
FROM dbo.hist_reasontable
INNER JOIN dbo.hist_statusevents
ON dbo.hist_reasontable.shiftindex = dbo.hist_statusevents.shiftindex
AND dbo.hist_reasontable.reason = dbo.hist_statusevents.reason
INNER JOIN dbo.hist_operlist
ON dbo.hist_statusevents.operid = dbo.hist_operlist.operid
AND dbo.hist_statusevents.shiftindex = dbo.hist_operlist.shiftindex
AND dbo.hist_reasontable.shiftindex = dbo.hist_operlist.shiftindex
INNER JOIN dbo.hist_exproot
ON dbo.hist_statusevents.shiftindex = dbo.hist_exproot.shiftindex
WHERE
(dbo.hist_exproot.year >= 16)
AND (dbo.hist_statusevents.unit = 2)
AND(dbo.hist_statusevents.eqmt <> 'L98')
AND (dbo.hist_statusevents.eqmt <> 'L96')
AND (dbo.hist_statusevents.eqmt <> 'L09')
AND (dbo.hist_statusevents.eqmt <> 'S47')
ORDER BY dbo.hist_exproot.shiftdate,
dbo.hist_statusevents.eqmt
如果您有任何疑问,请告诉我。
谢谢
执行您要求的操作的最简单方法是:
SELECT DISTINCT *
FROM (Query 1) JOIN
(Query 2) ON...
这两个子查询的连接将由您在 on 之后放置的内容处理,非重复查询将消除所有重复项。
这不是一个答案,但对于评论来说完全太长了。下面是使用别名时查询的外观。我还清理了一些其他格式,以便您可以看到它应该是什么样子。
SELECT he.year
, he.[month#]
, he.month
, he.day
, he.[shift#]
, he.shift
, hl.excav
, SUM(hl.loadtons) AS 'TONS'
, SUM(hl.ex_tmcat00)/3600 AS 'TTOTAL'
, SUM(hl.ex_tmcat01+hl.ex_tmcat02)/3600 AS 'T_EFECT'
FROM dbo.hist_exproot he
INNER JOIN dbo.hist_loads hl ON he.shiftindex = hl.shiftindex
WHERE he.year >= 16
GROUP BY he.year
, he.[month#]
, he.MONTH
, he.DAY
, he.[shift#]
, he.shift
, hl.excav
SELECT er.year
, er.[month#]
, er.shiftdate
, er.shift
, se.eqmt
, er.crew
, rt.category
, se.reason
, rt.name
, ol.operid
, se.starttime
, se.endtime
, se.duration/3600 as 'Time'
, se.comment
FROM dbo.hist_reasontable rt
INNER JOIN dbo.hist_statusevents se ON rt.shiftindex = se.shiftindex
AND rt.reason = se.reason
INNER JOIN dbo.hist_operlist ol ON se.operid = ol.operid
AND se.shiftindex = ol.shiftindex
AND rt.shiftindex = ol.shiftindex
INNER JOIN dbo.hist_exproot er ON se.shiftindex = er.shiftindex
WHERE er.year >= 16
AND se.unit = 2
AND se.eqmt <> 'L98'
AND se.eqmt <> 'L96'
AND se.eqmt <> 'L09'
AND se.eqmt <> 'S47'
ORDER BY er.shiftdate
, se.eqmt
我假设你使用的是SQL Server。
正如其他人所指出的,一种方法是使用 CTE SELECT DISTINCT column_list FROM CTE1 INNER JOIN CTE2 ON join_condition
,其中 CTE1 和 CTE2 是没有 ORDER BY
子句的原始查询。基于您的示例的连接列将dbo.hist_exproot.shiftindex
。
另一种方法是按如下方式连接所有表...
SELECT
he.year
, he.[month#]
, he.month
, he.day
, he.[shift#]
, he.shift
, hl.excav
, SUM(hl.loadtons) AS 'TONS'
, SUM(hl.ex_tmcat00)/3600 AS 'TTOTAL'
, SUM(hl.ex_tmcat01+hl.ex_tmcat02)/3600 AS 'T_EFECT'
, he.shiftdate
, se.eqmt
, he.crew
, rt.category
, se.reason
, rt.name
, ol.operid
, se.starttime
, se.endtime
, se.duration/3600 as 'Time'
, se.comment
FROM
dbo.hist_exproot he
INNER JOIN dbo.hist_loads hl ON he.shiftindex = hl.shiftindex
INNER JOIN dbo.hist_statusevents se ON se.shiftindex = he.shiftindex
INNER JOIN dbo.hist_reasontable rt ON rt.shiftindex = se.shiftindex
AND rt.reason = se.reason
INNER JOIN dbo.hist_operlist ol ON se.operid = ol.operid
AND se.shiftindex = ol.shiftindex
AND rt.shiftindex = ol.shiftindex
WHERE
he.year >= 16
AND se.unit = 2
AND se.eqmt <> 'L98'
AND se.eqmt <> 'L96'
AND se.eqmt <> 'L09'
AND se.eqmt <> 'S47'
GROUP BY
he.year
, he.[month#]
, he.MONTH
, he.DAY
, he.[shift#]
, he.shift
, hl.excav
ORDER BY
he.shiftdate
, se.eqmt
如果要从结果集中消除任何可能的重复项,请使用 DISTINCT
子句。但是,如果您不需要它,最好避免。
连接所有表的第二个解决方案会导致更复杂的查询,并且可能没有最佳查询计划。我会尝试这两种方法并使用更有效的方法。
感谢 Sean Lange 重新格式化原始查询。