SQL Multi-Table Question

  • I have 3 tables:

    SOHEADER (sales order header info)

    SOLINE (sales order line-item info) and

    XCCTRAN (credit card transaction table).

    SOHEADER is outer joined 1:* SOLINE

    SOHEADER is outer joined 1:* XCCTRAN

    The problem is because I have two 1:* joins (All joins on the same OrdNbr field), my line-item detail is duplicated if there are more than 1 credit card table entry for the join on order number.

    I want to basically just pull the first XCCTRAN record that exists.  Any ideas on how to do the select on XCCTRAN to pull just the "first" transaction XCCTRAN if there are multi-transactions? 

    Thanks,

    Leslie

  • You can try something along the lines: 

    SELECT x, y, z

    FROM SOHEADER HDR

         JOIN SOLINE LINE ON( HDR.OrdNbr = LINE.OrdNbr)

         JOIN (SELECT TOP 1 TRAN.OrdNbr FROM XCCTRAN TRAN

                   INNER JOIN SOHEADER SHDR ON TRAN.OrdNbr = SHDR.OrdNbr) XCC ON( HDR.OrdNbr = XCC.OrdNbr)

     

    I wasn't born stupid - I had to study.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply