对于 table 服务器中未指定数量的 table 共享的列中的每个 table,如何计算不同的值?
How to count, for every table, distinct values in a column that is shared by an unspecified number of tables in SQL Server?
我在 SQL 服务器数据库中有一个流动的和不确定的视图数量,其中有一个名为 "CONFIDENCE" 的列。对于该集合中的每个视图,我想计算不同值在该列中出现的频率。
我知道如何找到所有包含 "CONFIDENCE" 列的视图...
select c.table_schema as schema_name,
c.table_name as name
from information_schema.columns c
join information_schema.tables t
on c.table_name = t.table_name
where c.column_name = 'confidence'
and t.table_type = 'view'
而且我知道如何计算单个视图的不同值...
select distinct confidence,
count(*) as occurrences
from schema.view_name
group by confidence
order by confidence;
但我不知道如何将两者联系起来。我怀疑这与 cross apply
或 union all
有关,但我只能找到用于合并固定数量对象的资源。
你需要知道一个查询的答案才能编写另一个,你可以用一点 DSQL 来解决这个问题。
DECLARE @SQL NVARCHAR(MAX) = ''
SELECT @SQL = @SQL + 'UNION ALL SELECT ''' + c.table_schema + '.' + c.table_name + ''' tbl, confidence, count(*) as occurrences FROM [' + c.table_schema + '].[' + c.table_name + '] GROUP BY confidence
'
FROM information_schema.columns c
JOIN information_schema.tables t ON c.table_name = t.table_name
WHERE c.column_name = 'confidence'
AND t.table_type = 'view'
SET @SQL = RIGHT(@SQL, LEN(@SQL) - 9)
EXEC sp_executesql @SQL
我在 SQL 服务器数据库中有一个流动的和不确定的视图数量,其中有一个名为 "CONFIDENCE" 的列。对于该集合中的每个视图,我想计算不同值在该列中出现的频率。
我知道如何找到所有包含 "CONFIDENCE" 列的视图...
select c.table_schema as schema_name,
c.table_name as name
from information_schema.columns c
join information_schema.tables t
on c.table_name = t.table_name
where c.column_name = 'confidence'
and t.table_type = 'view'
而且我知道如何计算单个视图的不同值...
select distinct confidence,
count(*) as occurrences
from schema.view_name
group by confidence
order by confidence;
但我不知道如何将两者联系起来。我怀疑这与 cross apply
或 union all
有关,但我只能找到用于合并固定数量对象的资源。
你需要知道一个查询的答案才能编写另一个,你可以用一点 DSQL 来解决这个问题。
DECLARE @SQL NVARCHAR(MAX) = ''
SELECT @SQL = @SQL + 'UNION ALL SELECT ''' + c.table_schema + '.' + c.table_name + ''' tbl, confidence, count(*) as occurrences FROM [' + c.table_schema + '].[' + c.table_name + '] GROUP BY confidence
'
FROM information_schema.columns c
JOIN information_schema.tables t ON c.table_name = t.table_name
WHERE c.column_name = 'confidence'
AND t.table_type = 'view'
SET @SQL = RIGHT(@SQL, LEN(@SQL) - 9)
EXEC sp_executesql @SQL