Sql server update语句中的计数

Sql server update语句中的计数,sql-server,tsql,count,sql-update,sql-server-2014,Sql Server,Tsql,Count,Sql Update,Sql Server 2014,我有一个带有列AmountDayOfMonth的时间表,其中值为NULL。 我需要更新此列并插入每个月的天数。 以下是我2月份的更新: update [Mart].[dbo].[Time] set AmountDayOfMonth = ( select (count(theMonth)) as kol1 from [Mart].[dbo].[Time] as t1 where t1.NameMonth = 'February' group by t1.The

我有一个带有列
AmountDayOfMonth
的时间表,其中值为
NULL
。 我需要更新此列并插入每个月的天数。 以下是我2月份的更新:

 update [Mart].[dbo].[Time]
 set AmountDayOfMonth = (
     select (count(theMonth)) as kol1 
     from [Mart].[dbo].[Time] as t1 
     where t1.NameMonth = 'February' group by t1.TheYear
 ) 

您没有指定问题,但我猜您有一些日历表,其中有一列表示每月的总天数。你只需要第二部分。只需将表名和列名更改为适当的值

数据初始化:

DECLARE @Time TABLE
    (
      TheYear INT ,
      MonthNumber VARCHAR(MAX) ,
      DayNumber INT ,
      AmountDayOfMonth INT
    );
WITH    cte
          AS ( SELECT   CAST('20160101' AS DATE) AS dt
               UNION ALL
               SELECT   DATEADD(dd, 1, dt)
               FROM     cte
               WHERE    DATEADD(dd, 1, dt) < '20170101'
             )
    INSERT  INTO @Time
            ( TheYear ,
              MonthNumber ,
              DayNumber
            )
            SELECT  YEAR(dt) ,
                    MONTH(dt) ,
                    DAY(dt)
            FROM    cte
    OPTION  ( MAXRECURSION 0 );

我不确定我是否理解你的问题。您能否编辑您的问题以包含一些示例数据和所需输出?
WITH    upd
          AS ( SELECT   * ,
                        COUNT(*) OVER ( PARTITION BY TheYear, MonthNumber ) AS cnt
               FROM     @Time
             )
    UPDATE  upd
    SET     AmountDayOfMonth = cnt
SELECT  *
FROM    @Time