Share via

Cross Join in a Query?

Anonymous
2012-10-03T21:49:49+00:00

I have data dumps that I am analyzing in Access.  I am creating different tables and am trying to create one joined datasheet that has everything in it.  The problem develops because each row doesn't match up in each table.  Here's a sample of my problem.

Jan Table              Feb Table             Mar Table

ID    Jan Amount    ID    Feb Amount   ID   Mar Amount

1      10                 1    100                1     1000

2      20                 2    200                2     2000

4      40                 3    300                3     3000

If I use inner joins, I get:

JanFebMar Table

ID    Jan Amount   Feb Amount    Mar Amount

1      10                100                 1000

2      20                200                 2000

(Nothing for ID's 3 & 4)

What I want to see is:

JanFebMar Table

ID    Jan Amount   Feb Amount    Mar Amount

1      10                100                 1000

2      20                200                 2000

3      0                  300                 3000

4      40

I think I need a cross join with SQL (an outer join in both directions), but I don't know how to write it.  Can someone help me?  Maybe there is a better way to do this whole thing though.  Thanks in advance for any assistance.

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

8 answers

Sort by: Most helpful
  1. Anonymous
    2012-10-03T23:52:31+00:00

    John Vinson answered your qurstion before you asked the question...

    oops... <looking around to be sure the Time Police aren't watching> <g>

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-10-03T22:50:16+00:00

    John Vinson answered your qurstion before you asked the question...

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2012-10-03T22:38:13+00:00

    I don't know how to get the data in that format.  It is currently in the format as indicated--separate tables.

    Jan Table              Feb Table             Mar Table

    ID    Jan Amount    ID    Feb Amount   ID   Mar Amount

    1      10                 1    100                1     1000

    2      20                 2    200                2     2000

    4      40                 3    300                3     3000

    How would I get it in your below suggested format in Access?  The data keeps getting dumped from somewhere else in the above format?

    ID    Period    Amount

    1     Jan         10

    1     Feb         100

    1     Mar        1000     

    2     Feb         20

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2012-10-03T22:36:02+00:00

    Well, a better way to store the data would be one tall table with fields for ID, DaleDate and Amount. You can get this with a UNION query:

    SELECT ID, #1/1# AS SaleDate, Amount FROM [Jan Table]

    UNION ALL

    SELECT ID, #2/1# AS SaleDate, Amount FROM [Feb Table]

    UNION ALL

    SELECT ID, #3/1# AS SaleDate, Amount FROM [Mar Table]

    UNION ALL

    <etc>

    Base an Append query on this to get the normalized data table.

    You can then easily create a Crosstab query based on this table, using the SaleDate as the column header, the ID as the Row Header, and First or Sum of Amount.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2012-10-03T22:05:40+00:00

    It may be easier for you to import all the data (as posted) into 1 single Table with the structure like

    ID    Period    Amount

    1     Jan         10

    1     Feb         100

    1     Mar        1000     

    2     Feb         20

    ...

    This way, the "Period" data is atored as Field values and not embedded in the Field name or Table names.  In all cases, your database needs to avoid storing data in the Table/Field names since database engines are designed to process data in Field values and not in Table names/Field names.

    Once you have the data in the above structure, a simple Cross-Tab Query will yield what you asked for.

    Was this answer helpful?

    0 comments No comments