Left Join Not Returning All Rows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Julian32
    New Member
    • Oct 2006
    • 18

    Left Join Not Returning All Rows

    Hello All,

    Sorry for the double post but I realized this topic would fit better in this forum.

    Before this I'd never before needed to use the "LEFT JOIN" command, and unfortunately it's giving me some problems.

    I have three tables setup as follows (note they are test tables for a prototype of a larger project):

    user_table:
    | UserID | Username | Password|
    | 1 | julian | abcdef |
    | 2 | test | test |


    file_table:
    | FileID | UserID | FileAccess |
    | 1 | 1 | file1.txt|
    | 2 | 1 | file2.txt |
    | 3 | 2 | file2.txt |

    list_table:
    | ListID | FileName |
    | 1 | file1.txt |
    | 2 | file2.txt |
    | 3 | file3.txt |

    I want to run a query that will display all rows of "FileName" on the second column, and "FileAccess " on the second column IF FileAccess is equal to FileName AND UserID in TableB is equal to a variable which is given a value in a previous part of my program. (For this test it has been given the value two.)

    This is my query:

    Code:
    sql = "SELECT file_list.FileName, file_table.FileAccess FROM file_list "
    sql = sql & "LEFT JOIN file_table ON file_list.FileName = file_table.FileAccess "
    sql = sql & "WHERE file_table.UserID =  2"
    This is what it returns:

    FileName | FileAccess
    file2.txt | file2.txt

    This is what I WANT it to return:
    FileName | FileAccess
    file1.txt |
    file2.txt | file2.txt

    I've tried all types of variations of the query and even tried to make it a right join all without success. Any help with the query will be greatly appreciated.
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Try this:

    sql = "SELECT file_List.FileN ame, IIf(file_table. UserID=2,file_t able.FileAccess ,"") " & _
    "FROM file_List LEFT JOIN file_table " & _
    " ON file_List.FileN ame=file_table. FileName"


    I want to run a query that will display all rows of "FileName" on the second column, and "FileAccess " on the second column IF FileAccess is equal to FileName AND UserID in TableB is equal to a variable which is given a value in a previous part of my program. (For this test it has been given the value two.)
    if user id is being passed as a variable let me know the variable name and I'll change the above accordingly

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      You seem to have the main table second.
      I don't know if this would work with a RIGHT JOIN as I never use them, but this is how I would do it.

      BTW I'm assuming file_list maps to list_table in your explanation data.
      Code:
      sql = "SELECT file_list.FileName, file_table.FileAccess " & _
      "FROM file_table LEFT JOIN file_list " & _
      "ON file_table.FileAccess = file_list.FileName " & _
      "WHERE file_table.UserID = 1"
      In your code you had the WHERE clause select records where UserID = 2 which would explain your results.

      Comment

      • Julian32
        New Member
        • Oct 2006
        • 18

        #4
        Hello all,

        Thanks to both of you for your help.

        I will repost the three tables since as was pointed out by NeoPa I incorrecly named some tables.


        user_table:
        | UserID | Username | Password|
        | 1 | julian | abcdef |
        | 2 | test | test |


        file_table:
        | FileID | UserID | FileAccess |
        | 1 | 1 | file1.txt|
        | 2 | 1 | file2.txt |
        | 3 | 2 | file2.txt |

        file_list:
        | ListID | FileName |
        | 1 | file1.txt |
        | 2 | file2.txt |

        Basically I want to query these tables to have on the first column a list of the filenames (file_list.File Name), followed by another column with the same value (file_table.Fil eAccess) if the User has access to the file.

        Originally posted by mmccarthy
        Try this:

        sql = "SELECT file_List.FileN ame, IIf(file_table. UserID=2,file_t able.FileAccess ,"") " & _
        "FROM file_List LEFT JOIN file_table " & _
        " ON file_List.FileN ame=file_table. FileName"



        if user id is being passed as a variable let me know the variable name and I'll change the above accordingly
        Hello mmcarthy,

        I tried this method and it came close to working except that the iif always returns NULL. I end up with this (I changed the my mistaken table and column names so that's not the cause of the error):

        FileName | Expr1001
        file1.txt |
        file2.txt | file2.txt
        file2.txt |

        I tried SELECT DISTINCT to avoid file2.txt from being queried twice (don't even know why it's doing that since there's only one file2.txt in "FileName") and all it did was move file2.txt on the second column down one row.

        Originally posted by NeoPa
        You seem to have the main table second.
        I don't know if this would work with a RIGHT JOIN as I never use them, but this is how I would do it.

        BTW I'm assuming file_list maps to list_table in your explanation data.
        Code:
        sql = "SELECT file_list.FileName, file_table.FileAccess " & _
        "FROM file_table LEFT JOIN file_list " & _
        "ON file_table.FileAccess = file_list.FileName " & _
        "WHERE file_table.UserID = 1"
        In your code you had the WHERE clause select records where UserID = 2 which would explain your results.
        Hello NeoPa,

        The table which I want all rows to be displayed from is file_list since I want to be able to see every file, and a corresponding column with the same file name if the user has access to it. I did try your code and surprisingly it returns the same recordset. BTW the UserId will be compared to another variable which can either have the value of 1 or 2 (to see the File Access Privileges of both user 1 and 2).

        Thanks again for your help, if either of you see something I overlooked I'd appreciate any help. In the meanwhile I'll try to do this in two different quieres.

        Thanks,
        Julian

        Comment

        • MMcCarthy
          Recognized Expert MVP
          • Aug 2006
          • 14387

          #5
          try this:

          sql = "SELECT file_list.FileN ame, First(file_tabl e.FileAccess) As FAccess " & _
          "FROM file_table RIGHT JOIN file_List " & _
          "ON file_table.File Name = file_List.FileN ame " & _
          "WHERE file_table.User ID = 2 " & _
          "GROUP BY file_list.FileN ame;"

          BTW I am assuming UserID is a number ...

          Comment

          • Julian32
            New Member
            • Oct 2006
            • 18

            #6
            Originally posted by mmccarthy
            try this:

            sql = "SELECT file_list.FileN ame, First(file_tabl e.FileAccess) As FAccess " & _
            "FROM file_table RIGHT JOIN file_List " & _
            "ON file_table.File Name = file_List.FileN ame " & _
            "WHERE file_table.User ID = 2 " & _
            "GROUP BY file_list.FileN ame;"

            BTW I am assuming UserID is a number ...
            Hello mmccarthy,

            I'm still receiving the same output:

            FIleName | FAccess
            file2.txt | file2.txt

            For some reason it's not displaying file1.txt from FileName and NULL for the corresponding FAccess column. And yes UserID is a number.

            Thanks for helping,
            Julian

            Comment

            • MMcCarthy
              Recognized Expert MVP
              • Aug 2006
              • 14387

              #7
              try this:

              Code:
               
              sql = "SELECT file_list.FileName, " & _
              "IIf(IsNull(file_table.FileAccess),"",file_table.FileAccess) As FAccess " & _
              "FROM file_table RIGHT JOIN file_List " & _
              "ON file_table.FileName = file_List.FileName " & _
              "WHERE file_table.UserID = 2 " & _
              "GROUP BY file_list.FileName;"

              Comment

              • MMcCarthy
                Recognized Expert MVP
                • Aug 2006
                • 14387

                #8
                Forget the last one, try this instead ...

                Code:
                 
                sql = "SELECT file_list.FileName, " & _
                "IIf(file_table.UserID=2, file_table.FileAccess, "") As FAccess " & _
                "FROM file_table RIGHT JOIN file_List " & _
                "ON file_table.FileName = file_List.FileName " & _
                "GROUP BY file_list.FileName;"

                Comment

                • Julian32
                  New Member
                  • Oct 2006
                  • 18

                  #9
                  Originally posted by mmccarthy
                  Forget the last one, try this instead ...

                  Code:
                   
                  sql = "SELECT file_list.FileName, " & _
                  "IIf(file_table.UserID=2, file_table.FileAccess, "") As FAccess " & _
                  "FROM file_table RIGHT JOIN file_List " & _
                  "ON file_table.FileName = file_List.FileName " & _
                  "GROUP BY file_list.FileName;"
                  Hello mmccarthy,

                  I tried that code but I get an error since in the last paremeter of the IIf ("") are not included since they are processed as string start and end quotes. I tried changing "" to '' and get this error:

                  Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

                  [Microsoft][ODBC Microsoft Access Driver] You tried to execute a query that does not include the specified expression 'IIf(file_table .UserID=2,file_ table.FileAcces s,'')' as part of an aggregate function.

                  Any further help will be greatly appreciated,
                  Julian

                  Comment

                  • MMcCarthy
                    Recognized Expert MVP
                    • Aug 2006
                    • 14387

                    #10
                    Code:
                     
                    sql = "SELECT file_list.FileName, " & _
                    "IIf(file_table.UserID=2, file_table.FileAccess,Null) As FAccess " & _
                    "FROM file_table RIGHT JOIN file_List " & _
                    "ON file_table.FileName = file_List.FileName " & _
                    "GROUP BY file_list.FileName;"
                    Try this ...

                    Comment

                    • Julian32
                      New Member
                      • Oct 2006
                      • 18

                      #11
                      Hello mmcarthy,

                      This query (I combined it with a previous one you gave me) is very close to working:

                      Code:
                      sql = "SELECT file_list.FileName, First(IIf(file_table.UserID=1, file_table.FileAccess,Null)) As FAccess " & _
                      "FROM file_table RIGHT JOIN file_list " & _
                      "ON file_table.FileAccess = file_list.FileName " & _
                      "GROUP BY file_list.FileName"
                      When I tried it with UserID = 2 it worked and produced the desired result. However when I tried it with UserID = 1 it gave me this:

                      FileName | FAccess
                      file1.txt | file1.txt
                      file2.txt |

                      For some reason it's not displaying the second value in FAccess. Perhaps the FIRST clause is only returning the first row?

                      Thanks again,
                      Julian

                      Comment

                      • MMcCarthy
                        Recognized Expert MVP
                        • Aug 2006
                        • 14387

                        #12
                        sql = "SELECT file_list.FileN ame, " & _
                        "IIf(file_table .UserID=2, file_table.File Access,Null) As FAccess " & _
                        "FROM file_table RIGHT JOIN file_List " & _
                        "ON file_table.File Name = file_List.FileN ame " & _
                        "GROUP BY file_list.FileN ame;"


                        Originally posted by Julian32
                        Hello mmcarthy,

                        This query (I combined it with a previous one you gave me) is very close to working:

                        Code:
                        sql = "SELECT file_list.FileName, First(IIf(file_table.UserID=1, file_table.FileAccess,Null)) As FAccess " & _
                        "FROM file_table RIGHT JOIN file_list " & _
                        "ON file_table.FileAccess = file_list.FileName " & _
                        "GROUP BY file_list.FileName"
                        When I tried it with UserID = 2 it worked and produced the desired result. However when I tried it with UserID = 1 it gave me this:

                        FileName | FAccess
                        file1.txt | file1.txt
                        file2.txt |

                        For some reason it's not displaying the second value in FAccess. Perhaps the FIRST clause is only returning the first row?

                        Thanks again,
                        Julian
                        If you take off the First() statement will it work ..

                        Comment

                        • Julian32
                          New Member
                          • Oct 2006
                          • 18

                          #13
                          Originally posted by mmccarthy
                          sql = "SELECT file_list.FileN ame, " & _
                          "IIf(file_table .UserID=2, file_table.File Access,Null) As FAccess " & _
                          "FROM file_table RIGHT JOIN file_List " & _
                          "ON file_table.File Name = file_List.FileN ame " & _
                          "GROUP BY file_list.FileN ame;"




                          If you take off the First() statement will it work ..
                          Hello mmccarthy,

                          When I take out the First () statement it gives me this error:

                          You tried to execute a query that does not include the specified expression as part of an aggregate function.

                          I believe it has something to do with the Group by, but if I take it away the first column starts to get duplicated values even with SELECT DISTINCT.

                          Regards,
                          Julian

                          Comment

                          • MMcCarthy
                            Recognized Expert MVP
                            • Aug 2006
                            • 14387

                            #14
                            try this:

                            sql = "SELECT file_list.FileN ame, " & _
                            "IIf(file_table .UserID=2, file_table.File Access,Null) As FAccess " & _
                            "FROM file_table RIGHT JOIN file_List " & _
                            "ON file_table.File Name = file_List.FileN ame " & _
                            "GROUP BY file_list.FileN ame, IIf(file_table. UserID=2, file_table.File Access,Null);"

                            Comment

                            • Julian32
                              New Member
                              • Oct 2006
                              • 18

                              #15
                              Originally posted by mmccarthy
                              try this:

                              sql = "SELECT file_list.FileN ame, " & _
                              "IIf(file_table .UserID=2, file_table.File Access,Null) As FAccess " & _
                              "FROM file_table RIGHT JOIN file_List " & _
                              "ON file_table.File Name = file_List.FileN ame " & _
                              "GROUP BY file_list.FileN ame, IIf(file_table. UserID=2, file_table.File Access,Null);"
                              I receive this error:

                              Syntax error (missing operator) in query expression 'IIf(file_table .UserID=1, file_table.File Access,Null) As FAccess'.

                              Regards,
                              Julian

                              Comment

                              Working...