0

尝试递归后我有疑问

我正在尝试两张桌子

第一张桌子是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
4

1 回答 1

1

如果您想要如图所示的输出(抑制重复的产品名称),则应执行以下操作:

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_id引用complain_reason_text表,以避免一遍又一遍地重复相同的抱怨文本

于 2013-11-11T08:00:19.930 回答