Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    nightangel73 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    May 2014
    Posts
    45

    Need help with select query statement for fields in different tables

    Okay I have this query statement



    SELECT "Acquisition" AS MovementType, LotNumber, Quantity, UserID
    FROM StockAcquisitions

    UNION ALL
    SELECT "Withdrawal" , LotNumber, Quantity*-1, UserID
    FROM StockWithdrawals

    UNION ALL SELECT "Stocktake", LotNumber, Quantity, UserID
    FROM StockTakes;


    Now what I want is that this query also displays me the UserName. But the UserName is in the table Users and I don't know how to join this field already having this union all statement. Help as always appreciated.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You can have the JOIN take place in each of the SELECT lines or you can build another query that uses the UNION query and the Users table joining on the UserID.

    Use the query builder to help you get the correct syntax for the SELECT JOIN and then edit the UNION.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    nightangel73 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    May 2014
    Posts
    45
    Quote Originally Posted by June7 View Post
    You can have the JOIN take place in each of the SELECT lines or you can build another query that uses the UNION query and the Users table joining on the UserID.

    Use the query builder to help you get the correct syntax for the SELECT JOIN and then edit the UNION.
    I tried with join statement but it didn't work but probably is that I don't know how to code it right. I tried doing a new query but this union query doesn't let do anything else but to modify that code.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    What does 'didn't work' mean - error message, wrong results, nothing? Did you use the query builder to get the sql syntax for the JOIN?

    Post your attempted SQL statement for analysis.

    A UNION query can be used in another query so I don't understand your last statement.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    nightangel73 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    May 2014
    Posts
    45
    ELECT "Acquisition" AS MovementType, LotNumber, Quantity, UserId, 'UserID.Name
    Join Users
    on StockAcquisitions.UserID = Users.UserID
    FROM StockAcquisitions


    UNION ALL
    SELECT "Withdrawal" , LotNumber, Quantity*-1, UserId, UserID.Name
    Join Users
    on 'StockAcquisitions.UserID = Users.UserID

    FROM StockWithdrawals


    UNION ALL SELECT "Stocktake", LotNumber, Quantity , UserId, UserID.Name
    Join Users
    on' StockTakes.UserID = Users.UserID
    FROM StockTakes;



    It says it's syntax error..

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Missing join type (INNER, RIGHT, LEFT) and it is in wrong position.

    Did you really use query builder to get those SELECT statements?

    SELECT "Acquisition" AS MovementType, LotNumber, Quantity, Users.UserId, [Name] FROM StockAcquisitions INNER JOIN Users ON StockAcquisitions.UserID = Users.UserID

    UNION ...


    BTW, Name is a reserved word and should avoid reserved words as field names.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    nightangel73 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    May 2014
    Posts
    45
    Quote Originally Posted by June7 View Post
    Missing join type (INNER, RIGHT, LEFT) and it is in wrong position.

    Did you really use query builder to get those SELECT statements?

    SELECT "Acquisition" AS MovementType, LotNumber, Quantity, Users.UserId, [Name] FROM StockAcquisitions INNER JOIN Users ON StockAcquisitions.UserID = Users.UserID

    UNION ...


    BTW, Name is a reserved word and should avoid reserved words as field names.
    Oh no I couldn't use the query builder with the union all statement. I tried to create new query in wizard and call this query but it said that query is out reach for the wizard...

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    My suggestion was to use the query builder to construct the SELECT statement. Then replicate that statement over in the UNION query for each SELECT line.

    As for using the UNION or UNION ALL (once it is properly constructed) in another query - that is definitely possible. I have done it.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    nightangel73 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    May 2014
    Posts
    45
    Okay using the code builder I have now this query. It works well but the problem is it repeats the transaction 4 times. I have no idea why it does 4 times.

    SELECT Users.Name, qryStockMovements.MovementType, qryStockMovements.LotNumber, qryStockMovements.Quantity
    FROM qryStockMovements, ((Users INNER JOIN StockAcquisitions ON Users.UserID = StockAcquisitions.UserID) INNER JOIN StockTakes ON Users.UserID = StockTakes.UserID) INNER JOIN StockWithdrawals ON Users.UserID = StockWithdrawals.UserID;

  10. #10
    lfpm062010 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Location
    US
    Posts
    415
    You use "UNION ALL". It means all the records on all tables.

    "UNION" should remove the duplicate. If that is what you want.

    For more information,

    http://msdn.microsoft.com/en-us/libr...ffice.15).aspx

  11. #11
    nightangel73 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    May 2014
    Posts
    45
    It still gives me the duplicates (not dupplicate but twiceduplicate lol).. I really have to figure this one out I appreciate so much your help.

  12. #12
    lfpm062010 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Location
    US
    Posts
    415
    Can you post your query?

    UNION should only select distinct value. unless the tables already have duplicate value.

  13. #13
    nightangel73 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    May 2014
    Posts
    45
    Experimenting here is this other query I built. This one will give me what I want too but it asks me parameter value userid and name

    SELECT LotNumber, Quantity, Users.UserID, Users.Name
    FROM StockAcquisitions

    UNION
    SELECT LotNumber, Quantity*-1, Users.UserID, Users.Name
    FROM StockWithdrawals

    UNION SELECT LotNumber, Quantity, Users.UserID, Users.Name

    FROM StockTakes;

  14. #14
    nightangel73 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    May 2014
    Posts
    45
    This is the query Stockmovements

    SELECT "Acquisition" AS MovementType, LotNumber, Quantity
    FROM StockAcquisitions

    UNION
    SELECT "Withdrawal" , LotNumber, Quantity*-1
    FROM StockWithdrawals

    UNION SELECT "Stocktake", LotNumber, Quantity
    FROM StockTakes;


    What I want is this same query but with the user name added. But the users are in the table Users

  15. #15
    lfpm062010 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Location
    US
    Posts
    415
    SELECT LotNumber, Quantity, Users.UserID, Users.Name
    FROM StockAcquisitions

    UNION
    SELECT LotNumber, Quantity*-1, Users.UserID, Users.Name
    FROM StockWithdrawals

    UNION SELECT LotNumber, Quantity, Users.UserID, Users.Name

    FROM StockTakes;

    There is no join to "USERS" Table. So, the "Users.UserID" and "Users.Name" is unknown.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Format Conatenated fields in Select Statement
    By KWarzala in forum Programming
    Replies: 8
    Last Post: 04-24-2014, 12:36 PM
  2. Select all query fields for main form?
    By tagteam in forum Access
    Replies: 13
    Last Post: 09-15-2013, 04:35 PM
  3. Aggregating fields in Select Query
    By becka11 in forum Queries
    Replies: 3
    Last Post: 05-03-2013, 09:55 AM
  4. Replies: 1
    Last Post: 02-22-2013, 12:46 PM
  5. Replies: 7
    Last Post: 12-30-2012, 03:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums