INSERT INTO calaccess_processed_filings_form460scheduleb1item (
    filing_id,
    line_item,
    lender_code,
    lender_committee_id,
    lender_title,
    lender_lastname,
    lender_firstname,
    lender_name_suffix,
    lender_city,
    lender_state,
    lender_zip,
    lender_employer,
    lender_occupation,
    lender_is_self_employed,
    treasurer_title,
    treasurer_lastname,
    treasurer_firstname,
    treasurer_name_suffix,
    treasurer_city,
    treasurer_state,
    treasurer_zip,
    intermediary_title,
    intermediary_lastname,
    intermediary_firstname,
    intermediary_name_suffix,
    intermediary_city,
    intermediary_state,
    intermediary_zip,
    begin_period_balance,
    amount_received,
    amount_paid,
    amount_forgiven,
    end_period_balance,
    date_due,
    interest_paid,
    interest_rate,
    original_amount,
    date_incurred,
    cumulative_ytd_contributions,
    transaction_id,
    memo_reference_number
)
SELECT
    filing.filing_id,
    item_version.line_item,
    item_version.lender_code,
    item_version.lender_committee_id,
    item_version.lender_title,
    item_version.lender_lastname,
    item_version.lender_firstname,
    item_version.lender_name_suffix,
    item_version.lender_city,
    item_version.lender_state,
    item_version.lender_zip,
    item_version.lender_employer,
    item_version.lender_occupation,
    item_version.lender_is_self_employed,
    item_version.treasurer_title,
    item_version.treasurer_lastname,
    item_version.treasurer_firstname,
    item_version.treasurer_name_suffix,
    item_version.treasurer_city,
    item_version.treasurer_state,
    item_version.treasurer_zip,
    item_version.intermediary_title,
    item_version.intermediary_lastname,
    item_version.intermediary_firstname,
    item_version.intermediary_name_suffix,
    item_version.intermediary_city,
    item_version.intermediary_state,
    item_version.intermediary_zip,
    item_version.begin_period_balance,
    item_version.amount_received,
    item_version.amount_paid,
    item_version.amount_forgiven,
    item_version.end_period_balance,
    item_version.date_due,
    item_version.interest_paid,
    item_version.interest_rate,
    item_version.original_amount,
    item_version.date_incurred,
    item_version.cumulative_ytd_contributions,
    item_version.transaction_id,
    item_version.memo_reference_number
FROM calaccess_processed_filings_form460filing filing
JOIN calaccess_processed_filings_form460filingversion filing_version
ON filing.filing_id = filing_version.filing_id
AND filing.amendment_count = filing_version.amend_id
JOIN calaccess_processed_filings_form460scheduleb1itemversion item_version
ON filing_version.id = item_version.filing_version_id;
