如何检查两个日期之间每个月的条件

时间:2016-12-13 21:29:10

标签: sql-server

我正在为项目的堆栈交换数据库工作。(https://data.stackexchange.com/stackoverflow/query/new
我想知道每个月都有帖子的TOP 10用户都在数据库中注册。 我试过这个,但我输了:

SELECT TOP 10 
   Propriétaire 
FROM 
   (SELECT 
      count(Mois) AS nbMoisPosté, 
      datediff(month, création, getdate()) AS nbMoisTotal, 
      Propriétaire 
    FROM
       (SELECT 
          count(Posts.Id) AS nbPosts,
          month(Posts.creationDate) AS Mois,
          year(Posts.creationDate) AS Année
          Posts.ownerUserId AS Propriétaire,
          Users.creationDate AS création
       FROM 
          Posts, Users
       WHERE 
          Users.Id = Posts.ownerUserId
       GROUP BY 
          Posts.ownerUserId, 
          Mois, 
          Année, 
          création) compte
   GROUP BY 
      datediff(month, création, getdate()), 
      Propriétaire) nbMois
WHERE nbMoisPosté = nbMoisTotal

它不起作用(语法错误)但我不明白我的错误。 感谢。

1 个答案:

答案 0 :(得分:2)

尝试纠正这些问题。

请确保您发布错误消息

SELECT TOP 10 
    Propriétaire 
FROM 

    (
    SELECT 
        count(Mois) AS nbMoisPosté, 
        datediff(month, création, getdate()) AS nbMoisTotal, 
        Propriétaire 
    FROM
        (
        SELECT 
            count(Posts.Id) AS nbPosts, 
            month(Posts.creationDate) AS Mois,
            year(Posts.creationDate) AS Année   --MISSING COMMA
            Posts.ownerUserId AS Propriétaire,
            Users.creationDate AS création
        FROM 
            Posts, 
            Users
        WHERE 
            Users.Id = Posts.ownerUserId
        GROUP BY 
            Posts.ownerUserId, 
            Mois, --GROUP BY THE FUNCTION month(Posts.creationDate)
            Année, --GROUP BY THE FUNCTION year(Posts.creationDate)
            création

            ) compte
    GROUP BY 
        datediff(month, création, getdate()), 
        Propriétaire
        ) nbMois
WHERE 
    nbMoisPosté = nbMoisTotal