2013-08-24 3 views
0

아래의 쿼리를 통해 날짜 범위에서 수행 된 서비스를 모니터링 할 수 있습니다. 또한 마지막 "X"일을 표시하도록 수정합니다.날짜 범위 내에서 활동이없는 클라이언트 찾기

여기 내 문제입니다. 날짜 범위에서 활동이없는 모든 고객을 찾아야합니다. 왼쪽 외부 조인을 사용하는 예제를 보았습니다. (http://www.postgresqlforbeginners.com/2010/11/sql-outer-joins.html)을 따르려고했지만 "누락 된"서비스가 표시되지 않습니다. 나는 "Bob"이 과거의 "X"날에는 보이지 않았 음을 알 필요가있었습니다. 클라이언트가 있다고 가정

SELECT groups.name as Office,to_char (notes.date_service,'MM/DD/YY')as DateEntered, 
to_char(notes.date_creation,'MM/DD/YY')as DateService, 
notes.date_creation - notes.date_service as DateDiff, 
services.code, clients.client_id, 
clients.name_lastfirst_cs, staff.staff_name_cs, address.addr_county 
FROM notes, services, clients, staff, groups, address 
WHERE notes.zrud_service = services.zzud_service 
AND notes.zrud_client = clients.zzud_client 
AND notes.zrud_staff = staff.zzud_staff 
AND notes.zrud_group = groups.zzud_group 
AND clients.zzud_client = address.zrud_client 
AND services.code IN ('10101', '10102', '10201' , '10202','10203','10204','10205','10401','10402','10403','10405') - - <I comment out this line and change it depending on the results I need > 

AND groups.name = 'RCE' 
AND notes.date_service BETWEEN (now() - '8 days'::interval)::timestamp AND now(); 
-- this last line is also changed for diferent time spans 

감사

답변

0

: 사람이보고 솔루션에 나를 조종 할 수있는 경우

그래서, 나는 여기에

쿼리 매우 감사 할 것 테이블 clients에 저장되어 있고 해당 활동이 notes 테이블에있는 경우 NOT EXISTS (안티 조인)를 사용하십시오.

-- I need to find all clients ..... 
SELECT * FROM clients 
WHERE 
-- ... that have had NO activity .... 
    NOT EXISTS (
    SELECT 1 FROM notes 
    WHERE notes.zrud_client = clients.zzud_client 
-- ... in a date range. 
     AND notes.date_service BETWEEN (now() - '8 days'::interval)::timestamp AND now() 
) 

상기 antijoin 6,가 왼쪽 외부로 전환 될 수있다이 방법 조인 대답

SELECT clients.* FROM clients 
LEFT JOIN notes 
ON ( 
    notes.zrud_client = clients.zzud_client 
    AND 
    notes.date_service BETWEEN (now() - '8 days'::interval)::timestamp AND now() 
    ) 
WHERE 
    notes.zrud_client IS NULL 
+0

Kordirko 대신 join에 조건을 이동합니다. 마지막 WHERE 문을 사용하여 어떻게 시도했는지, 그리고 제안한대로 주석을 달았습니다. 그런 다음 보고서에 나타난 여러 노트를 클라이언트에 추가하여 다른 쿼리에서 사라지는 지 알아 보았습니다. – Detox

0

그쪽 주요부 적절한 ANSI 조인

  • 사용이고;
  • 형식을 입력하십시오.
  • 테이블의 별명을 사용하십시오.

그래서 쿼리가된다 :

select 
    g.name as Office, 
    to_char(n.date_service,'MM/DD/YY') as DateEntered, 
    to_char(n.date_creation,'MM/DD/YY') as DateService, 
    n.date_creation - n.date_service as DateDiff, 
    s.code, c.client_id, 
    c.name_lastfirst_cs, st.staff_name_cs, a.addr_county 
from notes as n 
    inner join services as s on s.zzud_service = n.zrud_service 
    inner join clients as c on c.zzud_client = n.zrud_client 
    inner join staff as st on st.zzud_staff = n.zrud_staff 
    inner join groups as g on g.zzud_group = n.zrud_group 
    inner join address as a on a.zrud_client = c.zzud_client 
where 
    g.name = 'RCE' and 
    s.code in ('10101', '10102', '10201', '10202','10203','10204','10205','10401','10402','10403','10405') and 
    n.date_service BETWEEN (now() - '8 days'::interval)::timestamp AND now(); 

을하지만 그들이 주어진 기간에 노트에 아무 활동이없는 경우에도 모든 클라이언트를 얻으려면, 당신이 clients 쿼리 당신의 앵커가되고 싶어요 생각, 올바른 방향으로 나를 조종 주셔서 감사합니다 순전히 - left outer join을 사용하고 where

select 
    c.client_id, 
    c.name_lastfirst_cs, st.staff_name_cs, a.addr_county, 
    g.name as Office, 
    to_char(n.date_service,'MM/DD/YY') as DateEntered, 
    to_char(n.date_creation,'MM/DD/YY') as DateService, 
    n.date_creation - n.date_service as DateDiff, 
    s.code 
from clients as c 
    inner join address as a on a.zrud_client = c.zzud_client 
    left outer join notes as n on n.zrud_client = c.zzud_client and n.date_service BETWEEN (now() - '8 days'::interval)::timestamp AND now() 
    left outer join services as s on s.zzud_service = n.zrud_service and s.code in ('10101', '10102', '10201', '10202','10203','10204','10205','10401','10402','10403','10405') 
    left outer join staff as st on st.zzud_staff = n.zrud_staff 
    left outer join groups as g on g.zzud_group = n.zrud_group and g.name = 'RCE' 
관련 문제