0

我有两张桌子

        1) T_SETTLEMENT_Transaction (master table)
        2) T_SETTLEMENT_Activity (child table)

T_SETTLEMENT_Transaction 表中的数据;

SettlemtTransactionID(pk) PC_TransactioID  Form_Of_PAyment  AgentName    CreatedeDateTime         PaymentTransaDate
1000                       1753014          CASH              Test1       2013-01-10 21:00:00.937  2013-01-10 21:00:00.937
1001                       1456325          Check             test2       2013-01-10 21:00:00.937  2013-01-10 21:00:00.937

T_SETTLEMENT_Activity 表中的数据;

SettlementActivity_ID(pk)   SettlementTransactionID(foreighKey) RoutingNumber  SettlementAmount   SettlemtDate             SettlementStatus SettlementType Bank_Account_NUM
 1                             1000                                1111111111     525            2013-01-10 21:00:00.937       PEND             STL       1234567890
 2                             1001                                1111111122     625             2013-01-10 21:00:00.937       PEND             STL       1234567891
 3                             1000                                1111111111     25             2013-01-13 21:10:00.937       PEND             FEE       1234567890

需求场景是:

1)

 Im selecting records based on "date" , "PEND" & "STL" status from 
 "settlementDate", "settlement_status" and "settlemnt_trans_type" columns.

2)

  If "STL" status record is having "FEE" record then i should add
  SettlementAmount of "FEE" record to "STL" record and only "STL" record should be retrieved.
  (we are checking "STL" is having "FEE" record or Not based on Settlement_TransactionId column (Pls refer the tables i mentioned above)). 

3)

 I would insert the "FEE" record for same Settlement_TransactionID 
 (eg.for Settlement_TransactionID=1000 )on same day of inserting "STL" record(if im having FEE record for that) or after 3 days(approx 3 or 5 days)  with different SETTLEMENT_DATE(which is in  
 selected column ).     

请参阅下面显示的(预期输出)表数据,它告诉我想要的预期输出

我加入两个如下表

 SELECT DISTINCT sum(xa.SETTLEMENT_AMOUNT) AMT,
                xa.ROUTING_NUMBER,
                xa.BANK_ACCOUNT_NUM,
                xa.SETTLEMENT_DATE   
                xa.SETTLEMENT_TRANSACTION_ID
                xt.PAYMENT_AGENT_NAME
                xt.PC_TRANSACTION_ID
                xt.CREATED_DATE_TIME,
                xt.PAYMENT_TRANS_DATE
                        FROM
    PCTransDB.dbo.T_SETTLEMENT_Transaction xt,
    PCTransDB.dbo.T_SETTLEMENT_Activity xa
    with (NOLOCK)
    where xa.SETTLEMENT_DATE <='2013-01-10 21:00:00.937'
     and xa.SETTLEMENT_STATUS='PEND'
     and xa.SETTLEMENT_TRANS_TYPE in('FEE','STL')
     and xt.SETTLEMENT_TRANSACTION_ID=xa.SETTLEMENT_TRANSACTION_ID 
            group by xa.SETTLEMENT_TRANSACTION_ID,
                     xa.ROUTING_NUMBER,
                     xa.BANK_ACCOUNT_NUM,
                     xa.SETTLEMENT_DATE  
                     xa.SETTLEMENT_TRANSACTION_ID 
                     xt.PAYMENT_AGENT_NAME
                     xt.PC_TRANSACTION_ID
                     xt.CREATED_DATE_TIME,
                     xt.PAYMENT_TRANS_DATE;

上述查询的当前输出是

 AMT    Routing Number    Bank_Account_Num  SETTLEMENT_DATE       SETTLEMENT_TRANSACTION_ID PAYMENT_AGENT_NAME  PC_TRANSACTION_ID  CREATED_DATE_TIME        PAYMENT_TRANS_DATE
525.00    111111111     1234567890          2013-01-01 20:00:00.0   1000                    TEST                171477             2012-06-25 00:00:00.0       2012-06-25 00:00:00.0
25.00      111111111    1234567890          2013-01-02 20:10:00.0   1000                    TEST                 171477            2012-06-  25 00:00:00.0  2012-06-25 00:00:00.0
625.00   111111122       1234567891         2013-01-01 20:00:00.0   1001                    TEST1               171478             2012-06-25 00:00:00.0    2012-12-25 21:00:00.0       

但我的预期输出如下

 AMT    Routing Number    Bank_Account_Num  SETTLEMENT_DATE       SETTLEMENT_TRANSACTION_ID PAYMENT_AGENT_NAME  PC_TRANSACTION_ID  CREATED_DATE_TIME        PAYMENT_TRANS_DATE
550.00    111111111     1234567890          2013-01-02 20:10:00.0   1000                    TEST                171477             2012-06-25 00:00:00.0       2012-06-25 00:00:00.0

625.00   111111122       1234567891         2013-01-01 20:00:00.0   1001                    TEST1               171478             2012-06-25 00:00:00.0    2012-12-25 21:00:00.0

如果我在查询中出错以获得预期的输出,请纠正我。

注意:我在这里粘贴我的表格列:

T_SETTLEMENT_TRANSACTION

    SETTLEMENT_TRANSACTION_ID     NUMERIC(12,0)
    PC_TRANSACTION_ID             NUMERIC(12,0)
    PAYMENT_SOURCE_SYSTEM         VARCHAR(9)
    TRANSACTION_AMOUNT            NUMERIC(9,2)
    PAYMENT_AGENT_ID              VARCHAR(10)
    PAYMENT_AGENCY_ID             VARCHAR(10)
    USER_CLUB_CODE                VARCHAR(3)
    FORM_OF_PAYMENT               VARCHAR(4)
    PAYMENT_TRANS_DATE            DATETIME
    PAYMENT_USERREPO_AGENT_ID   VARCHAR(20)
    PAYMENT_AGENT_STATE           VARCHAR(10)
    PAYMENT_AGENT_NAME            VARCHAR(100)
    ACH_REJECT_COUNTER            BIGINT
    CREATED_BATCH_ID              VARCHAR(50)
    CREATED_DATE_TIME             DATETIME

2) T_SETTLEMENT_ACTIVITY

     SETTLEMENT_ACTIVITY_ID          NUMERIC(12,0)
     SETTLEMENT_TRANSACTION_ID       NUMERIC(12,0)
     ORDER_TRACE_NUMBER              VARCHAR(15)
     SETTLEMENT_STATUS               VARCHAR(10)
     SETTLEMENT_TRANS_TYPE           VARCHAR(10)
     SETTLEMENT_AMOUNT               NUMERIC(9,2)
     ROUTING_NUMBER                  VARCHAR(9)
     BANK_ACCOUNT_NUM                VARCHAR(20)
     SETTLEMENT_DATE                 DATETIME
     ACH_SUBMISSION_DATE_TIMESTAMP     DATETIME
     ACH_REJECT_CODE                 VARCHAR(10)
     ACH_TRACE_NUMBER                VARCHAR(15)
     ACH_RETURN_TRACE_NUMBER         VARCHAR(15)
     CREATED_BATCH_ID                VARCHAR(50)
     CREATED_DATE_TIME               DATETIME
     TRANS_XREF                      NUMERIC(12,0)
     NSF_XREF                        NUMERIC(12,0)
     MODIFIED_BY                     VARCHAR(50)
     LAST_MODIFIED_DATE_TIME         DATETIME
     SETTLEMENT_CANCEL_REASONCODE     VARCHAR(10)
4

1 回答 1

0

此查询似乎产生了所需的输出。但是您发布的查询可能无法运行,示例数据也无法正常工作。“BANK_ACCOUNT_NUM”列不与任何示例数据相关联,它位于 GROUP BY 子句中,因此可能会让您失望。

SELECT DISTINCT sum(xa.SETTLEMENT_AMOUNT) AMT,
                xa.ROUTING_NUMBER,
                xa.BANK_ACCOUNT_NUM,
                xa.SETTLEMENT_DATE,
                xa.SETTLEMENT_TRANSACTION_ID,
                xt.PAYMENT_AGENT_NAME,
                xt.PC_TRANSACTION_ID,
                xt.CREATED_DATE_TIME,
                xt.PAYMENT_TRANS_DATE
FROM
    T_SETTLEMENT_Transaction xt,
    T_SETTLEMENT_Activity xa
where xa.SETTLEMENT_DATE <='2013-01-10 21:00:00.937'
  and xa.SETTLEMENT_STATUS='PEND'
  and xa.SETTLEMENT_TRANS_TYPE in('FEE','STL')
  and xt.SETTLEMENT_TRANSACTION_ID=xa.SETTLEMENT_TRANSACTION_ID 
group by xa.SETTLEMENT_TRANSACTION_ID,
         xa.ROUTING_NUMBER,
         xa.BANK_ACCOUNT_NUM,
         xa.SETTLEMENT_DATE,
         xa.SETTLEMENT_TRANSACTION_ID, 
         xt.PAYMENT_AGENT_NAME,
         xt.PC_TRANSACTION_ID,
         xt.CREATED_DATE_TIME,
         xt.PAYMENT_TRANS_DATE;

在解决 SQL 问题时,将其简化为这样通常会有所帮助。

SELECT DISTINCT 
    sum(xa.SETTLEMENT_AMOUNT) AMT,
    xa.SETTLEMENT_TRANSACTION_ID
FROM
    T_SETTLEMENT_Transaction xt,
    T_SETTLEMENT_Activity xa
where xt.SETTLEMENT_TRANSACTION_ID=xa.SETTLEMENT_TRANSACTION_ID 
group by xa.SETTLEMENT_TRANSACTION_ID;

如果可行,您可以添加更多列和 WHERE 条件。

在未来发布工作 DDL 和 SQL INSERT 语句。这将使这里的人们只需粘贴代码并运行它。比试图找出所有列错误命名、数据截断和误导性数据要快得多。

于 2013-01-05T16:06:57.170 回答