简体   繁体   中英

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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