sql - Find the user with the most contact -


Users send messages in each other message box, find a user, which most other users know (get and Send ??)

  • USER_ID
  • USER_GROUP
  • MESSAGE_ID

    / li>

  • MESSAGE_MESSAGE
  • MESSAGE_FROM_USER
  • M_ESSAGE_TO_USER
  • A user can send and receive, so who knows other users (Send and receive by ??) < / P>

    Using SQL Server 2005+, CTE:


      As with usr_list (SELECT x.user_id, select x.message_from_user as tab x union for other user y.user_id, y.message_to_user as other_user tab Y) SELECT TOP 1 ul.user_id, COUNT ( *) AS Un-CTE's equivalent: num_friends as  
      SELECT TOP 1 ul.user_id, COUNT (node ​​CTE equivalent:  

    *) FROM (SELECT x.user_id, x.message_from_user as other_user from table x UNION SELECT y.user_id, y.message_to_user as other_user table) and UL by UL Group Num_friends DESC

    by User_id ORDER

    Comments

    Popular posts from this blog

    oracle - The fastest way to check if some records in a database table? -

    php - multilevel menu with multilevel array -

    jQuery UI: Datepicker month format -