我需要什么样的JOIN声明?

时间:2012-09-13 11:55:29

标签: sql tsql join

我有两个表PROCESS和STEP以及一些数据如下所示。 (他们有1-N的关系。)

PROCESS ->

ID START_TIME STARTED_BY STATUS
1  31/08/2012 User1      FINISHED
2  31/08/2012 User2      FINISHED
3  05/09/2012 User3      ACTIVE

STEP ->

ID PROCESS_ID START_TIME END_TIME   STATUS
1  1          31/08/2012 02/09/2012 FINISHED  
2  1          02/09/2012 03/09/2012 FINISHED    
3  1          03/09/2012 10/09/2012 FINISHED  
4  2          31/08/2012 04/09/2012 FINISHED  
5  2          04/09/2012 06/09/2012 FINISHED  
6  2          06/09/2012 09/09/2012 FINISHED  
7  3          05/09/2012 06/09/2012 FINISHED    
8  3          06/09/2012 NULL       ACTIVE

我需要的是一个JOIN,它将为我提供完成流程的开始和结束时间,如下所示:

PROCESS_ID START_TIME END_TIME  
1          31/08/2012 10/09/2012  
2          31/08/2012 09/09/2012

为了实现这个目的,我需要写什么样的JOIN语句?

2 个答案:

答案 0 :(得分:2)

为避免拉入PROCESS_ID 3,您需要先汇总并过滤

HAVING子句将确保您忽略STEP表中PROCESS_ID 3的所有行。简单WHERE END_TIME IS NOT NULLWHERE .. 'FINISHED'不会这样做。在JOIN之后聚合

SELECT
   P.PROCESS_ID,
   S.StartTime, S.EndTime
FROM
   PROCESS P
   JOIN
   (
   SELECT
      PROCESS_ID,
      MIN(START_TIME) AS StartTime,
      MAX(END_TIME) AS EndTime
   FROM
      STEP
   GROUP BY
      PROCESS_ID
   HAVING
      COUNT(*) = COUNT(END_TIME) -- only where we have no END_TIME nulls
   ) S ON P.PROCESS_ID = S.PROCESS_ID

例如,还有其他选项使用NOT EXISTS来忽略ACTIVE行,但我在这里只使用END_TIME和NULL来忽略STEP行

答案 1 :(得分:1)

您似乎需要INNER JOIN并使用MINMAX功能。

SELECT PROCESS.PROCESS_ID, MIN(START_TIME), MAX(END_TIME)
FROM PROCESS
INNER JOIN STEP ON STEP.PROCESS_ID = PROCESS.PROCESS_ID
GROUP BY PROCESS.PROCESS_ID