SQLITE Vlookup在两个不同的表中
我有两个表,其中一个是银行数据,另一个是付款确认,我需要执行查找 我尝试了几个SQLITE Vlookup在两个不同的表中,sql,sqlite,vlookup,Sql,Sqlite,Vlookup,我有两个表,其中一个是银行数据,另一个是付款确认,我需要执行查找 我尝试了几个left join示例,但未能获得所需的输出。 我正在使用Sqlite3 请建议 表格-付款详情 Payment_Date TranNo RefNo ChequeNo 11-Nov-19 tran001 ref001 chq001 11-Nov-19 tran001 ref002 chq005 12-Nov-19 tra
left join
示例,但未能获得所需的输出。
我正在使用Sqlite3
请建议
表格-付款详情
Payment_Date TranNo RefNo ChequeNo
11-Nov-19 tran001 ref001 chq001
11-Nov-19 tran001 ref002 chq005
12-Nov-19 tran003 ref003 chq007
13-Nov-19 tran017 ref001 chq005
CustomerID Payment_No Match_Status Payment_date
cust_1 ref003
cust_2 tran001
cust_3 ref002
cust_4 tran019
cust_5 tran001
cust_6 chq005
cust_7 tran019
CustomerID Payment_No Match_Status Payment_date
cust_1 ref003 TRUE 12-Nov-19
cust_2 tran001 TRUE 11-Nov-19
cust_3 ref002 TRUE 11-Nov-19
cust_4 tran019 FALSE FALSE
cust_5 tran001 TRUE 11-Nov-19
cust_6 chq005 TRUE 11-Nov-19
cust_7 tran019 FALSE FALSE
表格-付款确认
Payment_Date TranNo RefNo ChequeNo
11-Nov-19 tran001 ref001 chq001
11-Nov-19 tran001 ref002 chq005
12-Nov-19 tran003 ref003 chq007
13-Nov-19 tran017 ref001 chq005
CustomerID Payment_No Match_Status Payment_date
cust_1 ref003
cust_2 tran001
cust_3 ref002
cust_4 tran019
cust_5 tran001
cust_6 chq005
cust_7 tran019
CustomerID Payment_No Match_Status Payment_date
cust_1 ref003 TRUE 12-Nov-19
cust_2 tran001 TRUE 11-Nov-19
cust_3 ref002 TRUE 11-Nov-19
cust_4 tran019 FALSE FALSE
cust_5 tran001 TRUE 11-Nov-19
cust_6 chq005 TRUE 11-Nov-19
cust_7 tran019 FALSE FALSE
输出表-付款确认
Payment_Date TranNo RefNo ChequeNo
11-Nov-19 tran001 ref001 chq001
11-Nov-19 tran001 ref002 chq005
12-Nov-19 tran003 ref003 chq007
13-Nov-19 tran017 ref001 chq005
CustomerID Payment_No Match_Status Payment_date
cust_1 ref003
cust_2 tran001
cust_3 ref002
cust_4 tran019
cust_5 tran001
cust_6 chq005
cust_7 tran019
CustomerID Payment_No Match_Status Payment_date
cust_1 ref003 TRUE 12-Nov-19
cust_2 tran001 TRUE 11-Nov-19
cust_3 ref002 TRUE 11-Nov-19
cust_4 tran019 FALSE FALSE
cust_5 tran001 TRUE 11-Nov-19
cust_6 chq005 TRUE 11-Nov-19
cust_7 tran019 FALSE FALSE
请尝试以下操作:
表架构:
CREATE TABLE Payment_Details(
Payment_Date DATETIME,
TranNo VARCHAR(20),
RefNo VARCHAR(20),
ChequeNo VARCHAR(20));
INSERT INTO Payment_Details VALUES
('11-Nov-19','tran001','ref001','chq001'),
('11-Nov-19','tran001','ref002','chq005'),
('12-Nov-19','tran003','ref003','chq007'),
('13-Nov-19','tran017','ref001','chq005');
CREATE TABLE Payment_Confirmations(
CustomerId VARCHAR(20),
Payment_No VARCHAR(20),
Match_Status VARCHAR(20),
Payment_date DATETIME);
INSERT INTO Payment_Confirmations(CustomerId,Payment_No) VALUES
('cust_1','ref003'),
('cust_2','tran001'),
('cust_3','ref002'),
('cust_4','tran019'),
('cust_5','tran001'),
('cust_6','chq005'),
('cust_7','tran019');
SELECT DISTINCT A.CustomerID,
A.Payment_No,
CASE WHEN B.ChequeNo IS NULL THEN 'FALSE' ELSE 'TRUE' END Match_Status,
IFNULL(b.Payment_date,'FALSE') Payment_date
FROM Payment_Confirmations A
LEFT JOIN Payment_Details B ON A.Payment_No=B.TranNo OR A.Payment_No=B.RefNo OR A.Payment_No=B.ChequeNo;
SQL查询:
CREATE TABLE Payment_Details(
Payment_Date DATETIME,
TranNo VARCHAR(20),
RefNo VARCHAR(20),
ChequeNo VARCHAR(20));
INSERT INTO Payment_Details VALUES
('11-Nov-19','tran001','ref001','chq001'),
('11-Nov-19','tran001','ref002','chq005'),
('12-Nov-19','tran003','ref003','chq007'),
('13-Nov-19','tran017','ref001','chq005');
CREATE TABLE Payment_Confirmations(
CustomerId VARCHAR(20),
Payment_No VARCHAR(20),
Match_Status VARCHAR(20),
Payment_date DATETIME);
INSERT INTO Payment_Confirmations(CustomerId,Payment_No) VALUES
('cust_1','ref003'),
('cust_2','tran001'),
('cust_3','ref002'),
('cust_4','tran019'),
('cust_5','tran001'),
('cust_6','chq005'),
('cust_7','tran019');
SELECT DISTINCT A.CustomerID,
A.Payment_No,
CASE WHEN B.ChequeNo IS NULL THEN 'FALSE' ELSE 'TRUE' END Match_Status,
IFNULL(b.Payment_date,'FALSE') Payment_date
FROM Payment_Confirmations A
LEFT JOIN Payment_Details B ON A.Payment_No=B.TranNo OR A.Payment_No=B.RefNo OR A.Payment_No=B.ChequeNo;
检查中的结果,因此,基本上,您需要检查
Payment\u Confirmation
中的Payment\u No
是否与ranNo
或RefNo
或ChequeNo
匹配,然后更新状态和提取日期。。谢谢您的付款编号栏为什么同时包含交易编号和参考编号?那没有多大意义。这些是不同的东西,它们应该在不同的列中。@Tomalak,Payment\u No可以有3个refno或tran No或check No,也请注意相同的Payment\u No值可以用于多个客户。。。cust_2和cust_5感谢Dinesh、、tran001对不同的客户使用了2次,客户总数为7…您的查询创建了2行。对于trann001的同一客户……客户2 tran001于2019年11月11日生效…………客户2 tran001于2019年11月11日生效。。cust_5 tran001输入正确。。。。。。不存在…并向上移动了1个单元格,因此状态显示到客户6。。。。