SQL子查询错误

时间:2017-11-13 19:17:24

标签: sql db2 subquery

我认为A2套装存在问题,我只包括Restart_case = Y,但我不确定请帮忙。我收到此错误:在“_ID)AS Max_DateBegin”后面找到了意外的标记“WHERE”。预期的代币可能包括...... 谢谢

SELECT A2.* FROM
(SELECT A1.*, min(BeginDate) OVER (PARTITION BY Per_ID) AS Min_BeginDate,
MAX(BeginDate) OVER (PARTITION BY Per_ID) AS Max_BeginDate
WHERE RestartCase = 'Y'
From) A2
(SELECT distinct C.Per_ID, P.DOB, C.BeginDate, C.EndDate, C.RestartCase, P.per_type
FROM CaseSum C LEFT JOIN PERSON p on C.ID_PRSN = P.ID_PRSN)  A1
WHERE per_Type = 1 AND BeginDate <= '9/30/2017' AND (EndDate >= '10/01/2017' OR EndDate IS NULL)
ORDER BY A1.Per_ID

2 个答案:

答案 0 :(得分:0)

我相信这就是你想要的东西(或类似东西):

SELECT A2.* FROM (SELECT A1.*, MIN(BeginDate) OVER (PARTITION BY Per_ID) AS Min_BeginDate, MAX(BeginDate) OVER (PARTITION BY Per_ID) AS Max_BeginDate FROM (SELECT DISTINCT C.Per_ID, P.DOB, C.BeginDate, C.EndDate, C.RestartCase, P.per_type FROM CaseSum C LEFT JOIN PERSON P on C.ID_PRSN = P.ID_PRSN WHERE P.per_Type = 1 AND C.BeginDate <= '9/30/2017' AND (C.EndDate >= '10/01/2017' OR C.EndDate IS NULL) ) A1 WHERE A1.RestartCase = 'Y' ORDER BY A1.Per_ID ) A2

答案 1 :(得分:0)

在窗口函数中考虑条件CASE内联聚合以组合两个尝试的结果集:

SELECT DISTINCT C.Per_ID, P.DOB, C.BeginDate, C.EndDate, C.RestartCase, P.per_type,  
                MIN(CASE WHEN C.RestartCase = 'Y' THEN C.BeginDate END)
                              OVER (PARTITION BY C.Per_ID) AS Min_BeginDate,
                MAX(CASE WHEN C.RestartCase = 'Y' THEN C.BeginDate END)
                              OVER (PARTITION BY C.Per_ID) AS Max_BeginDate    
FROM CaseSum C 
LEFT JOIN PERSON P on C.ID_PRSN = P.ID_PRSN
WHERE P.per_Type = 1 AND C.BeginDate <= '9/30/2017' 
  AND (C.EndDate >= '10/01/2017' OR C.EndDate IS NULL)
ORDER BY C.Per_ID