简体   繁体   中英

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 . The other table is transactions with columns id , entity_id and created_at . The foreign keys linking the two tables are integration_id and entity_id .

The transactions table shows the number of transactions received from each company from the entities table.

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

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?

EDIT:

playing around with the query, I'm wondering if there is a way I can

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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