目标:检测昨天的表负载和今天的表负载之间的任何差异。每次加载都会加载与银行帐户关联的数据值。因此,我需要一个查询,该查询返回具有差异的每个单独帐户,并在列名称中包含值。
我需要来自两个不同表的几列数据。 AEI_GFXAccounts和AEI_GFXAccountSTP。每次加载表时,它都有一个“ run_ID”,该值递增1。因此,需要将其与MAX(run_id)
和MAX(run_id) -1
进行比较。
我尝试了以下查询。该查询所做的只是返回我需要的所有列。现在,我需要实现运行这些查询WHERE runID = MAX(runID)
的逻辑。然后在run_ID = Max(runID) -1
处再次运行它。比较两个表,显示可以显示在诸如SELECT AccountBranch WHERE MAX(Run_ID) -1 AS WAS
之类的列下的差异。等等,每个列的另一个自定义命名列为'IS NOW
'等。
SELECT AEI_GFXAccounts.AccountNumber,
AccountBranch,
AccountName,
AccountType,
CostCenter,
TransactionLimit,
ClientName,
DailyCumulativeLimit
FROM AEI_GFXAccounts
JOIN AEI_GFXAccountSTP
ON (AEI_GFXAccounts.feed_id = AEI_GFXAccountSTP.feed_id
and AEI_GFXAccounts.run_id = AEI_GFXAccountSTP.run_id)
答案 0 :(得分:2)
我使用类似的方法来检测日志系统的更改:
WITH data AS (
SELECT
a.run_id,
a.AccountNumber,
?.AccountBranch,
?.AccountName,
?.AccountType,
?.CostCenter,
?.TransactionLimit,
?.ClientName,
?.DailyCumulativeLimit
FROM
AEI_GFXAccounts a
INNER JOIN AEI_GFXAccountSTP b
ON
a.feed_id = b.feed_id and
a.run_id = b.run_id
),
yest AS (
SELECT * FROM data WHERE run_id = (SELECT MAX(run_id)-1 FROM AEI_GFXAccounts)
),
toda AS (
SELECT * FROM data WHERE run_id = (SELECT MAX(run_id) FROM AEI_GFXAccounts)
)
SELECT
CASE WHEN COALESCE(yest.AccountBranch, 'x') <> COALESCE(toda.AccountBranch, 'x') THEN yest.AccountBranch END as yest_AccountBranch,
CASE WHEN COALESCE(yest.AccountBranch, 'x') <> COALESCE(toda.AccountBranch, 'x') THEN toda.AccountBranch END as toda_AccountBranch,
CASE WHEN COALESCE(yest.AccountName, 'x') <> COALESCE(toda.AccountName, 'x') THEN yest.AccountName END as yest_AccountName,
CASE WHEN COALESCE(yest.AccountName, 'x') <> COALESCE(toda.AccountName, 'x') THEN toda.AccountName END as toda_AccountName,
...
FROM
toda INNER JOIN yest ON toda.accountNumber = yestaccountNumber
注意:
?.
前缀-分别用a.
或as.
替换它们(总是很好的做法来完全限定所有列别名)CASE WHEN a is null and b is not null or b is null and a is not null or a != b
的快速方法,但是如果昨天的帐户名(例如)为'x'
,而今天它为null,则比较失败,因为null变为'x' 。如果您选择了永远不会出现在列中的数据,则检查将成功,因为会将空值合并为永远不会出现在实际数据中的内容,因此<>
比较将可以完成toda.X <> yest.X
toda LEFT JOIN yest ...
。当然,它们的所有属性都将显示为新;) WHERE
COALESCE(toda.AccountBranch, 'x') <> COALESCE(yest.AccountBranch, 'x') OR
COALESCE(toda.AccountName, 'x') <> COALESCE(yest.AccountName, 'x') OR
...
答案 1 :(得分:0)
您有日期字段吗?如果是这样,您可以使用按帐户划分的Row_Number。排除所有最多具有1行“新帐户”的帐户,然后将每个帐户的负载的最大(行号)减去最大(行号)-1的负载。仅返回此负载大于0的返回帐户。还使用滞后功能代替Max(rownumber)-1
来获取以前的帐户负载