简体   繁体   English

使用查找表SQL将3个sql视图合并为1个查询

[英]Merging 3 sql views into 1 query using a look up table SQL

I need to sum estimated minutes of time from 3 different queries into 1 sql query. 我需要将3个不同查询的估计时间总计为1个sql查询。 I have 3 views (one for parts cut on Machine A by job, one for parts cut on Machine B by job, and one cut on Machine C by job). 我有3个视图(一个用于按作业在Machine A上切割的零件,一个用于按作业在Machine B上切割的零件,一个用于按作业在Machine C上切割的零件)。 I have another table that has a list of all the parts that have an estimated cut time, we will call TableA. 我还有另一个表,其中列出了所有具有估计切割时间的零件,我们将其称为TableA。 I have written three different queries using the views that left join onto TableA to sum the cut times. 我使用离开联接到TableA上的视图来编写三个不同的查询来求和切割时间。 However, I want a query that returns a table with a list of jobs and the estimated cut time for each job by machine. 但是,我需要一个查询,该查询返回一个表,该表包含作业列表和按机器列出的每个作业的估计切割时间。 So each row would have the job followed by three estimated cut times broken down by each machine. 因此,每一行都会有工作,然后是每台机器细分的三个估计的裁切时间。

 --------Machine A-------
 SELECT 
 sum(TableA.[minutes]) as 'total', MachineA.JobNum
 FROM MachineA
 LEFT JOIN TableA on
 TableA.Part = MachineA.PART
 WHERE TableA.OP = 10 and OP_DISABLE IS NULL
 group by MachineA.Job

 --------Machine B-------
 SELECT 
 sum(TableA.[minutes]) as 'total', MachineB.JobNum
 FROM MachineB
 LEFT JOIN TableA on
 TableA.Part = MachineB.PART
 WHERE TableA.OP = 10 and OP_DISABLE IS NULL
 group by MachineB.Job

 --------Machine C-------
 SELECT 
 sum(TableA.[minutes]) as 'total', MachineC.JobNum
 FROM MachineC
 LEFT JOIN TableA on
 TableA.Part = MachineC.PART
 WHERE TableA.OP = 10 and OP_DISABLE IS NULL
 group by MachineC.Job

Example Table I am looking for 我要查找的示例表

 Job | MachinA_Time | MachineB_Time | MachineC_Time|
 ----------------------------------------------------
 123 | 345          | 512           | 452          |
 124 | 215          | 351           | 356

With UNION you can do this: 使用UNION您可以执行以下操作:

SELECT JobNum
  ,MAX(MachinA_Time) AS MachinA_Time
  ,MAX(MachinB_Time) AS MachinB_Time
  ,MAX(MachinC_Time) AS MachinC_Time
FROM
(

    SELECT MachineA.JobNum, sum(TableA.[minutes]) as MachinA_Time, NULL AS MachinB_Time, NULL AS MachinA_Time
     FROM MachineA 
    LEFT JOIN TableA on TableA.Part = MachineA.PART
     WHERE TableA.OP = 10 and OP_DISABLE IS NULL
     group by MachineA.Job
    UNION ALL
     SELECT MachineB.JobNum, NULL, sum(TableA.[minutes]), NULL
     FROM MachineB
     LEFT JOIN TableA on
     TableA.Part = MachineB.PART
     WHERE TableA.OP = 10 and OP_DISABLE IS NULL
     group by MachineB.Job
    UNION ALL
     SELECT MachineC.JobNum, NULL, NULL, sum(TableA.[minutes])
     FROM MachineC
     LEFT JOIN TableA on
     TableA.Part = MachineC.PART
     WHERE TableA.OP = 10 and OP_DISABLE IS NULL
     group by MachineC.Job

) AS t
GROUP BY JobNum

I think you can save unnecessary joins by using conditional aggregation and UNION , since the format is the same 我认为您可以通过使用条件聚合和UNION来节省不必要的联接,因为格式相同

SELECT p.jobnum,
       SUM(CASE WHEN t.machine = 'A' THEN t.[minutes] END) as machineA_Time,
       SUM(CASE WHEN t.machine = 'B' THEN t.[minutes] END) as machineB_Time,
       SUM(CASE WHEN t.machine = 'C' THEN t.[minutes] END) as machineC_Time
FROM(SELECT s.minutes,s.part,'A' as machine FROM MachineA s
     UNION ALL 
     SELECT s.minutes,s.part,'B' FROM MachineB s
     UNION ALL 
     SELECT s.minutes,s.part,'C' FROM MachineC s) t
JOIN TableA p
 ON(p.part = t.part)
WHERE p.op = 10
GROUP BY p.jobnum

This query may need some extra adjustments, as I don't know where OP_DISABLE IS NULL came from . 该查询可能需要一些额外的调整,因为我不知道OP_DISABLE IS NULL来自何处。

Just join each result based on the sharing job#: 只需根据共享作业#加入每个结果:

WITH MachineA
AS
(
 SELECT 
 sum(TableA.[minutes]) as 'total', MachineA.JobNum
 FROM MachineA
 LEFT JOIN TableA on
 TableA.Part = MachineA.PART
 WHERE TableA.OP = 10 and OP_DISABLE IS NULL
 group by MachineA.Job
 ),
 MachineB
 as
 (-
 SELECT 
 sum(TableA.[minutes]) as 'total', MachineB.JobNum
 FROM MachineB
 LEFT JOIN TableA on
 TableA.Part = MachineB.PART
 WHERE TableA.OP = 10 and OP_DISABLE IS NULL
 group by MachineB.Job
 ),
  MachineC
 as
(
 SELECT 
 sum(TableA.[minutes]) as 'total', MachineC.JobNum
 FROM MachineC
 LEFT JOIN TableA on
 TableA.Part = MachineC.PART
 WHERE TableA.OP = 10 and OP_DISABLE IS NULL
 group by MachineC.Job
 )

 SELECT A.JobNum as Job, MachineA.total as MachineATime,  MachineB.total as MachineBTime, MachineC.total as MachineCTime
 FROM MachineA as A
 INNER JOIN MachineB as B
 ON B.JobNum = A.JobNum
 INNER JOIN MachineC as C
 ON C.JobNum = A.JobNum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM