来自其他查询

时间:2018-01-29 16:34:57

标签: postgresql subquery

我在Postgres中有2个查询,我想从第一个查询的结果中减去第二个查询的结果。

第一次查询

SELECT DISTINCT woas.workorderid, 
nth_value(wos.endtime,1) OVER(PARTITION BY woas.workorderid ORDER BY wos.endtime ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as "Nth Onhold Start time" from wo_status_info wos
LEFT JOIN wo_assessment woas ON woas.assessmentid = wos.assessmentid
WHERE wos.statusid = 1 AND wos.nextstatusid = 2
ORDER BY woas.workorderid

第一个查询结果

 |-------------------|-------------------------|
 |    workorderid    |  Nth Onhold Start time  |
 |-------------------|-------------------------|                 
 |         2         |       1515231826648     |
 |-------------------|-------------------------|                 
 |         6         |       1516456159371     |
 |-------------------|-------------------------|

第二次查询

SELECT DISTINCT woas.workorderid, 
nth_value(wos.endtime,1) OVER(PARTITION BY woas.workorderid ORDER BY wos.endtime ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as "Nth Onhold Start time" from wo_status_info wos
LEFT JOIN wo_assessment woas ON woas.assessmentid = wos.assessmentid
WHERE wos.statusid = 2 AND wos.nextstatusid = 1 OR wos.statusid = 2 AND wos.nextstatusid = 3
ORDER BY woas.workorderid

第二个查询结果

 |-------------------|-------------------------|
 |    workorderid    |  Nth Onhold End time    |
 |-------------------|-------------------------|                 
 |         2         |       1515241744772     |
 |-------------------|-------------------------|                 
 |         6         |       1516456311878     |
 |-------------------|-------------------------|

我认为我需要将这些查询作为子查询和某种自我JOIN,但我不确定如何编写它。

JOIN应该在workorderid

必填结果

 |-------------------|-------------------------|
 |    workorderid    |        Difference       |
 |-------------------|-------------------------|                 
 |         2         |         9918124         |
 |-------------------|-------------------------|                 
 |         6         |         152507          |
 |-------------------|-------------------------|

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

如果你想加入两个派生表(即你有的查询),一种方法就是这样:

SELECT *
FROM (
  SELECT ...
) a
LEFT JOIN (
  SELECT ...
) b ON a.PK_column = b.PK_column

在你的情况下:

SELECT a.workorderid, b.endtwo - a.endone AS difference
FROM (
  SELECT DISTINCT woas.workorderid, 
    nth_value(wos.endtime,1) OVER(PARTITION BY woas.workorderid ORDER BY 
    wos.endtime ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as endone from wo_status_info wos
  LEFT JOIN wo_assessment woas ON woas.assessmentid = wos.assessmentid
  WHERE wos.statusid = 1 AND wos.nextstatusid = 2
) a
LEFT JOIN (
  SELECT DISTINCT woas.workorderid, 
    nth_value(wos.endtime,1) OVER(PARTITION BY woas.workorderid ORDER BY 
    wos.endtime ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as endtwo from wo_status_info wos
  LEFT JOIN wo_assessment woas ON woas.assessmentid = wos.assessmentid
  WHERE wos.statusid = 2 AND wos.nextstatusid = 1 OR wos.statusid = 2 AND wos.nextstatusid = 3
) b ON a.workorderid = b.workorderid
ORDER BY a.workorderid

如果您只想在两个查询中都有匹配的行时返回结果,请将LEFT JOIN更改为INNER JOIN。我还将ORDER BY移动到外部查询。这是你在找什么?

另外,请注意WHERE子句和AND/OR条件。如果ORAND混合,则应始终使用括号将条件组合在一起。否则,您的OR可能会意外覆盖您的其他AND条件:

WHERE wos.statusid = 2 
AND wos.nextstatusid = 1 
OR wos.statusid = 2 
AND wos.nextstatusid = 3

我想你想要这样的东西:

WHERE 
  (wos.statusid = 2 AND wos.nextstatusid = 1) OR  
  (wos.statusid = 2 AND wos.nextstatusid = 3)