簡體   English   中英

Postgresql SELECT LEFT JOIN,帶有大小寫列

[英]Postgresql SELECT LEFT JOIN with columns on case

SELECT a1,a2,a3,a4,count(a5),b1,b2,b3
FROM table1
LEFT JOIN table2 ON a1=b1 AND a2=b2 (*here i need to join
              next columns a3=b3 only if from table2 will be returned more than 1 records 
    other wise first 2 columns will be enough*)
group by a1,a2,a3,a4,a5,b1,b2,b3

有人知道如何執行此技巧嗎?

好吧,如果我正確理解:

FROM table1 t1 LEFT JOIN
     (SELECT t2.*, COUNT(*) OVER (PARTITION BY b1, b2) as cnt
      FROM table2 t2
     )
     ON t1.a1 = t2.b1 AND t1.a2 = t2.b2 AND
        (cnt = 1 OR t1.a3 = t2.a3)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM