2012-05-29 3 views
1

SQL 쿼리에 대한 도움이 필요합니다. 많은 항목이있는 표가 하나 있는데 마지막 3 열의 값이 동일한 항목을 모두 조회하려고합니다. 다음과 같이SQL "거의"중복 항목 (마지막 3 열에 대해 동일한 값)에 대한 쿼리

내 표는 같습니다

|Refrigator|98C08A|2011-08-06 00:00:30|126|126 
|Refrigator|B7BE29|2011-08-06 00:00:30|73|70 
|Refrigator|599393|2011-08-06 00:00:30|126|126 
|Refrigator|B7BE29|2011-08-06 00:00:29|73|70 
|Refrigator|599393|2011-08-06 00:00:29|126|126 
|Refrigator|599393|2011-08-06 00:00:29|126|126 
|Refrigator|98C08A|2011-08-06 00:00:29|126|126 
|Refrigator|98C08A|2011-08-06 00:00:29|126|126 
|Refrigator|599393|2011-08-06 00:00:28|126|126 

그래서 나는 지난 3 열에 대해 동일한 값을 가진 모든 행을 가져올 지, 그래서 결과는 같아야합니다 :

|Refrigator|98C08A|2011-08-06 00:00:30|126|126 
|Refrigator|599393|2011-08-06 00:00:30|126|126 
|Refrigator|599393|2011-08-06 00:00:29|126|126 
|Refrigator|599393|2011-08-06 00:00:29|126|126 (if possible without this duplicate) 
|Refrigator|98C08A|2011-08-06 00:00:29|126|126 
|Refrigator|98C08A|2011-08-06 00:00:29|126|126 (if possible without this duplicate) 

누구든지 이것을 관리하는 방법을 알고 있습니까? 지금까지 시도 무엇 했다 :

SELECT * 
FROM smtab 
WHERE Datetime IN (
     SELECT Datetime 
     FROM smtab 
     GROUP BY Datetime 
     HAVING count(Datetime) >1) 
AND Power1 IN (
     SELECT Power1 
     FROM smtab 
     GROUP BY Power1 
     HAVING count(Power1) >1) 
AND Power8 IN (
     SELECT Power8 
     FROM smtab 
     GROUP BY Power8 
     HAVING count(Power8) >1) 
ORDER BY Datetime DESC; 

하지만 작동하지 않았다!

누군가 나를 도울 수 있기를 바랍니다. thx 미리 ...

+0

지난 3 열에 동일한 값이없는 당신의 예제 결과에주는 행. – liquorvicar

+0

이 관련 기사는 당신의 힌트 + 답을 위해서 http://beemerguy.net/blog/post/How-to-remove-duplicate-SQL-table-entries-(by-example).aspx – BeemerGuy

답변

1
SELECT DISTINCT * 
FROM smtab NATURAL JOIN (
    SELECT Datetime, Power1, Power8 
    FROM  smtab 
    GROUP BY Datetime, Power1, Power8 
    HAVING COUNT(*) > 1 
) AS t 
+0

와우, 정말 많이! 이것은 매우 우아한 솔루션이며 매력처럼 작동합니다! – funkypopcorn

+0

위의 시나리오에서 완벽하게 작동했습니다. 이제는 (특정 제한된 시간 범위까지) 더 많이 응답해야하는 중복 수를 제한하는 또 다른 제약 조건이 있습니다. 그래서 모든 복제본이 필요합니다. 예 : Where Datetime> '2011-08-06 00:00:00'AND Datetime < '2011-08-07 00:00:50' 해결책을 찾지 못했지만 예상대로 작동하지 못했습니다. 어떻게 든 그것을 관리 할 수있는 방법은 무엇입니까? – funkypopcorn

+0

@funkypopcorn : 하위 쿼리에'WHERE' 절을 추가하십시오. 예를 들면 다음과 같습니다. from ... smtab WHERE Datetime BETWEEN '2011-08-06 00:00:00'AND '2011- 08-07 00:00:50 'GROUP BY ...'? – eggyal

0

나는 자기 조달을 찾고 있다고 생각합니다. 시작하려면 this SO answer을보세요. 제외 할 열을 언급하지 않으므로 코드를 제공 할 수 없습니다.

0

문제는 중복, 내부 조인을 식별하고 일치하는 모든 것을 찾아야한다는 것입니다.

distinct은 각 복제본 중 하나만 반환 할 수있게합니다.

-- only select one of each duplicate. 
select distinct * 
    from smtab as a 
     -- Find the duplicates 
    join (select datetime, power1, power8 
      from smtab 
      group by datetime, power1, power8 
     having count(*) > 1) as b 
     -- join back on to the main table 
    on a.datetime = b.datetime 
    and a.power1 = b.power1 
    and a.power8 = b.power8 

각 개별 항목이 아닌 3 개의 항목 모두에서 중복 된 항목을 찾고 있습니다. 그러므로 당신은 당신의 복제물을 찾기 위해 동시에 3 명 모두를 그룹화해야합니다.

+0

thx에 관심이있을 것입니다! 위대한 작품! – funkypopcorn

0

이 방법은 SQL Server와 함께 내 데이터 모델에서 작동합니다. MySQL과 함께 작동하는지 확실하지 않습니다. 파생 된 쿼리에 테이블을 가입시키고 있습니다. 파생 된 쿼리는> 1 레코드가있는 모든 레코드를 찾습니다.

select * from Employees as e 
inner join 
(
select LastName, firstname from Employees 
group by LastName, FirstName having COUNT(1) > 1 
) as derived 
on e.LastName = derived.lastname and e.FirstName = derived.firstname 
order by e.LastName 

편집 : 는 데이터 모델에 더 관련된 만들기 같은 것을 시도하려면

SELECT * FROM smtab as s 
    inner join 
(
    select datetime, power1, power8 
    from smtab as s2 
    group by s2.datetime, power1, POWER8 having COUNT(1) > 1 
) as derived 
on s.datetime = derived.datetime and s.power1 = derived.power1 
and s.power8 = derived.power8 
ORDER BY Datetime DESC; 
관련 문제