通过比较两个表来识别缺失的组合
Identify the missing combinations by comparing two tables
我的主栏中有 3 列 table。
- 得分(0-99,100-110)
- 率 (5-9 ,10-15)
- 地点(A、B)
我有这些的所有组合(2 * 2 * 2 = 8 种组合)
下面是我主要的组合table
score rate location
----------------------------
0-99 5-9 A
100-110 5-9 A
0-99 10-15 A
100-110 10-15 A
0-99 5-9 B
100-110 5-9 B
0-99 10-15 B
100-110 10-15 B
我还有另一个 table 包含实际数据。我想找出实际table中所有缺失的组合。如何找到那些缺失的组合并附加到列中值为“0”的实际 table?
实际数据
score rate location value
---------------------------------
0-99 10-15 A 3
100-110 10-15 A 6
0-99 10-15 B 1
预期输出
score rate location value
------------------------------------
0-99 5-9 A 0
0-99 10-15 A 3
100-110 10-15 A 6
100-110 5-9 B 0
0-99 10-15 B 1
100-110 5-9 A 0
100-110 10-15 B 0
0-99 10-15 B 0
对 maintable 和 actualtable 使用 left join 然后应用 case when with value column
select t.score,t.rate,t.location, case when value is null then 0 else value end as value
from t left join t1
on t.score=t1.score and t.rate=t1.rate
and t.location=t1.location
根据您的实际数据和预期结果 score
、rate
、location
列值似乎是固定的,因此您可以使用 UNION ALL
让 score
,rate
,location
为tables.
CROSS JOIN
为score
、rate
、location
Union table生成笛卡尔积,求全table。
然后OUTER JOIN
create table t(
score varchar(50),
rate varchar(50),
location varchar(50),
value int
);
insert into t values ('0-99','10-15','A',3);
insert into t values ('100-110','10-15','A',6);
insert into t values ('0-99','10-15','B',1);
查询 1:
SELECT
s.score,
r.rate,
l.location,
coalesce(t1.value,0)
FROM
(
SELECT '0-99' score
UNION ALL
SELECT '100-110'
) s
CROSS JOIN
(
SELECT '10-15' rate
UNION ALL
SELECT '5-9'
) r
CROSS JOIN
(
SELECT 'A' as "location"
UNION ALL
SELECT 'B'
) l
LEFT JOIN t t1 on s.score = t1.score and t1.rate = r.rate and t1.location = l.location
ORDER BY l.location
| score | rate | location | coalesce |
|---------|-------|----------|----------|
| 0-99 | 10-15 | A | 3 |
| 0-99 | 5-9 | A | 0 |
| 100-110 | 10-15 | A | 6 |
| 100-110 | 5-9 | A | 0 |
| 100-110 | 5-9 | B | 0 |
| 0-99 | 10-15 | B | 1 |
| 100-110 | 10-15 | B | 0 |
| 0-99 | 5-9 | B | 0 |
我的主栏中有 3 列 table。
- 得分(0-99,100-110)
- 率 (5-9 ,10-15)
- 地点(A、B)
我有这些的所有组合(2 * 2 * 2 = 8 种组合)
下面是我主要的组合table
score rate location
----------------------------
0-99 5-9 A
100-110 5-9 A
0-99 10-15 A
100-110 10-15 A
0-99 5-9 B
100-110 5-9 B
0-99 10-15 B
100-110 10-15 B
我还有另一个 table 包含实际数据。我想找出实际table中所有缺失的组合。如何找到那些缺失的组合并附加到列中值为“0”的实际 table?
实际数据
score rate location value
---------------------------------
0-99 10-15 A 3
100-110 10-15 A 6
0-99 10-15 B 1
预期输出
score rate location value
------------------------------------
0-99 5-9 A 0
0-99 10-15 A 3
100-110 10-15 A 6
100-110 5-9 B 0
0-99 10-15 B 1
100-110 5-9 A 0
100-110 10-15 B 0
0-99 10-15 B 0
对 maintable 和 actualtable 使用 left join 然后应用 case when with value column
select t.score,t.rate,t.location, case when value is null then 0 else value end as value
from t left join t1
on t.score=t1.score and t.rate=t1.rate
and t.location=t1.location
根据您的实际数据和预期结果 score
、rate
、location
列值似乎是固定的,因此您可以使用 UNION ALL
让 score
,rate
,location
为tables.
CROSS JOIN
为score
、rate
、location
Union table生成笛卡尔积,求全table。
然后OUTER JOIN
create table t(
score varchar(50),
rate varchar(50),
location varchar(50),
value int
);
insert into t values ('0-99','10-15','A',3);
insert into t values ('100-110','10-15','A',6);
insert into t values ('0-99','10-15','B',1);
查询 1:
SELECT
s.score,
r.rate,
l.location,
coalesce(t1.value,0)
FROM
(
SELECT '0-99' score
UNION ALL
SELECT '100-110'
) s
CROSS JOIN
(
SELECT '10-15' rate
UNION ALL
SELECT '5-9'
) r
CROSS JOIN
(
SELECT 'A' as "location"
UNION ALL
SELECT 'B'
) l
LEFT JOIN t t1 on s.score = t1.score and t1.rate = r.rate and t1.location = l.location
ORDER BY l.location
| score | rate | location | coalesce |
|---------|-------|----------|----------|
| 0-99 | 10-15 | A | 3 |
| 0-99 | 5-9 | A | 0 |
| 100-110 | 10-15 | A | 6 |
| 100-110 | 5-9 | A | 0 |
| 100-110 | 5-9 | B | 0 |
| 0-99 | 10-15 | B | 1 |
| 100-110 | 10-15 | B | 0 |
| 0-99 | 5-9 | B | 0 |