2013-08-24 68 views
0

以下查詢允許我監視日期範圍內執行了哪些服務。我也修改它以顯示最後的「X」天。查找在日期範圍內沒有活動的客戶

這是我的問題。我需要找到在日期範圍內沒有任何活動的所有客戶。我看過使用左外連接 (http://www.postgresqlforbeginners.com/2010/11/sql-outer-joins.html)的示例並試圖遵循它,但結果沒有「缺失」服務會顯示。我需要看到「鮑勃」在過去的「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() 
) 

上述反連接可以被轉換爲左外連接以這種方式:

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的 - 感謝這麼多的轉向我到正確的方向。我試着用最後一個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和移動環境移至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' 
相關問題