其他用户的国家数

时间:2020-08-02 09:28:55

标签: postgresql

我有三个表 trip_country_sets 用户 trip

CREATE TABLE trip_country_sets (
tc_set_id varchar PRIMARY KEY,
trip_id varchar,
user_id varchar,
country varchar
);

CREATE TABLE users (
user_id varchar PRIMARY KEY,
name_t varchar
);

CREATE TABLE trip (
trip_id varchar PRIMARY KEY,
owner_id varchar,
title varchar
);insert into trip_country_sets values('tc1', 'trip1', 'usr1', 'counrty_test1');
insert into trip_country_sets values('tc2', 'trip2', 'usr2', 'counrty_test1');
insert into trip_country_sets values('tc3', 'trip3', 'usr3', 'counrty_test2');
insert into trip_country_sets values('tc4', 'trip4', 'usr1', 'counrty_test2');
insert into trip_country_sets values('tc5', 'trip5', 'usr2', 'counrty_test3');
insert into trip_country_sets values('tc6', 'trip6', 'usr3', 'counrty_test3');
insert into trip_country_sets values('tc7', 'trip3', 'usr1', 'counrty_test4');
insert into trip_country_sets values('tc8', 'trip4', 'usr2', 'counrty_test4');
insert into trip_country_sets values('tc9', 'trip1', 'usr3', 'counrty_test5');
insert into trip_country_sets values('tc10', 'trip2', 'usr1', 'counrty_test5');
insert into trip_country_sets values('tc11', 'trip1', 'usr2', 'counrty_test1');
insert into trip_country_sets values('tc12', 'trip1', 'usr3', 'counrty_test1');
insert into trip_country_sets values('tc13', 'trip4', 'usr2', 'counrty_test3');
insert into trip_country_sets values('tc14', 'trip5', 'usr1', 'counrty_test5');
insert into trip_country_sets values('tc15', 'trip6', 'usr2', 'counrty_test6');
insert into trip_country_sets values('tc16', 'trip2', 'usr3', 'counrty_test3');
insert into trip_country_sets values('tc17', 'trip1', 'usr2', 'counrty_test5');

insert into users values('usr1', 'name1');
insert into users values('usr2', 'name2');
insert into users values('usr3', 'name3');

insert into trip values('trip1', 'usr1', 'title1');
insert into trip values('trip2', 'usr2', 'title2');
insert into trip values('trip3', 'usr3', 'title3');
insert into trip values('trip4', 'usr1', 'title4');
insert into trip values('trip5', 'usr5', 'title5');
insert into trip values('trip6', 'usr1', 'title6');
insert into trip values('trip7', 'usr4', 'title4');
insert into trip values('trip8', 'usr1', 'title5');
insert into trip values('trip9', 'usr6', 'title6');

如何进行查询以查找用户的所有国家,不属于该用户的国家/地区

select
    trip_country_sets.country,
    count(distinct trip_country_sets.tc_set_id) as country_count
from trip_country_sets
    LEFT OUTER join trip on trip_country_sets.trip_id = trip.trip_id
    LEFT OUTER JOIN users on trip_country_sets.user_id = users.user_id
where
    trip.owner_id = 'usr1'
    and trip_country_sets.user_id <> 'usr1'
group by trip_country_sets.country, trip_country_sets.tc_set_id;

我的查询结果仅影响 trip_country_sets 表中的国家的国家/地区国家/地区 WHERE 值与查询 trip.owner_id ='usr1' trip_country_sets.user_id <>'usr1'的定义有关 我想要以下结果:

---------------------------------
|    country    | country_count |
---------------------------------
| counrty_test1 |       2       |
| counrty_test3 |       2       |
| counrty_test6 |       1       |
| counrty_test5 |       2       |
| counrty_test4 |       1       |

工作示例: https://rextester.com/live/GOKNM17080

2 个答案:

答案 0 :(得分:1)

如果您想按用户列出确实属于该用户的国家/地区列表,以及该用户仍然缺少的国家/地区数量,请​​考虑以下事项:

-- First, count the number of countries that a user can have
WITH total_countries AS (
  SELECT COUNT(distinct country) total
  FROM trip_country_sets
)
SELECT users.id, -- or name
       ARRAY_AGG(DISTINCT country), -- DISTINCT if the user can have multiple trips
       (SELECT total FROM total_countries) - COUNT(DISTINCT country) as missing_countries
FROM users
LEFT OUTER JOIN trip_country_sets on trip_country_sets.user_id = users.user_id
GROUP BY users.id

答案 1 :(得分:0)

我找到了答案。也许我没有完全解释这个问题。这是我的搜索查询,这是正确的答案:

select
    trip_country_sets.country,
    count(distinct trip_country_sets.tc_set_id) as country_count
from trip_country_sets
    LEFT OUTER join trip on trip_country_sets.trip_id = trip.trip_id
    LEFT OUTER JOIN users on trip_country_sets.user_id = users.user_id
where
    trip.owner_id = 'usr1'
    and trip_country_sets.user_id <> 'usr1'
group by trip_country_sets.country;

此查询返回我想要的结果:

---------------------------------
|    country    | country_count |
---------------------------------
| counrty_test1 |       2       |
| counrty_test3 |       2       |
| counrty_test6 |       1       |
| counrty_test5 |       2       |
| counrty_test4 |       1       |