DELIQUENCY_COUNT 0 1 2 3 4 5 6 7 8 9 TABLE 1
CYCLE_DELIQUENCY A B C D E F G H I J TABLE 2
Attribute Name = DELIQUENCY_COUNT, Value = 0 1 2 3 4 5 6 7 8 9, Table Name = Table1
Attribute Name = CYCLE_DELIQUENCY, Value = A B C D E F G H I J, Table Name = Table2
在这里,我想比较
DELIQUENCY_COUNT=0
的计数与 CYCLE_DELIQUENCY = A
的计数以及 DELIQUENCY_COUNT=1
的计数与 CYCLE_DELIQUENCY = B
的计数,依此类推。
如果计数有差异,那么我们报告不匹配
等效值(A=0、B=1、C=2、D=3、E=4、F=5、G=6、H=7、I=8、J=9)
在这里,我期待着
如果Table1,值0的DELIQUENCY_COUNT是1000,而Table2,值A的CYCLE_DELIQUENCY是1001,那么我们必须报告它。
同样,对于下一个值 表1,值1的DELIQUENCY_COUNT是5000,表2,值A的CYCLE_DELIQUENCY是5000,那么我们就不必报告它。
Table1 和 Table2 等效的 Value 属性 等效值(A=0、B=1、C=2、D=3、E=4、F=5、G=6、H=7、I=8、J=9)
我写了一个SQL,但不确定它是否有效
SELECT DISTINCT COUNT(CYCLE_DELIQUENCY)
CASE
WHEN 'A' THEN
COUNT(CYCLE_DELIQUENCY)
MINUS
SELECT DISTINCT COUNT(DELIQUENCY_COUNT) FROM {}.{} WHERE DELIQUENCY_COUNT = 0
END ' A REPORT ',
WHEN 'B' THEN
COUNT(CYCLE_DELIQUENCY)
MINUS
SELECT DISTINCT COUNT(DELIQUENCY_COUNT) FROM {}.{} WHERE DELIQUENCY_COUNT = 1
END ' B REPORT ',
FROM {}.{};
使用 Oracle(代码应根据您的 RDBMS 进行调整):
-- S a m p l e D a t a :
Create Table tbl_1 AS
Select LEVEL - 1 as deliquency_count, LEVEL * 10 as count_value
Connect By LEVEL <= 10;
Create Table tbl_2 AS
Select 'A' as cycle_deliquency, 10 as cycle_count_value Union All
Select 'B', 20 Union All
Select 'C', 31 Union All
Select 'D', 40 Union All
Select 'E', 50 Union All
Select 'F', 60 Union All
Select 'G', 72 Union All
Select 'H', 81 Union All
Select 'I', 90 Union All
Select 'J', 99
...您可以创建一个 cte 来链接表中的数据...
WITH
cycle_links AS
( Select cycle_deliquency, cycle_count_value,
Row_Number() Over(Order By cycle_deliquency) - 1 as deliquency_count
From tbl_2
)
...使用 join 获取数据和 case 表达式来标记要报告的行...
-- S Q L :
Select t.deliquency_count, t.count_value,
l.cycle_deliquency, l.cycle_count_value,
Case When t.count_value != l.cycle_count_value
Then Concat('REPORT ', l.cycle_deliquency)
End as REPORT
From tbl_1 t
Inner Join cycle_links l ON(l.deliquency_count = t.deliquency_count)
Order By t.deliquency_count
结果:
DELIQUENCY_COUNT | COUNT_VALUE | CYCLE_DELIQUENCY | CYCLE_COUNT_VALUE | 报告 |
---|---|---|---|---|
0 | 10 | A | 10 | 空 |
1 | 20 | B | 20 | 空 |
2 | 30 | C | 31 | 报告C |
3 | 40 | D | 40 | 空 |
4 | 50 | E | 50 | 空 |
5 | 60 | F | 60 | 空 |
6 | 70 | G | 72 | 报告G |
7 | 80 | H | 81 | 报告H |
8 | 90 | 我 | 90 | 空 |
9 | 100 | J | 99 | 报告J |
...如果您需要将数据转置为一行 - 使用 Oracle 您可以对结果进行透视 - 否则使用 Case 表达式进行条件聚合 ...
-- S Q L (Pivot) :
SELECT Max(a) as a, Max(b) as b, Max(c) as c, Max(d) as d, Max(e) as e,
Max(f) as f, Max(g) as g, Max(h) as h, Max(i) as i, Max(j) as j
FROM ( Select t.deliquency_count, t.count_value,
l.cycle_deliquency, l.cycle_count_value,
Case When t.count_value != l.cycle_count_value
Then Concat('REPORT ', l.cycle_deliquency)
End as REPORT
From tbl_1 t
Inner Join cycle_links l ON(l.deliquency_count = t.deliquency_count)
Order By t.deliquency_count
)
PIVOT ( Max(REPORT)
For CYCLE_DELIQUENCY In('A' as a, 'B' as b, 'C' as c, 'D' as d, 'E' as e,
'F' as f, 'G' as g, 'H' as h, 'I' as i, 'J' as j)
)
结果:
A | B | C | D | E | F | G | H | 我 | J |
---|---|---|---|---|---|---|---|---|---|
空 | 空 | 报告C | 空 | 空 | 空 | 报告G | 报告H | 空 | 报告J |