简体   繁体   English

SQL-COUNT数据库中所有表中具有WHERE条件的所有行

[英]SQL- COUNT all Rows with WHERE condition in all tables in database

I need a query that COUNT all row´s (with WHERE condition) in all tables in a database, where the table_names are unknown. 我需要一个查询,该数据库在table_names未知的数据库的所有表中计数所有行(带有WHERE条件)。 Reason why is: I got a "alarm_logging_system" that creates a new "archive" table every month or so, and automatically names it "AlarmLog(timestamp of last active alarm)" 原因是:我得到了一个“ alarm_logging_system”,它每个月左右创建一个新的“存档”表,并自动将其命名为“ AlarmLog(上次活动警报的时间戳)”

So I need a query which dynamically search through all tables that exists in the database, COUNTS all row in a column with WHERE condition, and return one single value. 因此,我需要一个查询,该查询可动态搜索数据库中存在的所有表,使用WHERE条件对一列中的所有行进行COUNTS,并返回一个单个值。

In example I want to get all active alarms, this month, last month, etc. Or a specific time range. 例如,我想获取所有活动警报,本月,上个月等。或特定时间范围。

This is an example of a query I wrote to get the count for all active alarms last month 这是我编写的查询示例,用于获取上个月所有活动警报的计数

SELECT COUNT(ConditionActive)  
FROM (whole database)???  
WHERE (Acked=0 AND ConditionActive = 1) 
AND (EventTime >= DATEADD(m,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()), 0)) 
AND [EventTime] <= DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))))
AS ACTIVE_LAST_MONTH

So what I need is a query, stored_procedure or a dynamic SQL query? 所以我需要的是查询,stored_procedure或动态SQL查询?

All the tables have the same schema and columns. 所有表都具有相同的架构和列。

Appreciate all help! 感谢所有帮助!

This should demonstrate why it is not generally considered a good practice to make multiple copies of the same table and then some aggregate data from the whole collection. 这应该证明为什么通常不认为最好先制作同一张表的多个副本,然后再从整个集合中汇总一些数据。 This just isn't how relational databases are designed to work. 这并不是关系数据库设计的工作方式。

This is untested because I don't have your table anywhere to work with but this should get it. 这是未经测试的,因为我没有任何地方可以使用您的表,但这应该可以解决。

declare @SQL nvarchar(max) = ''

--This get a result set of the count for all tables.
select @SQL = @SQL + 'select count(ConditionActive) as MyCount 
    from [' + t.name + '] 
    where Acked = 0 
    AND ConditionActive = 1 
    AND EventTime >= DATEADD(month, -1, DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)) 
    and [EventTime] <= DATEADD(day, -1, DATEADD(month, DATEDIFF(month, 0, GETDATE()),0)) UNION ALL'
from sys.tables t

--Now we need the sum of all counts
select @SQL = 'Select sum(MyCount) from (' + LEFT(@SQL, LEN(@SQL) - 10) + ') as x'

select @SQL

--uncomment the line below when you are confident that the dynamic sql is correct.
--exec sp_executesql @SQL

--EDIT-- I took the liberty of expanding the shortcuts in your DATEADD functions. -编辑-我自由扩展了DATEADD函数中的快捷方式。 The shortcuts are hard to remember and your were using both mm and m which both mean month. 快捷方式很难记住,您使用的mm和m均表示月份。 It is generally a better approach to just spell out the word to remove any ambiguity. 通常,仅拼出单词以消除任何歧义是更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM