为多个表提取前三到五个值

时间:2014-03-18 14:49:29

标签: sql multiple-columns multiple-tables

考虑以下代码,用于标识表名与给定命名约定匹配的表和列:

SELECT t.name AS table_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE t.name LIKE '%TABLE%'
ORDER BY table_name

此代码的结果如下所示:

TableNm | ColNm
Table 1 | Column 1
Table 1 | Column 2
Table 1 | Column 3
Table 2 | Column 1
Table 2 | Column 2
Table 2 | Column 3
Table 2 | Column 4
Table 3 | Column 1
Table 3 | Column 2

我想通过在每列中提供前三到五个值(在SELECT TOP#sense中,不一定是最常用的值)来修改此代码,这样我们就可以知道每列中存储了哪些数据对于多个表。例如:

TableNm | ColNm    | TopVal1     | TopVal2     | TopVal3
Table 1 | Column 1 | Top Value 1 | Top Value 2 | Top Value 3
Table 1 | Column 2 | Top Value 1 | Top Value 2 | Top Value 3
Table 1 | Column 3 | Top Value 1 | Top Value 2 | Top Value 3
Table 2 | Column 1 | Top Value 1 | Top Value 2 | Top Value 3
Table 2 | Column 2 | Top Value 1 | Top Value 2 | Top Value 3
Table 2 | Column 3 | Top Value 1 | Top Value 2 | Top Value 3
Table 2 | Column 4 | Top Value 1 | Top Value 2 | Top Value 3
Table 3 | Column 1 | Top Value 1 | Top Value 2 | Top Value 3
Table 3 | Column 2 | Top Value 1 | Top Value 2 | Top Value 3

0 个答案:

没有答案