Customers
id,name,orderid
----------------------
1,Ted, 1
2,Jen, 2
Orders
id,prodname
----------------------
1,widgetA
1,widgetC
2,widgetB
2,widgetA
desired results
name, prodname
----------------------
Ted, widgetA
' ', widgetC
Jen, widgetB
' ', widgetA
基本的に、顧客が複数の製品を持っている場合、顧客の名前を繰り返したくありません。これを行う方法はありますか?
解決:
Select
(CASE WHEN myrow=1 THEN Name ELSE ' ' END) AS Name,
product
From
(
Select
ROW_NUMBER() OVER (PARTITION BY c.Name ORDER BY c.Name) AS myrow,
o.productname AS product
FROM
Customers c JOIN
Orders o ON c.orderid = o.id
) T1
Order By Name