Using SQL join and subquery to query two tables in R

后端 未结 1 1854
既然无缘
既然无缘 2021-01-19 07:15

I\'m a beginner.

I have two .txt files and I\'m using R with sqldf pakage to query them

The first table (venues.txt) look like this:

userID         


        
相关标签:
1条回答
  • 2021-01-19 07:30

    I am a Java pl/sql developer so here is my shot to answer to:"a list of venues that were visited by at least two friends" using only join and assuming data from venues.txt is called venues and friends.txt file is called friend in the FROM clause. Basically, I am assuming that those files are tables.

    SELECT v1.venueID, f.friendID
    
    FROM venues v1 
    INNER JOIN friends f ON v1.userID = f.userID 
    INNER JOIN venues v2 ON v2.userID = f.friendID
    
    WHERE
       v1.venueID = v2.venueID
    

    and if you want to add more conditions i.e."at least two friends visited together, so having the same year, month, date, hour" then just add them to the filter(WHERE clause). So the query would look like this:

    SELECT v1.venueID, f.friendID
    
    FROM venues v1 
    INNER JOIN friends f ON v1.userID = f.userID 
    INNER JOIN venues v2 ON v2.userID = f.friendID
    
    WHERE
       v1.venueID = v2.venueID
       v1.year = v2.year
       v1.month = v2.month
       v1.date = v2.date
       v1.hour = v2.hour
    

    You might need to use DISTINCT in the SELECT statement if there are more than 2 friends at the venue(or optionally at the same time).

    0 讨论(0)
提交回复
热议问题