如何查询最大日期? (PostgreSQL的)

时间:2015-04-02 19:02:40

标签: sql postgresql

使用PostgreSQL,我希望只能看到带有最新修改时间戳的文档ID。我很难得到这个工作,并想知道是否有人有任何指针?

这是我目前的代码:

SELECT cmsdw_document.document_id as "Document ID",
  cmsdw_activity_meta.activity_name as "Activity Name",
  cmsdw_document.title as "Title",
  cmsdw_document.creation_ts as "Creation Timestamp",
  cmsdw_document.modification_ts as "Modification Timestamp",
  cmsdw_user.firstname as "First Name",
  cmsdw_user.lastname as "Last Name",
  cmsdw_container.name as "Name",
  cmsdw_document_stats_fact.content_id as "Content ID",
  cmsdw_document_stats_fact.views as "Views",
  cmsdw_document_stats_fact.likes as "Likes",
  cmsdw_document_stats_fact.bookmarks as "Bookmarks",
  cmsdw_document_stats_fact.comments as "Comments",
  cmsdw_document_stats_fact.shares as "Shares",
  cmsdw_document_stats_fact.unique_viewers as "Unique Viewers"
FROM
  public.cmsdw_document,
  public.cmsdw_document_stats_fact,
  public.cmsdw_container,
  public.cmsdw_object,
  public.cmsdw_user,
  public.cmsdw_activity_fact,
  public.cmsdw_activity_meta
WHERE
  cmsdw_activity_fact.activity_type = cmsdw_activity_meta.activity_type AND
  cmsdw_document_stats_fact.content_id = cmsdw_object.object_id AND
  cmsdw_document.document_id = cmsdw_object.object_id AND
  cmsdw_container.container_id = cmsdw_document.container_id AND
  cmsdw_object.dw_object_id = cmsdw_activity_fact.direct_dw_object_id AND
  cmsdw_object.object_type = cmsdw_activity_fact.direct_object_type AND
  cmsdw_activity_fact.user_id = cmsdw_user.user_id AND
  cmsdw_container.name = 'Getting Started' AND
  cmsdw_object.object_type = 102 AND
  cmsdw_activity_fact.activity_type = 20;

1 个答案:

答案 0 :(得分:2)

您应该修复查询以使其具有正确的join语法 - 简单规则:永远不要在from子句中使用逗号。

对于您的查询,您可以将select替换为:

select distinct on (cmsdw_document.document_id) . . .

"。 。 "是你查询的其余部分。然后添加:

order by cmsdw_document.document_id, cmsdw_document.modification_ts desc

这应该使用Postgres扩展名为您提供最新文档。