2017-12-05 7 views
1

国別上位2つのアカウントの一覧を取得したいと考えています。各国の最後の2つのログイン日を持つユーザーは、上位2つのアカウントを決定する基準です。すなわち、目的は、各国の最後の2人の最後の2人を最大の日付で達成することである。ユーザーの最終ログイン日を基準に国別上位2ユーザーを選択

-------------- 
accountsTable 
--------------------------------------- 
id last_login_date   country_id 
1 2016-07-13 13:52:10   3 
2 2016-03-30 10:11:18   3 
3 2016-03-30 10:11:18   1 
4 2017-01-13 22:34:20   2 
5 2017-04-22 12:25:04   2 
6 2017-04-24 15:12:00   1 
7 2017-05-18 10:16:30   4 
8 2017-10-04 23:21:12   4 
9 2017-10-10 00:54:55   2 
10 2017-10-13 14:28:34   2 
11 2017-06-18 10:16:30   4 
--------------------------------------- 


--------------- 
countriesTable 
-------------------------- 
country_id  country 
-------------------------- 
1    UK 
2    France 
3    Germany 
4    Japan 
5    Brazil 
6    Chile 
7    Mexico 
-------------------------- 


-------------- 
Desired Output 
------------------------------------------------ 
country  country_id  last_login_date 
------------------------------------------------ 
UK   1    2017-04-24 15:12:00 
UK   1    2016-03-30 10:11:18  
France  2    2017-10-13 14:28:34 
France  2    2017-10-10 00:54:55 
Germany  3    2016-03-30 10:11:18 
Germany  3    2016-07-13 13:52:10 
Japan  4    2017-05-18 10:16:30 
Japan  4    2017-10-04 23:21:12 
------------------------------------------------ 

select country, countriesTable.country_id, last_login_date 
from countriesTable 
inner join accountsTable 
on countriesTable.country_id = accountsTable.country_id 
group by (country) 
order by last_login_date 
limit 2 

countriesTableのテーブル構造。

create table countriesTable(
    country_id int primary key, 
    country varchar(20) 
); 

国のための挿入表。

insert into countriesTable values (1, 'UK'); 
insert into countriesTable values (2, 'France'); 
insert into countriesTable values (3, 'Germany'); 
insert into countriesTable values (4, 'Japan'); 
insert into countriesTable values (5, 'Brazil'); 
insert into countriesTable values (6, 'Chile'); 
insert into countriesTable values (7, 'Mexico'); 

accountsTableのテーブル構造。

create table accountsTable(
    id int primary key, 
    last_login_date timestamp, 
    country_id int 
); 

アカウント用の挿入表。

insert into accountsTable values(1, '2016-07-13 13:52:10', 3); 
insert into accountsTable values(2, '2016-03-30 10:11:18', 3); 
insert into accountsTable values(3, '2016-03-30 10:11:18', 1); 
insert into accountsTable values(4, '2017-01-13 22:34:20', 2); 
insert into accountsTable values(5, '2017-04-22 12:25:04', 2); 
insert into accountsTable values(6, '2017-04-24 15:12:00', 1); 
insert into accountsTable values(7, '2017-05-18 10:16:30', 4); 
insert into accountsTable values(8, '2017-10-04 23:21:12', 4); 
insert into accountsTable values(9, '2017-10-10 00:54:55', 2); 
insert into accountsTable values(10, '2017-10-13 14:28:34',2); 
insert into accountsTable values(11, '2017-06-18 10:16:30', 4); 

答えて

1

次のクエリは動作するはずです:私はそれを動作させるためにあなたのソリューションを調整しようとしています

select countriesTable.country, countriesTable.country_id, at_.last_login_date 
from countriesTable 
inner join accountsTable at_ 
on countriesTable.country_id = at_.country_id 
where (select count(*) 
     from accountsTable at2 
     where at_.country_id = at2.country_id 
     and at2.last_login_date>=at_.last_login_date 
    )<=2 
order by at_.country_id 
+0

。しかしアカウントのフィールドにlast_login_dateという名前のテーブルはありません –

+0

あなたの質問にもあなたの質問にもあります –

+0

それは正しいです。それは私の間違いです。それはテーブルの中にあります。 –

関連する問題