2014-06-19 4 views
0

우리는 매일 여러 대의 컴퓨터 정보를 사용하여 테이블을 채 웁니다. 문제는 때로는 특정 컴퓨터에서 정보를 가져 오지 않는 경우입니다.TSQL에서 누락 된 행 쿼리

예를 들어, 표의 열은 computer_name, information_pulled, qty_pulled, date_pulled입니다.

그래서 15 일을 제외하고는 1 주일에 매일 뽑아 버립니다. 쿼리가 실행됩니다.

Computer_name, Information_pulled, qty_pulled, date_pulled 

computer1  infopulled   2   2014-06-14 
computer2  infopulled   3   2014-06-14 
computer3  infopulled   2   2014-06-14 
computer1  infopulled   2   2014-06-15 
computer3  infopulled   1   2014-06-15 
computer1  infopulled   3   2014-06-16 
computer2  infopulled   2   2014-06-16 
computer3  infopulled   4   2014-06-16 

15 번째 컴퓨터 2에서는 아무것도 가져 오지 않았습니다. 특정 날짜에 누락 된 행을 가져 오는 쿼리를 작성하려고합니다.

예를 들어, 실행 후에는 말한다

computer 2  null   null   20140615 

또는이 가까운 것을. 우리는 매일 아침이 테이블에 데이터가 채워지지 않을 때 데이터를 잡으려고 노력하고 있습니다. 우리가 능동적 일 수 있고 긍정적 인 것도 아니며 null 값을 검색하는 누락 된 데이터를 쿼리 할 수도 있습니다.

답변

3

컴퓨터가 테이블에 포함되지 않은 경우를 알 수 있도록 모든 컴퓨터의 마스터 목록이 있어야합니다. 이것을 보유하고있는 Computer이라는 테이블이 있다고 가정 해보십시오.

declare @date date 
set @date = '6/15/2014' 

그런 다음 다음과 같이 행을 누락 조회 할 수 있습니다 :

select c.Computer_name, null, null, @date 
from Computer c 
where not exists(select 1 
       from myTable t 
       where t.Computer_name = c.Computer_name 
       and t.date_pulled = @date) 

SQL Fiddle

당신이 확신하는 경우

검사 할 날짜를 저장하는 변수를 선언 computer_name이 테이블에 적어도 한 번 이상 이미 존재하면 별도의 Computer 테이블을 생성하지 않고 쿼리 l 예 :

이 쿼리는 테이블에 이미 행이없는 컴퓨터를 표시하지 않으므로 강력하지 않습니다 (예 : 새로운 컴퓨터 또는 정보를 가져 본 적이없는 문제가있는 컴퓨터). 당신은 다음과 같은 날짜와 컴퓨터 이름에 의해 자체에 테이블을 조인하는 경우

0

, 당신이없는 날짜의 목록을해야

SELECT t1.computer_name, null as information_pulled, null as qty_pulled, 
DATEADD(day,1,t1.date_pulled) as missing_date 
FROM computer_info t1 
LEFT JOIN computer_info t2 ON t2.date_pulled = DATEADD(day,1,t1.date_pulled) 
     AND t2.computer_name = t1.computer_name 
WHERE t1.date_pulled >= '2014-06-14' 
    AND t2.date_pulled IS NULL 

이것은 또한, 아직 뽑아되지 않은 다음 날짜를 얻을 것이다하지만, 그것은 분명해야하고 추가 조건을 추가하여 필터링 할 수 있습니다.

AND DATEADD(day,1,t1.date_pulled) < '2014-06-17' 

당연한 말이지 만 이전 날 동안 각 컴퓨터 이름이 이미 테이블에 존재하는 경우에만 작동합니다. 그렇지 않다면 별도의 컴퓨터 테이블을 만드는 @ Jerrad의 제안이 도움이 될 것입니다.

편집 : 간격이 하루보다 큰 경우, 당신은

SELECT t1.computer_name, null as info, null as qty_pulled, 
     DATEADD(day,1,t1.date_pulled) as missing_date, 
     t3.date_pulled AS next_pulled_date 
FROM computer_info t1 
LEFT JOIN computer_info t2 ON t2.date_pulled = DATEADD(day,1,t1.date_pulled) 
     AND t2.computer_name = t1.computer_name 
LEFT JOIN computer_info t3 ON t3.date_pulled > t1.date_pulled 
     AND t3.computer_name = t1.computer_name 
LEFT JOIN computer_info t4 ON t4.date_pulled > t1.date_pulled 
     AND t4.date_pulled < t3.date_pulled 
     AND t4.computer_name = t1.computer_name 
WHERE t1.date_pulled >= '2014-06-14' 
    AND t2.date_pulled IS NULL 
    AND t4.date_pulled IS NULL 
    AND DATEADD(day,1,t1.date_pulled) < '2014-06-17' 

'T3'는 하나의 누락, 제 1 및 'T4'를 통해 모든 날짜에 참여합니다 가입 볼 수도 있습니다 t4.pulled_date IS NULL과 함께 참여하면 해당 날짜 중 가장 낮은 날짜를 제외하고 모두 제외됩니다.

하위 쿼리를 사용하여이 작업을 수행 할 수도 있지만 조인을 제외하면 과거에 잘 처리되었습니다.

0

저는 크로스 조인 (cross-join)이 문제에 대한 답을 줄 것이라고 생각합니다.
아래 쿼리에서 모든 컴퓨터는 매일 적어도 한 번 이상 적어도 한 번 이상 성공적으로 업로드해야합니다.
이 방법으로 모든 누락 된 컴퓨터/데이트 커플을 얻을 수 있습니다.

select 
    Compare.* 
from Table_1 T1 
    right join (
     select * 
     from 
      (select Computer_name from Table_1 group by Computer_name) CPUS, 
      (select date_pulled from Table_1 group by date_pulled) DAYs 
     ) Compare 
    on T1.Computer_name=Compare.Computer_name 
     and T1.date_pulled=Compare.date_pulled  
where T1.Computer_name is null 

희망이 도움이됩니다.