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
基本上我不想重複客戶的名字,如果她有多個產品。有沒有辦法做到這一點?sql查詢不顯示重複記錄
解決方案:
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
你一定要做到這一點,顯示層上不上的數據庫級別 – Lamak
@Lamak:但你可能想這樣做,如果你正在返回在SQL Reporting Services報告中使用的結果集 – Sung
我說使用遊標,但我覺得懶得想出一個使用遊標的解決方案 – Sung