我想计算不同表中银行的出现次数,并在FinalTable的相应列中更新。
BankTable:
BankName ErrorCode
SBI 200
ICICI 200
SBI 400
SBI 500
SBI 503
ATMTable:
BankName ErrorCode
ICICI 200
ICICI 400
ICICI 400
SBI 500
SBI 503
MitraTable:
BankName ErrorCode
ICICI 200
ICICI 200
SBI 400
SBI 500
SBI 503
PNB 501
FinalTable:
BankName BankCount ATMCount MitraCount TotalCount
ICICI 1 3 2 6
SBI 4 2 3 9
PNB 0 0 1 1
我想计算不同银行的出现次数,并在FinalTable中设置值。
答案 0 :(得分:1)
您可以将UNION ALL
与条件聚合一起使用:
SELECT BankName,
COUNT(CASE WHEN src = 'Bank' THEN 1 END) AS BankCount,
COUNT(CASE WHEN src = 'ATM' THEN 1 END) AS ATMCount,
COUNT(CASE WHEN src = 'Mitra' THEN 1 END) AS MitraCount,
COUNT(*) AS TotalCount
FROM (
SELECT BankName, 'Bank' AS src
FROM BankTable
UNION ALL
SELECT BankName, 'ATM' AS src
FROM ATMTable
UNION ALL
SELECT BankName, 'Mitra' AS src
FROM MitraTable) AS t
GROUP BY BankName
字段src
用于将源表返回到使用UNION ALL
操作结果的外部查询。使用此字段,我们可以执行条件聚合,以计算每个源表中每个库的出现次数。
答案 1 :(得分:0)
试试这个:
hdparm -t