B
B
Boris Boyko2015-11-16 19:41:25
SQL
Boris Boyko, 2015-11-16 19:41:25

How to make such sql query?

Travel agency theme. People fly to different countries.
Question : how to withdraw those who have been to " 2 countries at least twice "?
There is a table "order" in which fields, for example, date, country, client. How to make such requests? Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2015-11-16
@Fibonachy

For MySQL it would be something like this:

SELECT `client_id`
    FROM (
        SELECT `client_id`, `country_id`
            FROM `orders`
            GROUP BY `client_id`, `country_id`
            HAVING COUNT(*) > 1
    ) AS `t1`
    GROUP BY `client_id`
    HAVING COUNT(*) > 1

In MSSQL, the principle will be the same, but there may be some peculiarities.

A
app25, 2015-11-17
@app25

with cl ( contry,client,koll) as
(select contry,client ,COUNT(*) over (partition by client,contry)
from orders
)
select distinct * from cl
where koll >=2
on MSSQL you can try like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question