如何将我的两个查询合并为1?

时间:2018-02-22 23:41:47

标签: sql-server tsql

所以我有:

SELECT
  od.PartNo,
  CAST(od.PartDesc AS NVARCHAR(MAX)) AS [PartDesc],
  MAX(CAST(t.TicketDate AS DATETIME)) AS [EndDate]
FROM TimeTicketDet t JOIN OrderDet od ON t.JobNo = od.JobNo
  JOIN OrderRouting r ON t.JobNo = r.JobNo
WHERE t.WorkCntr = 500
  AND r.Status = 'Finished'
GROUP BY od.PartNo, CAST(od.PartDesc AS NVARCHAR(MAX))
ORDER BY MIN(CAST(t.TicketDate AS DATETIME)) DESC

SELECT
  od.PartNo,
  CAST(od.PartDesc AS NVARCHAR(MAX)) AS [PartDesc],
  MIN(CAST(t.TicketDate AS DATETIME)) AS [StartDate]
FROM TimeTicketDet t JOIN OrderDet od ON t.JobNo = od.JobNo
  JOIN OrderRouting r ON t.JobNo = r.JobNo
WHERE t.WorkCntr = 100
GROUP BY od.PartNo, CAST(od.PartDesc AS NVARCHAR(MAX))
ORDER BY MIN(CAST(t.TicketDate AS DATETIME)) DESC

我需要MIN和MAX列在1个查询中,但因为我在WHERE子句中引用了2个不同的工作中心,所以我不确定如何去做。我试过这个:

SELECT
  od.PartNo,
  CAST(od.PartDesc AS NVARCHAR(MAX)) AS [PartDesc],
    (SELECT
       MIN(CAST(t.TicketDate AS DATETIME)) AS [StartDate]
     FROM TimeTicketDet t JOIN OrderDet od ON t.JobNo = od.JobNo
       JOIN OrderRouting r ON t.JobNo = r.JobNo
     WHERE t.WorkCntr = 100
     GROUP BY od.PartNo
     ),
  MAX(CAST(t.TicketDate AS DATETIME)) AS [EndDate]
FROM TimeTicketDet t JOIN OrderDet od ON t.JobNo = od.JobNo
  JOIN OrderRouting r ON t.JobNo = r.JobNo
WHERE t.WorkCntr = 500
  AND r.Status = 'Finished'
GROUP BY od.PartNo, CAST(od.PartDesc AS NVARCHAR(MAX))
ORDER BY MIN(CAST(t.TicketDate AS DATETIME)) DESC
除了MIN列只是系统中的第一个日期之外,它几乎起作用了,它与PartNo / Workcenter没有关系,所以我被卡住了。我不确定我是否关闭,或者我是否应该使用像CTE这样的其他东西

提前致谢

2 个答案:

答案 0 :(得分:1)

如果您希望将开始和结束视为单独的列,那么case表达式将允许您在更改where条件后选择适当的值以允许两组行通过。如果所需的开始日期总是早于最终结束日期,正如我想象的那样,你可能甚至不需要那么花哨。你确实提到在第二个结果中有更多行,所以我不确定它来自哪里,它可能会改变逻辑。

在这种情况下,主要是使用OR

SELECT
  od.PartNo,
  MIN(CAST(od.PartDesc AS NVARCHAR(MAX))) AS PartDesc,
  MIN(CASE WHEN t.WorkCntr  = 100 THEN CAST(t.TicketDate AS DATETIME) END)) AS StartDate,
  MAX(CASE WHEN t.WorkCntr <> 100 THEN CAST(t.TicketDate AS DATETIME) END)) AS EndDate
FROM
    TimeTicketDet t
    INNER JOIN OrderDet od ON t.JobNo = od.JobNo
    INNER JOIN OrderRouting r ON t.JobNo = r.JobNo
WHERE t.WorkCntr = 100 OR t.WorkCntr = 500 AND r.Status = 'Finished'
GROUP BY od.PartNo
ORDER BY PartDesc DESC;

注意:我选择了结束日期条件,因为它们彼此相反,对我来说它更明显地表明发生了什么。

顺便说一句,如果您将子查询与之相关联,那么您可以使用子查询:

...
(
    SELECT MIN(CAST(t.TicketDate AS DATETIME))
    FROM OrderDet od2 INNER JOIN TimeTicketDet t
        ON t.JobNo = od2.JobNo
    WHERE t.WorkCntr = 100 AND od2.PartDesc = od.PartDesc
) AS StartDate
...

答案 1 :(得分:0)

由于您已经有2个查询,因此只需JOIN个查询。如果他们没有相同数量的记录,你可以LEFT/RIGHT JOIN

SELECT *
FROM (
    SELECT
        od.PartNo,
        CAST(od.PartDesc AS NVARCHAR(MAX)) AS [PartDesc],
        MAX(CAST(t.TicketDate AS DATETIME)) AS [EndDate]
    FROM TimeTicketDet t JOIN OrderDet od ON t.JobNo = od.JobNo
    JOIN OrderRouting r ON t.JobNo = r.JobNo
    WHERE t.WorkCntr = 500 AND r.Status = 'Finished'
    GROUP BY od.PartNo, CAST(od.PartDesc AS NVARCHAR(MAX))
) a
JOIN (
    SELECT
        od.PartNo,
        CAST(od.PartDesc AS NVARCHAR(MAX)) AS [PartDesc],
        MIN(CAST(t.TicketDate AS DATETIME)) AS [StartDate]
    FROM TimeTicketDet t JOIN OrderDet od ON t.JobNo = od.JobNo
    JOIN OrderRouting r ON t.JobNo = r.JobNo
    WHERE t.WorkCntr = 100
    GROUP BY od.PartNo, CAST(od.PartDesc AS NVARCHAR(MAX))
) b ON b.PartNo = a.PartNo