简体   繁体   English

如何从 column_name 获取记录

[英]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.

相关问题 如何从DML语句获取column_name和table_name? - How to get column_name and table_name from DML statement? 无论查询中的 where 子句如何,如何从表中获取 max(column_name)? - How to get max(column_name) from a table irrespective of a where clause in the query? 如何在(子查询)中选择 Where column_name - how to select Where column_name in (subquery) SQL: WITH 子句如何引用 column_name - SQL: WITH clause how to refer to column_name 列名 > “ ”? - Column_name > “ ”? 我如何获得所有表名,如“ column_name”这样的列。 难过的是,我只需要在'column_name'中具有特定值的表 - How do I get all table names where a column like 'column_name'. The catch is I want only table that have a specific value in the 'column_name' 从表中选择column_name,其中column_name = all_values - select column_name from table where column_name = all_values SQL:如何使用信息架构中的Table_Name和Pivoted Column_Name构建选择查询 - SQL: How to build a select query with Table_Name and Pivoted Column_Name from Information Schema ORACLE SQL-如何在表中的COLUMN_NAME之间进行搜索并获取价值 - ORACLE SQL-How can i search between COLUMN_NAME in a Table and get value “SELECT column_name FROM table_name”意外结果 - "SELECT column_name FROM table_name" unexpected result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM