简体   繁体   English

SQL查询:如何使用子查询或AVG函数查找新条目之间的天数?

[英]SQL Query: How to use sub-query or AVG function to find number of days between a new entry?

I have a two tables, one called entities with these relevant columns: id , company_id ,and integration_id . 我有两个表,其中一个称为entities具有以下相关列: idcompany_idintegration_id The other table is transactions with columns id , entity_id and created_at . 另一个表是具有identity_idcreated_at列的transactions The foreign keys linking the two tables are integration_id and entity_id . 链接两个表的外键是integration_identity_id

The transactions table shows the number of transactions received from each company from the entities table. transactions表显示了从entities表中每个公司收到的交易数。

Ultimately, I want to find date range with highest volume of transactions occurring and then from that range find the average number of days between transaction for each company. 最终,我想找到发生交易量最大的日期范围,然后从该范围中找到每个公司的两次交易之间的平均天数。

To find the date range I used this query. 为了找到日期范围,我使用了此查询。

SELECT DATE_FORMAT(t.created_at, '%Y/%m/%d'), COUNT(t.id)
FROM entities e
JOIN transactions t
ON ei.id = t.entity_id
GROUP BY t.created_at;

I get this: 我得到这个:

Date_FORMAT(t.created_at, '%Y/%m/%d') | COUNT(t.id)
+-------------------------------------+------------
2015/11/09                             4

etc 等等

From that I determine the range I want to use as 2015/11/09 to 2015/12/27 and I made this query 据此,我确定要用作2015/11/09到2015/12/27的范围,并进行了此查询

SELECT company_id, COUNT(t.id)
FROM entities e
INNER JOIN transactions t
ON e.integration_id = t.entity_id
WHERE tp.created_at BETWEEN '2015/11/09' AND '2015/12/27'
GROUP BY company_id;

I get this: 我得到这个:

company_id  | COUNT(t.id)
+-----------+------------
1234          17

and so on 等等

Which gives me the total transactions made by each company over this date range. 这给了我每个公司在此日期范围内进行的总交易。 What's the best way now to query for the average number of days between transactions by company? 现在,按公司查询两次交易之间的平均天数的最佳方法是什么? How can I sub-query or is there a way to use the AVG function on dates in a WHERE clause? 如何对子查询或在WHERE子句中的日期上使用AVG函数的方法?

EDIT: 编辑:

playing around with the query, I'm wondering if there is a way I can 玩查询,我想知道是否有办法

SELECT company_id, (49 / COUNT(t.id)) ... SELECT company_id, (49 / COUNT(t.id)) ...

49, because that is the number of days in that date range, in order to get the average number of days between transactions? 49,因为那是该日期范围内的天数,才能获得两次交易之间的平均天数?

I think this might be it, does that make sense? 我想可能就是这样,这有意义吗?

I think this may work: 我认为这可能有效:

Select z.company_id, 
datediff(max(y.created_at),min(created_at))/count(y.id) as avg_days_between_orders,
max(y.created_at) as latest_order,
min(created_at) as earliest_order, 
count(y.id) as orders
From
(SELECT entity_id, max(t.created_at) latest, min(t.created_at) earliest
FROM   entities e, transactions t
Where  e.id = t.entity_id
group by entity_id
order by COUNT(t.id) desc
limit 1) x,
transactions y,
entities z
where z.id = x.entity_id
and z.integration_id = y.entity_id
and y.created_at between x.earliest and x.latest
group by company_id;

It's tough without the data. 没有数据很难。 There's a possibility that I have reference to integration_id incorrect in the subquery/join on the outer query. 我有可能在外部查询的子查询/联接中对Integration_id的引用不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM