0

下面的查询允许我监控在某个日期范围内执行了哪些服务。我还对其进行了修改以显示最后的“X”天。

这是我的问题。我需要找到在某个日期范围内没有活动的所有客户。我已经看到使用左外连接的示例(http://www.postgresqlforbeginners.com/2010/11/sql-outer-joins.html)并尝试遵循它,但结果不会显示“缺失”服务。我需要看到在过去的“X”天内没有看到“Bob”,而不是确实发生的活动。

所以,如果有人可以看看这个并引导我找到解决方案,我将非常感激

这是查询:

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

谢谢

4

2 回答 2

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'
于 2013-08-25T05:27:54.220 回答
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
于 2013-08-24T22:52:08.857 回答