删除重复条目但保持关系

时间:2015-04-20 04:11:56

标签: sql oracle11g

我有一个看起来像这样的表:

Name | Product | Total
----  --------- ------
A       Toy       $5
A       Car       $30,000
A       Equipment $500
B       Car       $100,000

等...

我想删除重复的名称条目,但保持与产品示例的关系:

Name | Product | Total
----  --------- ------
A       Toy       $5
        Car       $30,000
        Equipment $500
B       Car       $100,000

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

如果您希望仅在Name时保留Product ='Toy',请使用Case声明

Select case when Product ='Toy' then Name else '' END as Name,
       Product,
       Total
From yourtable

答案 1 :(得分:0)

试试这个

WITH CTE AS (
SELECT
rownum = ROW_NUMBER() OVER (ORDER BY i.name),
i.name,i.product,i.Total
FROM yourtable i
)
SELECT
case when prev.name = cte.name
then
' '
else
cte.name
end as name,
cte.address,
cte.Total
FROM CTE
LEFT JOIN CTE prev ON prev.rownum = CTE.rownum - 1

答案 2 :(得分:0)

我认为您要制作Name = '',因为您无法删除重复的记录。

因此,您可以在ROW_NUMBER子句中使用WITH函数并更新Name字段

;WITH C AS(
    SELECT  ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Name) AS Rn
            ,Name
            ,Product
            ,Total
    FROM yourTable
)
UPDATE C
SET Name = ''
WHERE Rn <> 1