统计匹配值并以矩阵形式显示
Count the match values and display in matrix form
我有以下 tables 和一些示例数据:
Table: TblTestDB
CREATE TABLE TblTestDB (id int,name varchar(100));
INSERT INTO TblTestDB VALUES(1,'Sam'),(2,'Jack'),
(3,'Rock'),(4,'Don'),(5,'Tam');
Table: TblDB1
CREATE TABLE TblDB1 (id int,name varchar(100));
INSERT INTO TblDB1 VALUES(1,'Sam'),(2,'Jack'),
(3,'Rock');
Table: TblDB2
CREATE TABLE TblDB2 (id int,name varchar(100));
INSERT INTO TblDB2 VALUES(1,'Jazz'),(2,'Dsouze'),
(3,'Rock'),(4,'Jack');
Table: TblDB3
CREATE TABLE TblDB3 (id int,name varchar(100));
INSERT INTO TblDB3 VALUES(1,'Sam'),(2,'Jazz'),
(3,'Rock');
我想以以下形式显示结果:
TableName Name ID
------------------------------
TblDB1 3 3
TblDB2 2 4
TblDB3 2 3
关于结果集的解释:我想显示在 table TblTestDB
和其他(TblDB1
,TblDB2
,[=19]之间匹配的列值的计数=]).
尝试了以下查询:
查询:
SELECT DB,MAX(Name) AS Name, MAX(ID) AS ID
FROM
(
SELECT 'TblDB1' AS DB,COUNT(a.Name) AS Name,0 AS ID
FROM TblTestDB a
INNER JOIN TblDB1 b ON a.Name = b.Name
UNION
SELECT 'TblDB2' AS DB,COUNT(a.Name) AS Name,0 AS ID
FROM TblTestDB a
INNER JOIN TblDB2 b ON a.Name = b.Name
UNION
SELECT 'TblDB3' AS DB,COUNT(a.Name) AS Name,0 AS ID
FROM TblTestDB a
INNER JOIN TblDB3 b ON a.Name = b.Name
UNION
SELECT 'TblDB1' AS DB,0 AS Name,COUNT(a.ID) AS ID
FROM TblTestDB a
INNER JOIN TblDB1 b ON a.ID = b.ID
UNION
SELECT 'TblDB2' AS DB,0 AS Name,COUNT(a.ID) AS ID
FROM TblTestDB a
INNER JOIN TblDB2 b ON a.ID = b.ID
UNION
SELECT 'TblDB3' AS DB,0 AS Name,COUNT(a.ID) AS ID
FROM TblTestDB a
INNER JOIN TblDB3 b ON a.ID = b.ID
) a
GROUP BY DB
问题:我可能会显示 10 多个列,如 matrix/pivot,上面的查询根据列列表增长。
任何让它简短的更好方法。
SELECT DISTINCT 'TblDB1' AS DB
,COUNT(CASE WHEN a.Name = b.Name THEN 1 ELSE NULL END) AS Name
,COUNT(CASE WHEN a.ID = b.ID THEN 1 ELSE NULL END) AS ID
FROM TblTestDB a
CROSS JOIN TblDB1 b
UNION ALL
SELECT DISTINCT 'TblDB2' AS DB
,COUNT(CASE WHEN a.Name = b.Name THEN 1 ELSE NULL END) AS Name
,COUNT(CASE WHEN a.ID = b.ID THEN 1 ELSE NULL END) AS ID
FROM TblTestDB a
CROSS JOIN TblDB2 b
UNION ALL
SELECT DISTINCT 'TblDB3' AS DB
,COUNT(CASE WHEN a.Name = b.Name THEN 1 ELSE NULL END) AS Name
,COUNT(CASE WHEN a.ID = b.ID THEN 1 ELSE NULL END) AS ID
FROM TblTestDB a
CROSS JOIN TblDB3 b
这个够简单了吧?
SELECT
tableName, sum(s.[Name]) as ncnt, max(s.id) as ID
FROM TblTestDB t
cross apply (
SELECT tableName, count(name) as [name], max(id) as ID
FROM (
SELECT 'TblDB1' as tid, * FROM TblDB1
union
SELECT 'TblDB2' as tid, * FROM TblDB2
union
SELECT 'TblDB3' as tid, * FROM TblDB3
) i
where i.name = t.name
group by tableName
) s
group by tableName
不确定你的意思,"I may get more than 10 columns to show like matrix/pivot"。
例如,如果您要添加一个 Surname 列,它应该单独匹配,还是与 Name 列结合使用? (前者可能有点复杂...)
我有以下 tables 和一些示例数据:
Table: TblTestDB
CREATE TABLE TblTestDB (id int,name varchar(100));
INSERT INTO TblTestDB VALUES(1,'Sam'),(2,'Jack'),
(3,'Rock'),(4,'Don'),(5,'Tam');
Table: TblDB1
CREATE TABLE TblDB1 (id int,name varchar(100));
INSERT INTO TblDB1 VALUES(1,'Sam'),(2,'Jack'),
(3,'Rock');
Table: TblDB2
CREATE TABLE TblDB2 (id int,name varchar(100));
INSERT INTO TblDB2 VALUES(1,'Jazz'),(2,'Dsouze'),
(3,'Rock'),(4,'Jack');
Table: TblDB3
CREATE TABLE TblDB3 (id int,name varchar(100));
INSERT INTO TblDB3 VALUES(1,'Sam'),(2,'Jazz'),
(3,'Rock');
我想以以下形式显示结果:
TableName Name ID
------------------------------
TblDB1 3 3
TblDB2 2 4
TblDB3 2 3
关于结果集的解释:我想显示在 table TblTestDB
和其他(TblDB1
,TblDB2
,[=19]之间匹配的列值的计数=]).
尝试了以下查询:
查询:
SELECT DB,MAX(Name) AS Name, MAX(ID) AS ID
FROM
(
SELECT 'TblDB1' AS DB,COUNT(a.Name) AS Name,0 AS ID
FROM TblTestDB a
INNER JOIN TblDB1 b ON a.Name = b.Name
UNION
SELECT 'TblDB2' AS DB,COUNT(a.Name) AS Name,0 AS ID
FROM TblTestDB a
INNER JOIN TblDB2 b ON a.Name = b.Name
UNION
SELECT 'TblDB3' AS DB,COUNT(a.Name) AS Name,0 AS ID
FROM TblTestDB a
INNER JOIN TblDB3 b ON a.Name = b.Name
UNION
SELECT 'TblDB1' AS DB,0 AS Name,COUNT(a.ID) AS ID
FROM TblTestDB a
INNER JOIN TblDB1 b ON a.ID = b.ID
UNION
SELECT 'TblDB2' AS DB,0 AS Name,COUNT(a.ID) AS ID
FROM TblTestDB a
INNER JOIN TblDB2 b ON a.ID = b.ID
UNION
SELECT 'TblDB3' AS DB,0 AS Name,COUNT(a.ID) AS ID
FROM TblTestDB a
INNER JOIN TblDB3 b ON a.ID = b.ID
) a
GROUP BY DB
问题:我可能会显示 10 多个列,如 matrix/pivot,上面的查询根据列列表增长。 任何让它简短的更好方法。
SELECT DISTINCT 'TblDB1' AS DB
,COUNT(CASE WHEN a.Name = b.Name THEN 1 ELSE NULL END) AS Name
,COUNT(CASE WHEN a.ID = b.ID THEN 1 ELSE NULL END) AS ID
FROM TblTestDB a
CROSS JOIN TblDB1 b
UNION ALL
SELECT DISTINCT 'TblDB2' AS DB
,COUNT(CASE WHEN a.Name = b.Name THEN 1 ELSE NULL END) AS Name
,COUNT(CASE WHEN a.ID = b.ID THEN 1 ELSE NULL END) AS ID
FROM TblTestDB a
CROSS JOIN TblDB2 b
UNION ALL
SELECT DISTINCT 'TblDB3' AS DB
,COUNT(CASE WHEN a.Name = b.Name THEN 1 ELSE NULL END) AS Name
,COUNT(CASE WHEN a.ID = b.ID THEN 1 ELSE NULL END) AS ID
FROM TblTestDB a
CROSS JOIN TblDB3 b
这个够简单了吧?
SELECT
tableName, sum(s.[Name]) as ncnt, max(s.id) as ID
FROM TblTestDB t
cross apply (
SELECT tableName, count(name) as [name], max(id) as ID
FROM (
SELECT 'TblDB1' as tid, * FROM TblDB1
union
SELECT 'TblDB2' as tid, * FROM TblDB2
union
SELECT 'TblDB3' as tid, * FROM TblDB3
) i
where i.name = t.name
group by tableName
) s
group by tableName
不确定你的意思,"I may get more than 10 columns to show like matrix/pivot"。 例如,如果您要添加一个 Surname 列,它应该单独匹配,还是与 Name 列结合使用? (前者可能有点复杂...)