Postgresql with recursive query



尝试使用递归后,我有疑问

我正在尝试使用两张桌子

第一个表complain_table

complain                   product_id
----------------------------------------
Not working          -          1
not working                     1
not working                     1
Loading problem                 2
Loading problem                 2

第二个表product_table

Name     id                      
--------------
usb      1
cd       2

现在我想要的输出是

product                complain
-----------------------------------
usb                   Not working
                      Not working
                      Not working
cd                    Loading problem
                      Loading problem
如果您希望按

所示输出(禁止显示重复的产品名称),则应执行以下操作:

select case 
         when row_number() over (partition by p.name order by ct.complain) = 1 then p.name
         else null
       end as product,
       ct.complain
from products p
  join complain_table ct on p.product_id = ct.product_id
order by p.product_id;

顺便说一句:您的complain_table看起来应该有一个引用complain_reason_text表的complain_reason_id,以避免一遍又一遍地重复相同的投诉文本

最新更新