Create table RequestAccepted (requesterid int not null, accepterid int null, acceptdate…
- User
Create table RequestAccepted (requester_id int not null, accepter_id int null, accept_date date null) Truncate table RequestAccepted insert into RequestAccepted (requester_id, accepter_id, accept_date) values ('1', '2', '2016/06/03') insert into RequestAccepted (requester_id, accepter_id, accept_date) values ('1', '3', '2016/06/08') insert into RequestAccepted (requester_id, accepter_id, accept_date) values ('2', '3', '2016/06/08') insert into RequestAccepted (requester_id, accepter_id, accept_date) values ('3', '4', '2016/06/09')
select * from RequestAccepted
-- find people who have most friends
- Assistant
To find the people who have the most friends, we can use a combination of count and group by clause in SQL. Here is the query:
select requester_id, count(accepter_id) as num_friends from RequestAccepted group by requester_id order by num_friends descThis query will give us the requester_id and the number of accepter_ids (i.e., friends) for each requester. The result will be ordered in descending order of the number of friends, so the people with the most friends will appear at the top of the result set.
- User
-- find people who have most friends
select top 1 count(id) as id from (select requester_id as id from RequestAccepted union all select accepter_id from RequestAccepted) a group by id order by id desc
- Assistant
The query you provided will give you the person who has the maximum number of friends across both the requester_id and accepter_id columns in the RequestAccepted table. However, it does not provide the list of people who have the most friends.
To get the list of people who have the most friends, you can modify the query slightly:
select id, count(*) as num_friends from ( select requester_id as id from RequestAccepted union all select accepter_id as id from RequestAccepted ) a group by id having count(*) = ( select max(friend_count) from ( select count(*) as friend_count from ( select requester_id as id from RequestAccepted union all select accepter_id as id from RequestAccepted ) b group by id ) c )This query finds the count of friends for each person (using both the requester_id and accepter_id), then selects only the people who have the maximum number of friends. The result will be a list of people (id) and their corresponding count of friends (num_friends).
- User
Create table Tree (id int, p_id int) Truncate table Tree insert into Tree (id, p_id) values ('1', NULL) insert into Tree (id, p_id) values ('2', '1') insert into Tree (id, p_id) values ('3', '1') insert into Tree (id, p_id) values ('4', '2') insert into Tree (id, p_id) values ('5', '2')
select * from Tree
-- if there is no parent id report root then if that id not parent id report leaf, inners id also located as parent id expected result: ---- id | type | --+----+-------+ --| 1 | Root | --| 2 | Inner | --| 3 | Leaf | --| 4 | Leaf | --| 5 | Leaf |
- Assistant
To achieve the expected result, you can use a combination of the CASE statement and a subquery to determine the type of each node in the Tree table. Here is the updated query:
select id, case when id in (select id from Tree where p_id is null) then 'Root' when id in (select distinct p_id from Tree) then 'Inner' else 'Leaf' end as type from TreeThis query uses the CASE statement to check each node's id against conditions. If the id is present in the subquery where p_id is null, it is categorized as 'Root'. If the id is present in the subquery that selects distinct p_id values, it is categorized as 'Inner'. Finally, if the id does not meet either of these conditions, it is categorized as 'Leaf'. The result will provide the id and the corresponding type for each node in the Tree table.