简体   繁体   English

SQL表联接问题

[英]SQL Table Joining issue

I have a two tables: "invoice" as I and "Accounts Receivable" as AR . 我有两个表:“发票”为I和“应收帐款”为AR

AR table includes invoice issued (with id 0) and cash received (with id 4), while I table includes invoice amount column and adjustment column. AR表包括已开具发票(ID为0)和已收现金(ID为4),而I表包括发票金额列和调整列。

Apart from regular invoices and adjustments there are cases where adjustments were made to invoice and net affect is 0.00 on AR table. 除常规发票和调整外,在某些情况下对发票进行了调整,并且AR表上的净影响为0.00。 Plus sometimes, invoices are created and written off at invoice table before posting, so AR will have 0.00 amount in AR but I table has $100 in amount and -$100 in adjustment. 另外,有时会在过帐之前在发票表中创建和注销发票,因此ARAR金额为0.00,但I表中的金额为$ 100,调整金额为-$ 100。

I am trying to create a query where it gives me invoice issued and cash received side by side and also create a new column that includes adjustment made for invoices with 0.00 balance in AR . 我正在尝试创建一个查询,该查询可让我查看已发出的发票和并列收到的现金,还创建一个新列,其中包含对AR余额为0.00的发票进行的调整。 Columns that might help: 可能有帮助的列:

AR.ID = unique ID
AR.ARinvnumber= Invoice number from Invoice table
Ar.Type= 0=invoice, 1 = payment received
Ar.Amount= ARamount saved from invoice 
I.Id= unique ID
Invoice number = number of invoice
Invamount= Actual invoice amount
Inv Adjustment= Adjustment applied on invoice

Any idea how I can achieve that? 知道我该如何实现吗? I am able to match I and AR table and cash and AR from AR table 我能匹配IAR表以及现金和ARAR

Select *
From (select ar.customerId, ar.customername,ar.invnumber ar.amount, i.invamout, i.invadjustment from Ar join I on ar.arinvnumber=i.invoicenumber where ar.artype=1) inv
join
select (select ar.customerId, ar.customername, ar.invnumber ar.amount, i.invamout, i.invadjustment from Ar join I on ar.arinvnumber=i.invoicenumber where ar.artype=1) cash

on inv.invnumber=cash.invnumber and inv.customerid=cash.customerid

after getting this, how can I include those invoices for whom adjustment were made but there was no AR because adjustment equals invoice amount. 得到这个后,如何包含那些已对其进行调整但没有AR发票,因为调整等于发票金额。

Answer: 回答:

The following answer worked for me. 以下答案对我有用。 Basically I wanted to include all the adjustments from invoice table including those ones which are not populated in AR table because the adjustments were made to clear client's balance related to the work done after final invoice was issues. 基本上,我想包括发票表中的所有调整,包括那些未填充在AR表中的调整,因为所做的调整是为了清除与最终发票出具后所完成工作相关的客户余额。 I used following query 我使用以下查询

Select *
From (select AR.ARInvnum as ARInvnum, AR.Arclientnumber as Aclient, sum(AR.Amount), I.Invoicenumber,      sum(distinct(I.IAmount)), sum(I.IAdjust)
    From AR.ARInvnum=I.Invoicenumber
    Where ar.artype=0
Group by AR.ARInvnum, I.Invoicenumber, AR.Arclientnumber)AInvoice
Left join 
    (select AR.ARInvnum as PARInvnum, AR.Arclientnumber as PClient,      sum(AR.Amount), I.Invoicenumber, sum (I.IAmount), sum(I.IAdjust)
    From AR.ARInvnum=I.Invoicenumber
    Where ar.artype=4
Group by AR.ARInvnum, I.Invoicenumber, AR.ARclientnumber)PInvoice
on
AInvoice.ARInvnum=PInvoice.PARInvnum
and 
AInvoice.Aclient=PInvoice.PClient

Keep in Mind the Distinct clause in the first portion of the subquery removes any duplicates as well as sum them, so it will look like one summary related to particular invoice number. 请记住,子查询第一部分中的Distinct子句会删除所有重复项并对其进行求和,因此它看起来像是一个与特定发票编号相关的摘要。 first portion of the subquery is summarizing based on the invoice issues and the adjustments related to the client. 子查询的第一部分基于发票问题和与客户相关的调整进行汇总。 and second part is matching all payments received. 第二部分是匹配所有收到的付款。 I hope, this helps. 我希望这有帮助。

You need to include a limiting clause, also known as a where clause. 您需要包括一个限制子句,也称为where子句。 MSDN documentation on select lays out the syntax as follows: 关于select的 MSDN文档列出了以下语法:

SELECT select_list [ INTO new_table ]

[ FROM table_source ] [ WHERE search_condition ]

[ GROUP BY group_by_expression ]

[ HAVING search_condition ]

[ ORDER BY order_expression [ ASC | DESC ] ]

Be aware, you will not need every expression to create a valid select statement. 请注意,您不需要每个表达式都可以创建有效的select语句。 Also you do not need to join the same query to itself. 同样,您不需要将相同的查询自身连接起来。 Why not simplify by running the query by itself? 为什么不通过单独运行查询来简化呢?

select 
    ar.customerId, ar.customername, ar.invnumber, 
    ar.amount, i.invamout, i.invadjustment 
from Ar 
  join I on ar.arinvnumber=i.invoicenumber 
where ar.artype=1

But your questions asks how to limit the results to only 但是您的问题询问如何将结果限制为仅

those invoices for whom adjustment were made but there was no [account receivable] 已对其进行调整但没有[应收帐款]的发票

Update your where clause to something like 将您的where子句更新为类似

where ar.artype=1 and i.adjustment is not null and i.adjustment = i.invamount

This will limit the results returned by the select statement to only those records that meet all three of the following criteria: 这会将select语句返回的结果限制为仅满足以下三个条件的所有记录:

  1. artype is equal to 1 artype等于1
  2. adjustment is not null 调整不为空
  3. adjustment is equal to invamount 调整等于侵害

If that result set is too narrow, tweak your where clause. 如果结果集太窄,请调整where子句。 I find it helpful to identify a single record that looks like other records I would like to find. 我发现识别看起来像我想要查找的其他记录的单个记录很有帮助。 Use that records unique identifier as a test for whether or not your query is working like you expect. 使用该记录唯一标识符作为测试查询是否按预期工作的测试。

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

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