Fucking financial database

insert into auths select account_id, “authorization” from card_batch_items, customers where card_batch_id = 61 and status = ‘Completed’ and card_batch_items.customer_id = customers.id and “authorization” not in (select number from transactions where number is not null);

insert into transactions (number, date, memo) select “authorization”, ‘2007-8-25’, ‘Credit Card Charge’ from auths;

insert into transaction_items (account_id, amount, transaction_id, number) select case when alternation.i = 1 then account_id else 1552 end as account_id, case when alternation.i = 0 then -amount else amount end as amount, transactions.id as transaction_id, auths.”authorization” as number from card_batch_items, alternation, auths, transactions where card_batch_id = 60 and card_batch_items.status = ‘Completed’ and auths.”authorization” = card_batch_items.”authorization” and transactions.number = auths.”authorization”