2017-02-21 13 views
-4

私は一連の記録を持っています。以下のデータに基づいて、特定の顧客請求書(XR)のすべてのトランザクション(XZ)を取得する方法。SQLを使用してすべてのお支払い処理を取得

凡例

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 

上記説明:36,247.62の量と顧客請求書(XR)があります。伝票番号(9500011864)には4.76という値の支払が記録されています(XZ)。これは部分支払であるため、36,242.86の金額を持つマニュアル清算残高(XA)があります。伝票番号(9000001660)を参照して、72,490.48の金額で別の支払が転記されます(XZ)。

例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はこれに適用できますか?ありがとうございました!

+0

1)例1の場合。 '9600009487'と' 9600012264'の2つの希望レコードの関係は何ですか?条件は '9500011864'だけですか? ........ 2) 'TY = 'XZ'のどこで'を使うのが問題なのですか? –

+1

あなたの与えられたシナリオに基づいて: あなたはちょうどこれをやって---テーブルから[TY] = 'XZ'を選択します これが役立つことを望みます。 –

+1

これは再帰的な処理です。彼らの関係は文書をクリアすることに基づいています。 XRは、XAの文書をXAにクリアし、XAはXAの文書をXAの文書にクリアします。部分支払(XZ)消込伝票→(XA)伝票番号 – user1852837

答えて

0

あなたが再帰クエリを必要としています。指定した "Doc Nos"のXRレコードを選択します。 "Cleared Doc"では、関連するレコードを検索します(ここで、 "関連"とは、TYに応じて、 "Doc Nos"または "Cleared Doc"にこの番号があることを意味します)。このように見つかったすべてのレコードで、 "Cleared Doc"などと同じ操作を行います。最後に、見つかった行からすべての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'; 
0

あなたがXZ

ドキュメントをクリアし、条件がDoc Nosのためにあるべきにドキュメントだけでなく、XAクリアドキュメントをクリアXZする

XRクリアドキュメント言った、あなたの先輩に依存& 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

関連する問題