【问题标题】:SQL Join Tables - 2nd newest entry to related main entry in other tableSQL Join Tables - 其他表中相关主条目的第二个最新条目
【发布时间】:2014-12-13 05:03:29
【问题描述】:

遵循抽象场景。我有 2 张桌子。一个包含“posts”,另一个包含“cmets” 如果您创建一个新的“帖子”,还会添加一个“初始”类型的新“评论”。每当您现在添加真实评论时,它都会获取 postID 和“图片”、“文本”、“电影”之类的类型......

我现在需要一个表格来显示最初的帖子日期和第一个“真实”评论。我想知道写第一条评论需要多长时间。

SELECT post.ID,post.Title,post.Author,post.CreateDate,
comment.Type,comment.Text,comment.CreateDate
FROM Posts as post
INNER JOIN Comments as comments on post.ID = comment.PostID

这给了我关于 cmets 数量的发布信息。 现在我只想将第一个“真实”评论日期放在帖子的 createDate 旁边(其中 comment.Type != 'initial')

不知道该怎么做。在这种情况下有人可以帮助我吗? 应该可以在 MS SQL 2000 上运行它...

提前谢谢...

【问题讨论】:

    标签: sql sql-server tsql join sql-server-2000


    【解决方案1】:

    试试这个:

    SELECT post.ID,max(post.Title),max(post.Author),min(post.CreateDate),
    min(comment.CreateDate),
    datediff(mi,min(post.CreateDate),min(comment.CreateDate)) as 'first comment in minutes'
    FROM Posts as post
    INNER JOIN Comments as comments on post.ID = comment.PostID
    where  comment.Type != 'initial'
    group by post.id
    

    【讨论】:

    • 这很好用!谢谢你。如果你有时间,你能补充一个简短的解释吗?
    • 我很高兴它有帮助。看看这个聚合函数:msdn.microsoft.com/en-us/library/ms173454.aspx
    • 我使用min(comment.CreateDate) 确定过滤where comment.Type != 'initial' 后的第一个真实评论
    猜你喜欢
    • 1970-01-01
    • 2013-02-28
    • 2018-10-12
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多