我已经思考了相关的帖子,但无法获得成功。我需要一份报告,显示客户的最后一条注释。我真的很接近,但查询通常会“错过”实际的最后一个条目。假设我运行报告以显示过去 6 个月的最后一个条目,并将“bob”的最后一个条目显示为 8/1/13。当我查看表格时,有一个关于 10/23/13 的注释。我检查了数据是否以某种方式搞砸了,但看起来不错。如果有人能阐明为什么我在查询中有一些“几乎是最后的注释”而不是实际的最新注释,我将非常感激。这是我使用的查询和解释它的评论:
SELECT
DISTINCT ON (c.client_id) --the client id
g.name, -- the office location
MAX (n.date_service),
n.date_creation,
sv.code, -- the service / activity code
c.name_lastfirst_cs, -- client's name
n.date_service,
s.staff_id -- staff id
FROM notes n, clients c, services sv, staff s, groups g
WHERE n.zrud_service = sv.zzud_service
AND n.visibility_flag = '1' -- 1 = valid note
AND n.zrud_client = c.zzud_client
AND n.zrud_staff = s.zzud_staff
AND n.zrud_group = g.zzud_group
AND g.name = 'HALIFAX' -- am specifying a single location
-- the next 4 lines were an attempt to query specific time range and actitity codes
--AND s.code IN ('10101', '10102', '10201', 10202','10203','10204','10205','10401','10402','10403','10405')
-- AND n.date_service >= '1/1/2010'
-- AND n.date_service<= '11/7/2013'
-- AND n.date_service BETWEEN (now() - '800 days'::interval)::timestamp AND now()
GROUP BY g.name,sv.code, c.client_id, c.name_lastfirst_cs, s.staff_id,
n.date_service, n.date_creation