2017-07-07 42 views
0

查詢1:加入下列查詢時發生異常,有人可以幫助我加入查詢嗎?

select col1,sum(col2) from tablea src 
    join 
    (select distinct col3,ROW_NUMBER() OVER(ORDER BY col3 desc) as RN) 
    From tableb) trgt 
    where RN=1 and src.col3 = trgt.col3 group by col1 

問題2:

select col1,sum(col2) from tablea src 
join 
(select distinct col3,ROW_NUMBER() OVER(ORDER BY col3 desc) as RN) 
From tableb) trgt 
where RN=2 and src.col3 = trgt.col3 group by col1 

加入查詢:

select * 
    from (Query 1) curr join (Query 2) 
    prev on curr.col1 = prev.col1 

FAILED:ParseException的線6:75不能識別鄰近輸入 '爲' 中 'CURR'子查詢源

回答

0

我認爲沒有必要加入這兩個查詢,si他們彼此非常相似,就像兩滴水。
某種支點查詢可以完成這項工作:

SELECT col1, 
     SUM(CASE WHEN rn=1 THEN col2 END), 
     SUM(CASE WHEN rn=2 THEN col2 END) 
FROM tablea src 
JOIN (
    select col3,ROW_NUMBER() OVER(ORDER BY col3 desc) as RN) 
    From tableb 
) trgt 
ON src.col3 = trgt.col3 AND trgt.rn <= 2 
GROUP BY col1 
+0

謝謝它的作品! –

+0

如果你喜歡我的回答,請重音,謝謝。 – krokodilko