根据客户在一个月内开具发票并在另一个月付款的方式过滤记录

时间:2016-03-31 21:47:44

标签: tsql filter interbase

所以我目前有一个记录列表,我试图根据他们开具发票的时间和他们支付预约的时间进行过滤 我只想要客户开具发票的记录,直到下个月才开始付款,即他们已经预约并在2月开具发票,但直到3月才付款

到目前为止我的查询:

SELECT TRANS.WHENTRANS ACDATE,
    TRANS.AMTUNRES UNPAID,
    TRANS.TRANTYPE,
    TRANS.INCPROVCODE IPROV,
    ACHOLDERACH.ACBALANCE BALANCE,
    ACHOLDERANY.PREFORMATTEDINTERN ACCOUNTNAMECHART,
    Amount
FROM (  TRANS
  LEFT JOIN ACHOLDER ACHOLDERACH ON ACHOLDERACH.ACHOLDERID = TRANS.ACHOLDERID)
  LEFT JOIN ANYBODY ACHOLDERANY ON ACHOLDERANY.ANYBODYID = TRANS.ACHOLDERID



 where TranType = 'I' and amount = x
and TranType = 'P' and amount = -x and date = current month

1 个答案:

答案 0 :(得分:1)

假设发票和付款是表格中的两个单独行,以下情况对我有用。

CREATE TABLE #TEST (ACDATE DATE, UNPAID VARCHAR(1), TRANTYPE VARCHAR(1), IPROV VARCHAR(5), ACBALANCE FLOAT, ACCOUNTNAMECHART VARCHAR(100), AMOUNT FLOAT)
INSERT INTO #TEST
(ACDATE, UNPAID, TRANTYPE, IPROV, ACBALANCE, ACCOUNTNAMECHART, AMOUNT)
VALUES
('8/1/2016', 'Y', 'I', '003', 0, 'TESTNAME', 150.50),
('9/1/2016', 'N', 'P', '003', 0, 'TESTNAME',150.50)
SELECT A.ACDATE AS INVOICEDATE, A.AMOUNT AS INVOICEAMT, B.ACDATE AS PAYMENTDATE, B.AMOUNT AS PAYMENTAMT
FROM
(
    SELECT *
    FROM #TEST
    WHERE TRANTYPE = 'I'
) A
LEFT JOIN
(
    SELECT *
    FROM #TEST
    WHERE TRANTYPE = 'P'
) B ON A.ACCOUNTNAMECHART = B.ACCOUNTNAMECHART -- OR YOU CAN USE WHATEVER KEY FIELDS YOU HAVE TO HOOK TH INVOICE TO THE PAYMENT
WHERE MONTH(A.ACDATE) = MONTH(DATEADD(MM,-1,B.ACDATE))