查找比较表列的顶部匹配项
我有一个数据库,最多可从不同来源混合400个桌子。我需要按列相似性将这些表分组到Excel文件中(考虑到表具有0、1、2或所有具有相同名称的列)。挑战是示例如下:
fac.table_1 have columns C1, C2, C3, C4 and C5
dim.table_2 has columns C1, C3, and C5
stg.table_3 has columns C1, C6, and C7
stg.table_4 has columns C2, and C99
...
预期的结果应该是:
sch_name | table_name | ncols | nmatches
dim | table_2 | 3 | 3
stg | table_3 | 3 | 1
stg | table_4 | 2 | 1
我认为方法是将类似代码与计数或相交的内容一起使用,将我想与他人进行比较的表名中的位置:
SELECT
schemas.name sch_nm,
tables.name tb_nm,
columns.name col_nm
FROM sys.tables
LEFT JOIN sys.columns ON tables.object_id = columns.object_id
LEFT JOIN sys.schemas ON tables.schema_id = schemas.schema_id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想计算另一个表中列名称的列数吗?
结果:
db<>小提琴演示
You wanted to count the number of columns which the column name exists in another table ?
Result :
db<>fiddle demo