[英]How to get records from column_name
I need help in SQL Server, I want to get the number of identical values from a column, but I only have column names, here is my script我需要 SQL 服务器的帮助,我想从列中获取相同值的数量,但我只有列名,这是我的脚本
select
column_name, table_name, data_type, d.name, d.description
from
INFORMATION_SCHEMA.COLUMNS
inner join
Dims d on table_name = 'Dim_' + d.name + '_View'
where
column_name = 'ExtCode'
group by
column_name, table_name, data_type, d.name, d.description
having
count(column_name) > 1;
I want to get the number of records in column names that are greater than 1我想获取列名中大于 1 的记录数
I think you are trying to do two things with one SQL: 1. Find all the column names that are repeated across tables, and 2. Display each occurrence of it along with the table they occur in. Break this up into two parts.我认为您正在尝试用一个 SQL 做两件事:1. 查找跨表重复的所有列名,以及 2. 显示它的每次出现以及它们出现的表。将其分为两部分。
Start with a list of repeated column names:从重复的列名列表开始:
Select column_name From INFORMATION_SCHEMA.COLUMNS
Where table_name like 'Dim%View'
Group By column_name Having count(*)>1
Then join that to your query to retain only the columns that appear more than once:然后将其加入您的查询以仅保留出现多次的列:
select
Cols.column_name, cols.table_name, cols.data_type, d.name, d.description
from INFORMATION_SCHEMA.COLUMNS cols inner join Dims d
on table_name = 'Dim_' + d.name + '_View'
Inner Join (
Select column_name From INFORMATION_SCHEMA.COLUMNS
Where table_name like 'Dim%View'
Group By column_name Having count(*)>1
) multi On multi.column_name=cols.column_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.