使用SQL获取所有支付交易

时间:2017-02-21 07:06:42

标签: sql sql-server

我有一系列的记录。根据以下数据,如何获取特定客户发票(XR)的所有交易(XZ)。

图例

XR - customer invoice
XZ - payments
XA - manual clearing

示例1

Doc Nos      Type   TY    Amount     Cleared Doc
9500011864   A121   XR    36247.62   9000001660
9600009487   A121   XZ       -4.76   9000001660
9000001660   A121   XA    36242.86   9600012264
9600012264   A121   XZ   -72490.48   9600012264

CONDITION

Doc Nos : 9500011864
TY      : XR

渴望输出

Doc Nos      Type   TY    Amount     Cleared Doc
9600009487   A121   XZ       -4.76   9000001660
9600012264   A121   XZ   -72490.48   9600012264

上述说明:客户发票(XR)的金额为36,247.62。有记录的付款(XZ),文件编号(9500011864)的值为4.76。由于这是部分付款,因此有一个人工清算余额(XA),金额为36,242.86。另一笔付款发布(XZ),金额为72,490.48,参考文件号码(9000001660)。

示例2

Doc Nos      Type   TY    Amount     Cleared Doc
9500011864   A121   XR    36247.62   9000001660
9600009487   A121   XZ       -4.76   9000001660
9000001660   A121   XA    36242.86   9000001661
9000001661   A121   XZ       -2.86   9000001661
9000001661   A121   XA    36240.00   9600012264
9600012264   A121   XZ   -36240.00   9600012264

CONDITION

Doc Nos : 9500011864
TY      : XR

渴望输出

Doc Nos      Type   TY    Amount     Cleared Doc
9600009487   A121   XZ       -4.76   9000001660
9000001661   A121   XZ       -2.86   9000001661
9600012264   A121   XZ   -36240.00   9600012264

示例3

Doc Nos      Type   TY    Amount     Cleared Doc
9500000368   A121   XR    36247.62   9000000022
9000000022   A121   XA   -36247.62   9000000022
9000000022   A121   XA    36247.62   9000000022
9600016951   A121   XZ   -36247.62   9000000022

CONDITION

Doc Nos : 9500000368
TY      : XR

渴望输出

Doc Nos      Type   TY    Amount     Cleared Doc
9600016951   A121   XZ   -36247.62   9000000022

我正在使用SQL Server 2016.我谦虚地请求SQL专家帮助我完成此过程。 CTE Recursive可以适用于此吗?先谢谢!

2 个答案:

答案 0 :(得分:0)

取决于你的老年人,你说的是什么

  

XR清算doc到XZ清算文档,以及XA清算doc到XZ   清算文件

条件应为Doc Nos& TY

希望这有帮助: -

Select * from table
where TY = ( Get the TY value here based on conditions of Doc Nos & TY )

<强>演示: -

Create table MyTable (Doc_Nos varchar(20) , [type] varchar (10) , TY char (2) , Amount Decimal (18,2) , Cleared_Doc varchar(20))
go
insert into MyTable values ('9500011864' , 'A121' , 'XR' , 36247.62 , '9000001660' )
insert into MyTable values ('9600009487' , 'A121' , 'XZ' , -4.76 , '9000001660' )
insert into MyTable values ('9000001660' , 'A121' , 'XA' , 36242.86 , '9600012264' )
insert into MyTable values ('9600012264' , 'A121' , 'XZ' , -72490.48 , '9600012264' )
go

declare
        @sDocNos varchar(20),
        @TY     char (2)

select 
        @sDocNos = '9500011864' ,
        @TY      ='XR'

Select * from MyTable
where TY = (
            select TY 
            from MyTable 
            where Cleared_Doc = (
                select Cleared_Doc 
                from MyTable 
                where Doc_Nos = @sDocNos 
                and TY = @TY ) and TY = 'XZ')

<强>结果: -

enter image description here

答案 1 :(得分:0)

您需要递归查询。您为给定的&#34; Doc Nos&#34;选择XR记录。凭借其#34; Cleared Doc&#34;您可以找到相关记录(其中&#34;相关&#34;表示他们在&#34; Doc Nos&#34;或&#34; Cleared Doc&#34;取决于他们的TY)中有此号码。通过所有这些找到的记录,您可以使用他们的&#34; Cleared Doc&#34;等等。最后,您可以显示找到的行中的所有XZ记录。

with cte(doc_nos, type, ty, amount, cleared_doc) as
(
  select doc_nos, type, ty, amount, cleared_doc
  from mytable 
  where doc_nos = 9500011864 and ty = 'XR'
  union all
  select t.doc_nos, t.type, t.ty, t.amount, t.cleared_doc
  from cte 
  join mytable t on (cte.ty = 'XR' and t.ty = 'XZ' and cte.cleared_doc = t.cleared_doc)
                 or (cte.ty = 'XR' and t.ty = 'XA' and cte.cleared_doc = t.doc_nos)
                 or (cte.ty = 'XA' and t.ty = 'XZ' and cte.cleared_doc = t.doc_nos)
)
select *
from cte
where ty = 'XZ';
相关问题