SELECT
txn.id AS id,
txn.timestamp AS timestamp,
txn.added_at AS added_at,
txn.amount AS amount,
txn.currency AS currency,
txn.description AS description,
txn.internal_transfer AS internal_transfer,

txn.vendor_id AS vendor_id,
vendor.name AS vendor_name,
vendor.description AS vendor_description,

txn.category_id AS category_id,
category.parent_id AS category_parent_id,
category.name AS category_name,
category.description AS category_description,
category.color AS category_color,

account_credit.id AS credited_account_id,
account_credit.name AS credited_account_name,
account_credit.type AS credited_account_type,
account_credit.number_suffix AS credited_account_number_suffix,

account_debit.id AS debited_account_id,
account_debit.name AS debited_account_name,
account_debit.type AS debited_account_type,
account_debit.number_suffix AS debited_account_number_suffix,

GROUP_CONCAT(tag) AS tags
FROM
"transaction" txn
LEFT JOIN transaction_vendor vendor
	ON vendor.id = txn.vendor_id
LEFT JOIN transaction_category category
	ON category.id = txn.category_id
LEFT JOIN account_journal_entry j_credit
	ON j_credit.transaction_id = txn.id AND UPPER(j_credit.type) = 'CREDIT'
LEFT JOIN account account_credit
	ON account_credit.id = j_credit.account_id
LEFT JOIN account_journal_entry j_debit
	ON j_debit.transaction_id = txn.id AND UPPER(j_debit.type) = 'DEBIT'
LEFT JOIN account account_debit
	ON account_debit.id = j_debit.account_id
LEFT JOIN transaction_tag tags ON tags.transaction_id = txn.id
WHERE txn.id = ?
GROUP BY txn.id