Results 1 to 2 of 2
  1. #1
    sdy007 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    1

    Query Question

    Hello,



    In my problem I have multiple tables by month. In each table, there are 3 columns: ID, date, and number of orders.
    In the SQL code, I have tried to create a query that UNION the ID numbers from all months by doing "SELECT [col1] FROM [Dec] UNION SELECT [col1] FROM [Jan]...etc.

    The problem I have is that I don't know how to have additional columns in the query after doing that. Can anyone help me out?

    Ultimately I want a table/query that has the distinct ID #s in one column and then distinct dates from all months going across the fields, and each cell would contain the number of orders for that ID on that date. So I guess another problem is that there are some IDs not ordering on all dates so how do I handle that?

    Thanks

  2. #2
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742

    General answer

    Need to know the exact form of the tables to give you the exact method. Not sure if ID is a customer ID, a transaction ID, or what. Sounds like you've designed yourself into a box, by creating a new table each month when you don't have a real need for it.

    That being said, first you UNION the tables together to get a massive table. We'll call that query "query1" -
    Code:
    SELECT ID, ordDate, ordCount FROM [Jan]
    UNION
    SELECT ID, ordDate, ordCount FROM [Feb]
    UNION
    SELECT ID, ordDate, ordCount FROM [Mar]
    UNION
    SELECT ID, ordDate, ordCount FROM [Apr]
    (etc)
    Second, you're going to take that query and PIVOT on the date -
    Code:
    TRANSFORM   SUM(ordcount) As OrderCount
    SELECT      ID,
                ordCount
    FROM        query1  
    GROUP BY    ID
    PIVOT       ordDate
    I'm not sure whether PIVOT will choke on the UNION or not - PIVOTs are notoriously finnicky. If it does, then just use your query to create a temporary table and use the table in the PIVOT.

    Any cells where the ID had no ordCount for that date will be empty.

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

Similar Threads

  1. Query question/help
    By 9944pdx in forum Queries
    Replies: 4
    Last Post: 12-12-2017, 08:13 PM
  2. Query question
    By jscriptor09 in forum Queries
    Replies: 2
    Last Post: 06-12-2013, 03:30 PM
  3. query question
    By jscriptor09 in forum Queries
    Replies: 10
    Last Post: 05-01-2013, 09:49 PM
  4. Sub-query question
    By hklein in forum Queries
    Replies: 1
    Last Post: 05-01-2012, 03:02 PM
  5. Question with query
    By Eric Huang in forum Queries
    Replies: 3
    Last Post: 08-01-2011, 05:53 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