简体   繁体   English

根据解析的列内容连接两个表

[英]Joining two tables based off of parsed column content

I am trying to join two tables based off of two columns that have been combined (often imperfectly) into another tables column.我正在尝试根据已组合(通常不完美)到另一个表列的两列来连接两个表。 I am trying to join the tables so the correct records are associated with each other, so I can compare the FDebit and the Debit fields.我正在尝试加入这些表,以便正确的记录相互关联,这样我就可以比较FDebitDebit字段。

The FMEMO is normally generated by taking the Num , then adding a space , then adding the Memo text. FMEMO通常是通过获取Num生成的,然后添加一个space ,然后添加Memo文本。 As you can see below, our process is not yet perfect.正如您在下面看到的,我们的流程还不完善。 I would therefore like to match the Num , then the space , then the first 10 characters of the Memo field to the FMEMO field.因此,我想匹配Num ,然后是space ,然后是 Memo 字段的前 10 个字符与FMEMO字段。

I have included code below with sample data.我在下面的代码中包含了示例数据。 Could you please offer me some suggestions on how to accomplish this?你能给我一些关于如何实现这一点的建议吗?

Invoices table发票表

MEMO Num DEBIT MEMO Num 借记卡
Supplies.补给品。 Soto Cano 1135 2.25索托卡诺 1135 2.25
Suction Hose (1-1/2") by the food 3 74.04吸食软管 (1-1/2") 3 74.04
Hose/Tubing:Braided Hose (1") by the food 3 98.72软管/管道:食品编织软管 (1") 3 98.72
QP10 Meyers surface pump (60hz) 3 206.27 QP10 Meyers 地面泵 (60hz) 3 206.27
Cage including f tank, alum parts box and 2 buckets of alum 3 752.03笼子包括 f 罐、明矾零件盒和 2 桶明矾 3 752.03
Cage including valve manifold, F1 & F2 3 3774.08保持架,包括阀组,F1 和 F2 3 3774.08
cage with IBC in it 1135 268.41装有 IBC 的笼子 1135 268.41
Pvc accesories for installation of LWTS.用于安装 LWTS 的 PVC 配件。 1175 4.26 1175 4.26
Pvc accesories for installation of LWTS.用于安装 LWTS 的 PVC 配件。 1175 27.26 1175 27.26

Expenses table费用表

FMEMO FDebit Supplies. FMEMO FDebit 用品。 Soto Cano 41.8 2.25索托卡诺 41.8 2.25
3 Suction Hose (1-1/2 74.04 3 吸管 (1-1/2 74.04
3 Hose/Tubing:Braided Hose (1 98.72 3 软管/管材:编织软管 (1 98.72
3 QP10 Meyers surface pump (60hz) 3970 206.27 3 QP10 Meyers 地面泵 (60hz) 3970 206.27
3 Cage including f tank, alum parts box and 2 buckets of alum 14474 752.03 3 个笼子,包括 f 罐、明矾零件盒和 2 个明矾桶 14474 752.03
3 Cage including valve manifold, F1 & F2 72638 3774.08 3 保持架,包括阀组,F1 和 F2 72638 3774.08
3 cage with IBC in it 5166 268.41 3 笼中装有 IBC 5166 268.41
1175 Pvc accesories for installation of LWTS. 1175 Pvc 配件,用于安装 LWTS。 82.03 4.26 82.03 4.26
1175 Pvc accesories for installation of LWTS. 1175 Pvc 配件,用于安装 LWTS。 524.67 27.26 524.67 27.26

Code to replicate:要复制的代码:

CREATE TABLE #tempExpenses (
FMEMO varchar(Max), FDebit money)

CREATE TABLE #tempInvoices (
MEMO varchar(Max), Num integer, DEBIT money)

INSERT INTO #tempExpenses (FMEMO, FDEBIT) VALUES ('Supplies. Soto Cano 41.8', 2.25)<br/>
INSERT INTO #tempExpenses (FMEMO, FDEBIT) VALUES ('3 Suction Hose (1-1/2', 74.04)<br/>
INSERT INTO #tempExpenses (FMEMO, FDEBIT) VALUES ('3 Hose/Tubing:Braided Hose (1', 98.72)<br/>
INSERT INTO #tempExpenses (FMEMO, FDEBIT) VALUES ('3 QP10 Meyers surface pump (60hz) 3970', 206.27)<br/>
INSERT INTO #tempExpenses (FMEMO, FDEBIT) VALUES ('3 Cage including f tank, alum parts box and 2 buckets of alum 14474', 752.03)<br/>
INSERT INTO #tempExpenses (FMEMO, FDEBIT) VALUES ('3 Cage including valve manifold, F1 & F2 72638', 3774.08)<br/>
INSERT INTO #tempExpenses (FMEMO, FDEBIT) VALUES ('3 cage with IBC in it 5166', 268.41)<br/>
INSERT INTO #tempExpenses (FMEMO, FDEBIT) VALUES ('1175 Pvc accesories for installation of LWTS. 82.03', 4.26)<br/>
INSERT INTO #tempExpenses (FMEMO, FDEBIT) VALUES ('1175 Pvc accesories for installation of LWTS. 524.67', 27.26)<br/>

INSERT INTO #tempInvoices (MEMO, Num, DEBIT) VALUES ('Supplies. Soto Cano', 1135, 2.25)<br/>
INSERT INTO #tempInvoices (MEMO, Num, DEBIT) VALUES ('Suction Hose (1-1/2") by the food', 3, 74.04)<br/>
INSERT INTO #tempInvoices (MEMO, Num, DEBIT) VALUES ('Hose/Tubing:Braided Hose (1") by the food', 3, 98.72)<br/>
INSERT INTO #tempInvoices (MEMO, Num, DEBIT) VALUES ('QP10 Meyers surface pump (60hz)', 3, 206.27)<br/>
INSERT INTO #tempInvoices (MEMO, Num, DEBIT) VALUES ('Cage including f tank, alum parts box and 2 buckets of alum', 3, 752.03)<br/>
INSERT INTO #tempInvoices (MEMO, Num, DEBIT) VALUES ('Cage including valve manifold, F1 & F2', 3, 3774.08)<br/>
INSERT INTO #tempInvoices (MEMO, Num, DEBIT) VALUES ('cage with IBC in it', 1135, 268.41)<br/>
INSERT INTO #tempInvoices (MEMO, Num, DEBIT) VALUES ('Pvc accesories for installation of LWTS.', 1175, 4.26)<br/>
INSERT INTO #tempInvoices (MEMO, Num, DEBIT) VALUES ('Pvc accesories for installation of LWTS.', 1175, 27.26)<br/>

SELECT *
FROM #tempExpenses 

SELECT *
FROM #tempInvoices

Well I really hate myself for producing this TSQL but I think this is what you are looking for:好吧,我真的很讨厌自己生产这个 TSQL 但我认为这就是您要寻找的:

SELECT *
FROM #tempInvoices i
INNER JOIN #tempExpenses e ON CAST(Num as varchar(10)) + ' ' + SUBSTRING(MEMO,1,9-LEN(CAST(NUM as varchar(10)))) = SUBSTRING(FMEMO,1,10)

Which concatenates the number and takes so many characters form the field ie if 3 then 9, if 1111 then 9-4 and joins with the same amount of characters form the other table.它连接数字并从字段中获取如此多的字符,即如果 3 则 9,如果 1111 则 9-4 并与相同数量的字符连接形成另一个表。

Of course this is a very inefficient and ugly query.当然,这是一个非常低效和丑陋的查询。 I would rather normalize the data in the database (parse clean etc)我宁愿规范化数据库中的数据(解析干净等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM