Sql 计算在其他对象修改日期之后创建的对象数

Sql 计算在其他对象修改日期之后创建的对象数,sql,tsql,Sql,Tsql,我正在尝试统计t-SQL查询中特定日期之后创建的所有帖子: select a.ParentID as [Post Link] , count(DISTINCT a.id) as [Answers after edition] , q.LastEditDate as [Last Edition] , a.creationdate as [Answered On] , DATEDIFF(hour, q.LastEditDate, a.creationdate)

我正在尝试统计t-SQL查询中特定日期之后创建的所有帖子:

select a.ParentID as [Post Link]
     , count(DISTINCT a.id) as [Answers after edition]
     , q.LastEditDate as [Last Edition]
     , a.creationdate as [Answered On]
     , DATEDIFF(hour, q.LastEditDate, a.creationdate) as [Hours after edition]
from posts a 
inner join posts q on q.id = a.ParentID
where q.posttypeid = 1 -- Q
and a.posttypeid = 2 -- A
and q.LastEditorUserId = ##userid##
and a.creationdate > q.LastEditDate
and q.AnswerCount > 1
group by a.ParentID
其结果是:

Column 'posts.LastEditDate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
此查询是对其他查询的修改:

select q.id as [Post Link]
     , q.owneruserid as [User Linkl]
     , q.LastEditDate as [Last Edition]
     , a.creationdate as [Answered On]
     , DATEDIFF(hour, q.LastEditDate, a.creationdate) as [Hours after edition]
from posts q 
inner join posts a on a.id = q.AcceptedAnswerId
where q.posttypeid = 1 -- Q
and a.posttypeid = 2 -- A
and q.LastEditorUserId = ##userid##
and a.creationdate > q.LastEditDate
order by [Hours after edition]
变化在于后者仅比较存在AcceptedAnswerId的帖子。我想做同样的事情,但这次将具有相同q.Id的分组

这正是我想要的:

select a.id as [Answer Link]
     , q.owneruserid as [User Linkl]
     , q.LastEditDate as [Last Edition]
     , a.creationdate as [Answered On]
     , DATEDIFF(hour, q.LastEditDate, a.creationdate) as [Hours after edition]
from posts a 
inner join posts q on q.id = a.ParentID
where q.posttypeid = 1 -- Q
and a.posttypeid = 2 -- A
and q.LastEditorUserId = ##userid##
and a.creationdate > q.LastEditDate
and q.AnswerCount > 1
但不按ParentId分组

Rhis正是我所期望的:

Post Link   | Answer Count | Hours after edition
http://link |            3 |                   3

我不太想在编辑后保留几个小时,但如果能保留得更好的话。

将评论转化为答案。在您的第一段代码中:

 , q.LastEditDate as [Last Edition]
 , a.creationdate as [Answered On]
您在这方面遇到了一个错误,因为它们不在GROUPBY语句中,并且脚本不知道如何处理多个值。如果您将它们包括在group by中,您可能会为每个父ID获得几行。我认为您希望获取最大日期作为最后一次编辑,最小日期作为第一次创建


顺便说一下……我想你一定是在MSSQL上。如果您在MySQL中,它会在没有GROUPBY的情况下执行您的查询,并随机填充这两个字段。哎呀

其中是列上的筛选器,是聚合上的筛选器。因为我们正在将它们移动到maxdate或mindate,它们正在成为聚合,需要在having子句中,而不是where子句中。从您需要的订单所在的组中进行选择

Braiam提供的最终脚本:

select a.ParentId as [Post Link]
 , count(DISTINCT a.id) as [Answers after edition]
 --, q.LastEditDate as [Last Edition]
 --, a.creationdate as [Answered On]
 --, DATEDIFF(hour, q.LastEditDate, a.creationdate) as [Hours after edition]
from posts a 
inner join posts q on q.id = a.ParentID
where q.posttypeid = 1 -- Q
and a.posttypeid = 2 -- A
and q.LastEditorUserId = ##userid##
and q.AnswerCount > 1
group by a.ParentId
having max(q.LastEditDate) < min(a.creationdate)

如果一个父ID找到多个LastEditDate或creationdate,您是否有任何逻辑可以使用哪一个LastEditDate或creationdate?它返回的“posts.lastedDate”错误无效,因为没有关于如何处理它的多个值的说明。第一个查询的简单解决方案是使用maxlasteditdate和mincreationdate。其中,列上有一个筛选器,聚合上有一个筛选器。在group by parentID下,输入mina.creationdate>maxq.lastedDate。它是从groupby Having的位置选择的我想你一定是在MSSQL上这里不,使用SEDE和T-SQL,是的,在阅读后消除了错误。啊…是猜测给定的tsql,而不是mysql。我们应该清理这个问题…介意我编辑我的问题,包括你的最后脚本,我们会留下来作为你问题的答案吗?