根据第三列中的日期从另一列 table 中的 COUNT(*) 更新一列

update one column from COUNT(*) in another table depending on date in a third column

我有一个插入查询,我想重做一个更新查询 我希望更新的数据库如下所示:

Date (date, Null)
Hour (int, Null)
No of Alarms Stockholm (int, Null)
No of Alarms Sverige (int, Null)

我的插入查询看起来像:

INSERT INTO [LABLOG].dbo.[AlarmStatistic] (
    [Date]
    ,[Hour]
    ,[No of Alarms Sverige]
    )
SELECT CAST([Event Date] AS DATE) AS [DATE]
    ,DATEPART(hour, [Event Date]) AS [Hour]
    ,COUNT(*) AS [No of Alarms Sverige]
FROM MYSERVER01.[SESVELOG].[dbo].[STATISTIC]
WHERE [Event Date] >= '2014-07-01'
    AND [Event Date] < '2014-12-01'
    AND [event text] LIKE 'Alarm.BSI Point - Alarm type: On; Alarm status: Normal; Event operator:%RAPID%; Presentation group:%'
GROUP BY CAST([Event Date] AS DATE)
    ,DATEPART(hour, [Event Date])

阅读几篇论坛帖子后,我得出以下结论

UPDATE [LABLOG].[dbo].[AlarmStatistic]
SET [No of Alarms Sverige] = (
        SELECT CAST([Event Date] AS DATE) AS [DATE]
            ,DATEPART(hour, [Event Date]) AS [Hour]
            ,COUNT(*) AS [No of Alarms Sverige]
        FROM MYSERVER01.[SESVELOG].[dbo].[STATISTIC]
        WHERE [Event Date] >= '2014-07-01'
            AND [Event Date] < '2014-12-01'
            AND [event text] LIKE 'Alarm.BSI Point - Alarm type: On; Alarm status: Normal; Event operator:%RAPID%; Presentation group:%'
        GROUP BY CAST([Event Date] AS DATE)
            ,DATEPART(hour, [Event Date])
        )

虽然这不起作用,我明白了

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

我想做的是根据一列中的文本计算一台服务器中的行数,并在新 table.

中按小时显示行数

您正在使用子查询为 [No of Alarms Sverige] 赋值。您的子查询应该 return 只有一个值。但实际上你的子查询 returns:

  • 很多行(一般情况下)
  • 许多列(在所有情况下)

我认为以下查询应该有所帮助:

UPDATE [LABLOG].[dbo].[AlarmStatistic]
SET [No of Alarms Sverige] = (
        SELECT COUNT(*) AS [No of Alarms Sverige]
        FROM MYSERVER01.[SESVELOG].[dbo].[STATISTIC]
        WHERE [Event Date] >= '2014-07-01'
            AND [Event Date] < '2014-12-01'
            AND [event text] LIKE 'Alarm.BSI Point - Alarm type: On; Alarm status: Normal; Event operator:%RAPID%; Presentation group:%'
            AND CAST([Event Date] AS DATE) = [Date]
            AND DATEPART(hour, [Event Date]) = [Hour]
        )