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