在SQL Server中,我正在尝试按ID分组匹配的行.Null被视为通配符.
说明:匹配行是什么意思?
匹配行意味着 - 只要两行的所有列匹配.
匹配列平均值 - 每个值的相同值('A'='A')或空值('A'/'B'/'C'/ ... = NULL).
在我的例子中:
第1行与第2行匹配 - 因为:
First column: 'A' = 'A'
Second column: 'B' = NULL
Third column: NULL = 'C'
Run Code Online (Sandbox Code Playgroud)
第1行与第4行不匹配:
First column: 'A' = 'A'
Second column: 'B' != 'D'
Third column: NULL = NULL.
Run Code Online (Sandbox Code Playgroud)
比较失败,因为第二列中的值不匹配.
任何人都可以帮助我使用SQL吗?
例如:
用于创建测试表:
create table test_table
(
id int,
column1 varchar(20),
column2 varchar(20),
column3 varchar(20)
);
insert into test_table (id, column1, column2, column3) values
(1, 'A', 'B', NULL),
(2, 'A',NULL, 'C'),
(3, …Run Code Online (Sandbox Code Playgroud) 我试图只选择其他组中未包含的组.
在该示例中,组号2包含在组号1中,因为组号1具有组号2的所有值.
组号3不包含在组号1中,因为它具有组号1不包含的值50.
结果应该是1号和3号组.
(或者相反 - 只获得包含在其他组中的组号2)
寻找没有循环的方式,因为我有超过200万的价值观.
我的表看起来像这样:
group_number id
-------------------
1 10
1 20
1 30
1 40
2 10
2 40
3 10
3 30
3 50
Run Code Online (Sandbox Code Playgroud)