加入2个不等的sql查询

时间:2017-06-19 21:31:55

标签: mysql sql-server tsql

我有两个选择的查询要加入一个没有重复的查询。此外,我想使用他们在所有表中找到的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

如果您有任何疑问,请与我们联系。

谢谢

3 个答案:

答案 0 :(得分:1)

做出要求的最简单方法是:

SELECT DISTINCT *
FROM (Query 1) JOIN
(Query 2) ON...

这两个子查询的连接将由您在开启后放置的内容处理,而distinct将消除所有重复项。

答案 1 :(得分:1)

这不是答案,但对评论来说太长了。如果您使用别名,这是您的查询的样子。我还清理了其他一些格式,以便你可以看到它应该是什么样的。

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

答案 2 :(得分:0)

我假设您使用的是SQL Server。

正如其他人所指出的,一种方法是使用像JIT这样的CTE,CTE1和CTE2是没有SELECT DISTINCT column_list FROM CTE1 INNER JOIN CTE2 ON join_condition子句的原始查询。基于您的示例的加入列将是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 子句。但如果你不需要,那就避免它。

连接所有表的第二个解决方案会导致更复杂的查询,并且可能没有最佳查询计划。我会尝试两种方法,并使用效率更高的方法。

感谢Sean Lange重新格式化原始查询。

相关问题