D
D
David Martin2014-10-24 04:53:38
MySQL
David Martin, 2014-10-24 04:53:38

How to make a selection from SQL most efficient?

It is necessary to solve the problem in SQL as efficiently as possible, which is quite simple.
But so far I can't find an easy way.
Given. 2 tables.
1 table. client id, client request number
2 table. client's request number, date, cost of the order on request
It is necessary to display the amount per month for clients who have more than N orders.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Entelis, 2014-10-24
@david_martin

If I understand you correctly, then something like this

select
  `client`.`id`,
  sum(`order`.`price`)
from `order`
join `client` on `order`.`client_id` = `client`.`client_id`
where `order`.`datetime` between "2014-10-01 00:00:00" and "2014-10-31 23:59:59"
group by `client`.`id`
having count(`order`.`id`) > 2

V
Viktor Koltcov, 2014-10-24
@Vityarik

look towards group by and having

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question