SQL Inner Join and filtering out results

后端 未结 1 1871
忘了有多久
忘了有多久 2021-01-29 07:50

I\'m doing a query for a report, and I need help with the INNER JOINs I\'ve been trying to make work for weeks. Its apart of a larger report query, but I need help in this final

相关标签:
1条回答
  • 2021-01-29 08:18

    Try using a sub select in order to get the most recent sequence number, like below:

    SELECT DISTINCT(ca.cust_acct_id)                 
    FROM cwo_work_order_acct ca              
    INNER JOIN
    (
    SELECT 
        act.organization_id,
        act.wkstn_id,
        act.business_date,
        act.rtl_loc_id,
        act.rtrans_lineitm_seq,
        max(act.trans_seq) trans_seq --Get the most recent seq number
      FROM cat_cust_item_acct_activity act                
      GROUP BY
            act.organization_id,
            act.wkstn_id,
            act.business_date,
            act.rtl_loc_id,
            act.rtrans_lineitm_seq        
      ) as acty
      ON  ca.organization_id = acty.organization_id                     
      AND ca.cust_acct_id = acty.cust_acct_id                     
      AND ca.cust_acct_code = acty.cust_acct_code
    
    INNER JOIN cwo_work_order_line_item cli                         
      ON  acty.organization_id = cli.organization_id                         
      AND acty.wkstn_id = cli.wkstn_id                         
      AND acty.trans_seq = cli.trans_seq                         
      AND acty.business_date = cli.business_date                         
      AND acty.rtl_loc_id = cli.rtl_loc_id                         
      AND acty.rtrans_lineitm_seq = cli.rtrans_lineitm_seq              
      AND cli.price_status_enum ='ESTIMATE'
    
    0 讨论(0)
提交回复
热议问题