简体   繁体   English

通过与id链接的另一个表的另一个属性连接表

[英]Joining a table thru another property of another table that is linked with id

Edit : I did a mistake the Invoices Table carry the transactionId 编辑:我错了,发票表携带transactionId

I have 3 tables : 我有3张桌子:

Transactions  Reconciliations             Invoices
id            num    line transId         id   Code    transId
--            ---   ---- -------          --   ----    -------------
3              1    1    3                5   Code 5   3
6              1    2    6                9   Code 9   8    
7              1    3    7                12  Code 12  11
8              2    1    8
12             2    2    12
10             3    1    10
11             3    2    11 

and this Query : 和这个查询:

select
    t1.id   -- transaction id
    t2.num  -- reconciliation number
    t3.Code -- Invoice code
from Transactions t1
left outer join Reconciliations t2 on t2.transId = t1.id
left outer join Invoices t3 on t3.transId = t1.id

Giving the following result : 得到以下结果:

id      num     code
--     ---     ----
3       1       Code 5
6       1       null
7       1       null
8       2       Code 9
12      2       null
10      3       null
11      3       Code 12

But what I want is this : 但是我想要的是:

id      num     code
--      ---     ----
3       1       Code 5
6       1       Code 5
7       1       Code 5
8       2       Code 9
12      2       Code 9
10      3       Code 12
11      3       Code 12

To put words on it when the linked Invoice table gives null I want to join on all the records from Reconciliations with the same Reconciliation number. 当链接的发票表为空时,要在上面加上文字,我想加入对帐中所有具有相同对帐编号的记录。

Edit : I would like the Code in invoices to be shared across all transactions that shares the same Reconciliation number 编辑 :我希望发票中的代码在共享相同对帐号的所有交易中共享

I have tried to do thru outer apply and sub query but I cannot figure out a way to achieve it. 我试图通过外部应用和子查询来做,但是我无法找到一种方法来实现它。 Have you any idea ? 你有什么主意吗?

You seem to want to spread the InvoiceId in Transactions up to the next value. 您似乎想将TransactionsInvoiceId传播到下一个值。

Here is one method: 这是一种方法:

select t.*
       (select top 1 InvoiceId
        from Transactions t2
        where t2.id <= t.id and t2.InvoiceId is not NULL
        order by id desc
       ) as newInvoiceId
from transactions t;

You can then substitute this into your query: 然后,您可以将其替换为查询:

select
    t1.id   -- transaction id
    t2.num  -- reconciliation number
    t3.Code -- Invoice code
from (select t.*
            (select top 1 InvoiceId
             from Transactions t2
             where t2.id <= t.id and t2.InvoiceId is not NULL
             order by id desc
            ) as newInvoiceId
     from transactions t
    ) t1
left outer join Reconciliations t2 on t2.transid = t1.id
left outer join Invoices t3 on t3.id = t1.transid ;

The solution is to join to Reconciliations again before joining to Invoices : 解决方案是在加入Invoices之前再次加入Reconciliations

select t.id, r.num, i.Code
from Transactions t
join Reconciliations r on r.transId = t.id
join Reconciliations r2 on r2.num = r.num
join Invoices i on i.transId = r2.transId

Note that the joins are now inner joins ( requiring a match), and how you easily make the connection to the right Invoice via the shared Reconciliation.num value - using inner joins means you only get the invoice row that matches. 请注意,这些联接现在是内部联接( 需要匹配),以及如何通过共享的Reconciliation.num值轻松地连接到正确的发票-使用内部联接意味着您只能获得匹配的发票行。

To see this query in action, execute it on SQLFiddle 要查看此查询的实际效果,请在SQLFiddle上执行它


Edit: To cater for missing invoices 编辑:以应付发票丢失

Use left join to invoices, but you need a group by with max() to limit the joins to just one invoice per transaction (without the max() you get lots of extra rows with null Code): 对发票使用左连接,但是您需要使用max()进行分组,以将每个交易的连接限制为仅一张发票(没有max()您会得到很多带有空代码的额外行):

select t.id, r.num, max(i.Code) as Code
from Transactions t
join Reconciliations r on r.transId = t.id
join Reconciliations r2 on r2.num = r.num
left join Invoices i on i.transId = r2.transId
group by t.id, r.num

To see this query in action, where I have invalidated invoice 12 from above fiddle, execute it on SQLFiddle 要查看此查询的实际操作,我在小提琴上方使发票12无效,请在SQLFiddle上执行该查询

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

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