PostgreSQL中的第一个事务

PostgreSQL中的第一个事务,postgresql,Postgresql,我的目标是在子查询中获得一组客户的第一笔交易,然后对它们进行平均。这是我正在使用的查询: SELECT DISTINCT ON (account_id) created_at AS "first_transaction_date" , account_id , money_amount FROM transactions WHERE status = 'accepted' 我得到了以下数据 first_transaction_date account_id Money_amo

我的目标是在子查询中获得一组客户的第一笔交易,然后对它们进行平均。这是我正在使用的查询:

SELECT  DISTINCT ON (account_id) created_at AS "first_transaction_date"
, account_id
, money_amount
FROM    transactions
WHERE   status = 'accepted' 
我得到了以下数据

first_transaction_date  account_id  Money_amount
2017-05-01              1           25
我的问题是,我得到了错误的信息,因为这不是该客户的第一笔交易

谢谢你的帮助

提前谢谢

斯特凡


另外,这是我第一次在这里发帖,如果我遗漏了任何内容,我深表歉意。您没有对任何内容进行排序,PostgreSQL无法保证行顺序符合创建顺序。 只需添加以下ORDERBY子句就足够了:

SELECT  DISTINCT ON (account_id) created_at AS "first_transaction_date"
, account_id
, money_amount
FROM    transactions
WHERE   status = 'accepted'
ORDER BY account_id, created_at

最少的样本数据和期望的结果将是有益的