在查询-SQL DB2中查找where子句的最小值和最大值

时间:2017-11-13 16:51:21

标签: sql db2 subquery

我正在尝试获取区域12的最小和最大日期。我不知道如何在查询中使用where子句。

SELECT A1.*, Ticket_ID,
min(StartDate) OVER(PARTITION BY Ticket_ID) AS Min_StartDate, 
MAX(EndDate) OVER(PARTITION BY Ticket_ID) AS Max_EndDate 
WHERE area = '12' 
FROM (SELECT T1.Ticket_ID, T1.StartDate,T1.StartDate, t2.title, t1.area,T2.Ticket_TYPE
      FROM Ticket1 t1 
      LEFT JOIN Ticket2 t2 on t1.Ticket_ID = t2.Ticket_ID 
     ) A1
WHERE Ticket_TYPE = 1 AND StartDate <= '9/30/2017' 
AND (EndDate >= '10/01/2017' OR EndDate IS NULL) 
ORDER BY Ticket_ID

1 个答案:

答案 0 :(得分:0)

尝试将其作为:

运行
SELECT A1.*, A1.Ticket_ID,
min(StartDate) OVER(PARTITION BY Ticket_ID) AS Min_StartDate, 
MAX(EndDate) OVER(PARTITION BY Ticket_ID) AS Max_EndDate 
FROM (SELECT T1.Ticket_ID, T1.StartDate,T1.EndDate, t2.title, t1.area,T2.Ticket_TYPE
      FROM Ticket1 t1 
      LEFT JOIN Ticket2 t2 on t1.Ticket_ID = t2.Ticket_ID 
      WHERE t2.Ticket_TYPE = 1 AND t1.StartDate <= '9/30/2017' 
        AND (t1.EndDate >= '10/01/2017' OR t1.EndDate IS NULL)
        AND t1.area = '12'
     ) A1
ORDER BY A1.Ticket_ID

*您有两次指定StartDate的拼写错误,在子查询中更正为EndDate。