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条件を使用して移動する必要があると思いますjoinwhere

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 に答える