0
我想要獲取屬於user_id的所有帳戶的歷史記錄中的第一筆交易。例如,在user_id = 12656的情況下,account_id = 1553的較早交易可以是064d1777-3783,但在帳戶1554的情況下,最舊的tx是0cb4cfb2-44a2使用組與訂單通過不起作用
這是表 「命令」
╔═════════╦════════════╦══════════╦══════════╦═══════╗
║ user_id ║ account_id ║ order_id ║ status ║ test ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12656 ║ 1553 ║ 1234 ║ CAPTURED ║ false ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12656 ║ 1554 ║ 5678 ║ CAPTURED ║ false ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12656 ║ 1554 ║ 91011 ║ PENDING ║ false ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12659 ║ 1556 ║ 176344 ║ PENDING ║ true ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12660 ║ 1557 ║ 261677 ║ CAPTURED ║ false ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12661 ║ 1558 ║ 347010 ║ CAPTURED ║ true ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12662 ║ 1559 ║ 432343 ║ CAPTURED ║ false ║
╠═════════╬════════════╬══════════╬══════════╬═══════╣
║ 12663 ║ 1560 ║ 517676 ║ CAPTURED ║ false ║
╚═════════╩════════════╩══════════╩══════════╩═══════╝
相同的表 「交易」
╔═════════╦════════════╦══════════╦════════════════╦═════════════════════════╗
║ user_id ║ account_id ║ order_id ║ transaction_id ║ creation_date ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12656 ║ 1553 ║ 1234 ║ 064d1777-3783 ║ 2012-10-03 00:54:35.042 ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12656 ║ 1554 ║ 5678 ║ 0cb4cfb2-44a2 ║ 2012-05-06 06:33:13.171 ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12656 ║ 1554 ║ 91011 ║ 164494f1-3226 ║ 2012-05-29 00:49:27.611 ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12659 ║ 1556 ║ 176344 ║ 064d1777-3784 ║ 2012-07-25 05:32:48.056 ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12660 ║ 1557 ║ 261677 ║ 0cb4cfb2-44a3 ║ 2012-04-11 00:42:17.176 ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12661 ║ 1558 ║ 347010 ║ 164494f1-3227 ║ 2012-05-16 08:01:38.666 ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12662 ║ 1559 ║ 432343 ║ 064d1777-3785 ║ 2012-04-11 00:48:38.499 ║
╠═════════╬════════════╬══════════╬════════════════╬═════════════════════════╣
║ 12663 ║ 1560 ║ 517676 ║ 0cb4cfb2-44a4 ║ 2012-05-06 06:39:33.44 ║
╚═════════╩════════════╩══════════╩════════════════╩═════════════════════════╝
和我編寫以下查詢:
SELECT order.account_id, order.user_id, tx.transaction_id, MIN(tx.creation_date) AS first_transaction
FROM transaction AS tx
INNER JOIN order AS order ON tx.order_id = order.order_id
WHERE order.test = 'false'
AND order.status = 'CAPTURED'
GROUP BY order.account_id, order.user_id, tx.transaction_id
但這是行不通的,我想知道爲什麼。感謝您的幫助。
謝謝您的快速響應。爲什麼這是必要的? –
@JuanDavid。 。 。您需要使用來自第一筆交易的信息爲每個「account_id」/「user_id」創建一行。 「明確的」完全是這樣的,正確的「按......排序」條款。 –