删除重复项是没有问题的,我遇到了一个问题,我不仅要删除副本,还要删除在SELECT语句中引起重复的原始行。
ItemNUMBER ID from_DATE to_DATE fancy_code
1 100 3/28/2015 4/10/2015 Z5111
2 100 4/18/2015 4/21/2015 Z5112
3 100 5/2/2015 5/5/2015 Z5111
4 100 5/31/2015 6/2/2015 Z5111
5 100 7/31/2015 8/16/2015 A400 --remove
6 100 7/31/2015 8/16/2015 A400 --remove
上面是我的表, - 删除是最终目标。任何帮助将不胜感激!
答案 0 :(得分:1)
强力方法是使用窗口函数:
with t as (
<your query here>
)
select t.*
from (select t.*,
count(*) over (partition by ID, from_DATE, to_DATE, fancy_code) as cnt
from t
) t
where cnt = 1;
更智能的方法是修复查询或导致重复的数据。当然,在某些情况下无法进行此类修复,因此您需要使用强力方法。
答案 1 :(得分:0)
在列表中
SELECT MIN(ItemNUMBER), from_DATE, to_DATE, fancy_code
FROM yourTable
GROUP BY ID, from_DATE, to_DATE, fancy_code
HAVING COUNT(*) = 1
如果需要DELETE
DELETE FROM yourTable
WHERE ItemNUMBER NOT IN (
SELECT MIN(ItemNUMBER) as ItemNUMBER
FROM yourTable
GROUP BY ID, from_DATE, to_DATE, fancy_code
HAVING COUNT(*) = 1
)
答案 2 :(得分:0)
您可以在这些行上使用连接选择它们。
SELECT ItemNUMBER, ID, from_DATE, to_DATE, fancy_code
FROM [Table] t join (
SELECT ItemNUMBER, ID, from_DATE, to_DATE, count(1)
FROM [Table] t
GROUP BY ItemNUMBER, ID, from_DATE, to_DATE
HAVING count(1) = 1
) u on (u.ItemNUMBER= t.ItemNUMBER and u.ID=t.ID
and u.from_DATE=t.from_DATE and u.to_DATE = t.to_DATE)
根据原始表中的密钥调整味道(ID将是候选者)。
答案 3 :(得分:0)
通常会删除双重条目
第1步:获取重复的条目
步骤2:从重复条目中识别项目编号
第3步:删除那些项目数
DELETE FROM table1
WHERE itemnumber IN (SELECT
itemNumber
FROM table1 z
INNER JOIN (SELECT
fancy_code,
id,
from_date,
to_date
FROM table1 x
GROUP BY fancy_code,
id,
from_date,
to_date
HAVING COUNT(*) > 1) y
ON y.fancy_code = z.fancy_code
AND y.id = z.id
AND y.from_date = z.from_date
AND y.to_date = z.to_date)
答案 4 :(得分:0)
如果您到达表格中的重复ItemNUMBER
,则可以通过简单的WHERE
子句删除所有这些内容。
为了做到这一点,您需要使用表格内部JOIN
,然后使用GROUP
- BY
您可以访问的DELETE theTable
WHERE ItemNUMBER IN
(
SELECT t1.ItemNUMBER
FROM theTable t1
JOIN theTable t2 ON t1.ID = t2.ID AND t1.from_DATE = t2.from_DATE AND t1.to_DATE = t2.to_DATE AND t1.fancy_code = t2.fancy_code
GROUP BY t1.ItemNUMBER
HAVING COUNT(t1.ItemNUMBER) > 1
)
可重复列。目的。
因此查询可以类似于以下内容:
span {
position: absolute;
right: 150px; /* width of Right element */
white-space: nowrap; /* prevent wrapping */
min-width: calc(100% - 350px); /* difference of Left and Right widths */
}
#leftside {
z-index: 1; /* cover the middle element if needed */
}
答案 5 :(得分:0)
使用CTE和count(*)最简单的方法。
WITH cte AS
(
SELECT fkledgeraccid, ROW_NUMBER() OVER (PARTITION BY ID,from_DATE,to_DATE,fancy_code) AS row
FROM TABLE1
)
Select * from cte where row=1