Results 1 to 11 of 11
  1. #1
    hansdbase is offline Advanced Beginner
    Windows 11 Access 2021
    Join Date
    Nov 2022
    Posts
    33

    Querying two unrelated columns from two unrelated tables


    Suppose I have a table that has parts_no and parts_name with 5 parts totals. So 2 columns and 5 rows in other words. Parts_name is unique. And I have a second unrelated table of departments that has department_no and department_name with 4 departments total. So 2 columns and 4 departments. department_name is unique. When I run a query to select parts_name, department_name from parts, departments; the answer I get appears to cross join the rows.

    But what I want is for it to create only 5 rows total with no duplicate values in any column and where there is not a corresponding value in the department table it should just put a null in that row.

    IS this possible?

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,936
    What do you mean by "corresponding value" - these tables do not have relationship so there is no "corresponding" anything.

    If you want only 5 rows, which part should go with which department? Show example table of what output you expect. Can use forum table builder with the Advanced post editor.
    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
    hansdbase is offline Advanced Beginner
    Windows 11 Access 2021
    Join Date
    Nov 2022
    Posts
    33
    PartID Part
    1 Paint_Brush
    1 Hammer

    DepartmentID Department
    1 HR
    2 Facilities
    3 Marketing

    Select Part, Department from parts, departments

    I want output to be

    Part Department
    Paint_Brush HR
    Hammer Facilities
    Null Marketing

  4. #4
    hansdbase is offline Advanced Beginner
    Windows 11 Access 2021
    Join Date
    Nov 2022
    Posts
    33
    PartID Part
    1 Paint_Brush
    1 Hammer

    DepartmentID Department
    1 HR
    2 Facilities
    3 Marketing

    Select Part, Department from parts, departments

    I want output to be

    Part Department
    Paint_Brush HR
    Hammer Facilities
    Null Marketing

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,936
    Okay, but why? What rule should be applied to cause these associations? What you want makes no sense.


    You show only 2 parts, not 5 and only 3 departments, not 4.
    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.

  6. #6
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    Why should the paintbrush be in HR and not in Marketing or Facilities?
    If partID is de ID of table Parts, it should be different for each part?
    If you want to relate the parts to the departments, you should have a table that links them together.

  7. #7
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Quote Originally Posted by hansdbase View Post
    Suppose I have a table that has parts_no and parts_name with 5 parts totals. So 2 columns and 5 rows in other words. Parts_name is unique. And I have a second unrelated table of departments that has department_no and department_name with 4 departments total. So 2 columns and 4 departments. department_name is unique. When I run a query to select parts_name, department_name from parts, departments; the answer I get appears to cross join the rows.

    But what I want is for it to create only 5 rows total with no duplicate values in any column and where there is not a corresponding value in the department table it should just put a null in that row.

    IS this possible?
    No not in a query.
    And as far as I can see it serves no useful purpose, just list the two tables next to each other.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    madpiet is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    156
    Quote Originally Posted by hansdbase View Post
    Suppose I have a table that has parts_no and parts_name with 5 parts totals. So 2 columns and 5 rows in other words. Parts_name is unique. And I have a second unrelated table of departments that has department_no and department_name with 4 departments total. So 2 columns and 4 departments. department_name is unique. When I run a query to select parts_name, department_name from parts, departments; the answer I get appears to cross join the rows.

    But what I want is for it to create only 5 rows total with no duplicate values in any column and where there is not a corresponding value in the department table it should just put a null in that row.

    IS this possible?
    Are you trying to do something like assign a random part to each department? The only way I can think of doing it is to create a CTE for each so I can add ROW_NUMBER() and then join on the result from the two tables. Sounds like a raffle type thing where each department wins one random prize. That's a really bizarre requirement, but anyway... I think this does what you want:

    Code:
    use tempdb;
    go
    CREATE TABLE Department(
    	DepartmentName VARCHAR(10) PRIMARY KEY
    	);
    GO
    
    
    CREATE TABLE Prize(
    	PrizeName VARCHAR(20) PRIMARY KEY
    	);
    GO
    
    
    INSERT INTO Department(DepartmentName) VALUES ('History'),('English'), ('French'),('Chemistry');
    INSERT INTO Prize(PrizeName) VALUES ('Chewed Sock'),('Old tennis ball'),('nylabone'),('wrench'),('hammer');
    
    
    ;WITH cteDepts(DepartmentName, rn)
    AS (
    SELECT TOP (5) DepartmentName,
    	rn = ROW_NUMBER() OVER (ORDER BY NEWID())
    FROM Department
    ORDER BY ABS(CHECKSUM(NEWID()))
    ),
    ctePrizes(PrizeName, rn)
    AS (
    SELECT TOP(4) PrizeName,
    	rn = ROW_NUMBER() OVER (ORDER BY NEWID())
    FROM PRIZE
    ORDER BY ABS(CHECKSUM(NEWID()))
    )
    SELECT p.PrizeName, d.DepartmentName
    FROM ctePrizes p
    LEFT JOIN cteDepts d
    ON p.rn = d.rn;
    Result:

    PrizeName DepartmentName
    hammer History
    Chewed Sock Chemistry
    nylabone English
    wrench French

  9. #9
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,806
    Likely it makes no sense because the samples are pseudo data and that just leads people down a rabbit hole. Any solution offered usually fails because of the unknown. If we can see no relationship between any pairs of examples shown, it probably means the tables are totally useless as they are, which means going back to the drawing board and not trying to put lipstick on a pig as they say.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    Quote Originally Posted by Micron View Post
    L not trying to put lipstick on a pig as they say.
    Miss Piggy looks very nice with lipstick

  11. #11
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,806
    When I see her, I see bacon.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Two Unrelated Tables and Have to Add together
    By yamalady in forum Queries
    Replies: 3
    Last Post: 11-22-2013, 09:19 AM
  2. Append query using two unrelated tables
    By maggiemago3 in forum Access
    Replies: 1
    Last Post: 09-07-2013, 09:55 PM
  3. Replies: 7
    Last Post: 03-27-2013, 02:26 PM
  4. two unrelated tables and parameter query
    By Seito in forum Queries
    Replies: 2
    Last Post: 09-12-2011, 03:01 PM
  5. Combining results from unrelated tables
    By jwreding in forum Queries
    Replies: 7
    Last Post: 08-12-2011, 01:19 PM

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