2016-02-29 66 views
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 

但这是行不通的,我想知道为什么。感谢您的帮助。

回答

2

使用distinct on

SELECT DISTINCT ON (o.account_id, o.user_id) o.account_id, o.user_id, 
     tx.transaction_id, tx.creation_date AS first_transaction 
FROM transaction tx INNER JOIN 
    order o 
    ON tx.order_id = o.order_id 
WHERE o.test = 'false' AND o.status = 'CAPTURED' 
ORDER BY o.account_id, o.user_id, tx.creation_date ; 
+0

谢谢您的快速响应。为什么这是必要的? –

+0

@JuanDavid。 。 。您需要使用来自第一笔交易的信息为每个“account_id”/“user_id”创建一行。 “明确的”完全是这样的,正确的“按......排序”条款。 –