通过在sql server中连接两个表来获取数据

时间:2013-01-04 07:46:24

标签: sql-server-2008

我有两张桌子

        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)

1 个答案:

答案 0 :(得分: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语句。这将让人们只需粘贴代码并运行它。比试图弄清楚所有列错误,数据截断和误导性数据要快得多。