Results 1 to 6 of 6
  1. #1
    Blackfaer is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2012
    Posts
    3

    Combining 2 query columns into 1 column with data in separate rows

    So I'm pretty advanced with macros and designer fields but very limited on actual VB coding... so would love a solution that doesn't mean I have to go into the VB under the hood...

    I have a database with all the hours employees have logged stored in the database. Our payroll company wants an excel spreadsheet that has very specific info in particular columns and fields on the excel spreadsheet, so I'm trying to design a query which will put the correct info in the correct fields per their system.

    The challenge is, I have currently a query with Employee ID, Overtime Hours, and Regular Hours as separate columns.

    I need to translate this to a query with a single column for hours and a separate column that designates those hours as OT or Reg, with two rows for those employees who have both types.



    Current:

    ID / Regular Hours / OT Hours
    101 / 70 / 7.5
    102 / 30 / 0
    103 / 5 / 0

    Needed:

    ID/ Hours / Type

    101 / 70 / Reg
    101 / 7.5 / OT
    102 / 30 / Reg
    103 / 5 / Reg

    I don't know how to create a query or a formula in a query to break out each employee row into multiple rows with different data in the hours column. It seems like there's something pretty straightforward that I've done in a similar vein but it doesn't seem to work - I can do the opposite and combine those hours by using the SUM function in a query, but I can't seem to break it out this way.

    Any suggestions? Thanks for your help!

    Access 2007, Windows 7.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You could do that with a UNION query:

    SELECT ID, RegularHours, "Reg" As PayType
    FROM TableName
    WHERE RegularHours > 0
    UNION ALL
    SELECT ID, OvertimeHours, "OT" As PayType
    FROM TableName
    WHERE OvertimeHours > 0

    Adjusting for your actual names of course.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Blackfaer is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2012
    Posts
    3
    Just tried that - as I said, I'm amateur with VB coding... got an error: Characters found after end of SQL statement.
    Tried changing a couple things but still same error: here's the code at the moment...

    SELECT [Payroll Export Query 2].ID, [Payroll Export Query 2].Regular
    FROM [Payroll Export Query 2];
    UNION ALL
    SELECT [Payroll Export Query 2].ID, [Payroll Export Query 2].OT
    FROM [Payroll Export Query 2];
    WHERE [Payroll Export Query 2].OT >0

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Get rid of the semi-colons.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Blackfaer is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2012
    Posts
    3
    Duh, that was it. The query builder tool does those semi colons automatically which is where I started... Thanks so much that did what I needed exactly!

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help, and welcome to the site!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. moving column data to rows
    By teasdam in forum Access
    Replies: 3
    Last Post: 12-23-2011, 08:39 AM
  2. Replies: 5
    Last Post: 12-01-2011, 05:38 PM
  3. Showing Columns & Rows in Crosstab query
    By coach32 in forum Queries
    Replies: 6
    Last Post: 09-11-2011, 07:01 PM
  4. combining 3 columns into 1 new column!
    By joebox8 in forum Queries
    Replies: 7
    Last Post: 07-06-2011, 01:46 AM
  5. Combining columns into rows
    By steeveepee33 in forum Queries
    Replies: 5
    Last Post: 04-30-2009, 09:18 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