我有两个表格<代码>用户(一个)和transaction (many)
,我需要从创建用户到进行第一次交易的平均时间。 使用<代码>AVG(TIMESTAMPDIFF)的Im 运行良好,但<代码>除外。 由<<>条/代码>分类的组别,在<条码>中,每个用户平均回报一次,而不是所有独特用户的单一平均数。 删除<代码> 按编码>分类,我获得一个单一的平均数字,但考虑到用户的多项交易,而我只希望每个用户有一次(第一次)。
Here s my SQL:
SELECT AVG(TIMESTAMPDIFF(DAY, u.date_created, t.transaction_date)) AS average
FROM transaction t
LEFT JOIN user u ON u.id = t.user_id
WHERE t.user_id IS NOT NULL AND t.status = 1
GROUP BY t.user_id;
I d appreciate it if someone can help me return the average for unique users only. It s fine to break the query down into two, but the tables are large so returning lots of data and putting it back in is a no-go. Thanks in advance.