Contractor UK Bulletin Board  PayStream

Go Back   Contractor UK Bulletin Board > Contractor UK Forums > Technical
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 30th September 2008, 18:48   #1
Sockpuppet
Super poster
 
Sockpuppet's Avatar
 
Join Date: Oct 2006
Location: Leicester
Posts: 4,286
Default Access Query Help Needed

I have 2 queries I am trying to combine here.

I have 2 tables.

Basically:

Table1:
Date
EmployeeCode
Hours_On_TA_System

Table2:
Date
EmployeeCode
Hours_On_WMS_System


I'm trying to combine the two so that I get a list of:

Date
Employee Code
Hours_On_WMS
Hours_On_TA


The problem with doing a join is that employees may appear in either table1, table2 or both.

With joins I can either show those in table1 matching to those in table 2. Or those in table 2 and the matching records from table 1.

There may be people who are in table 1 and not table 2 but also those that are in table 2 but not 1 at the same time.

Any ideas...I seem to remember something about Union queries but I can't see if this is what I need.

Cheers
Sockpuppet
__________________
We're not just doing this for the money. We're doing this for a sh*t load of money - Spaceballs

Note: You should never take anything I say seriously, especially when said on an internet forum
Sockpuppet is offline   Reply With Quote
Old 30th September 2008, 19:14   #2
TimberWolf
Super poster
 
TimberWolf's Avatar
 
Join Date: Oct 2007
Posts: 2,979
Default

Sounds like UNION ALL is what you're after.
TimberWolf is offline   Reply With Quote
Old 1st October 2008, 08:16   #3
Spacecadet
Super poster
 
Spacecadet's Avatar
 
Join Date: Mar 2006
Location: Jupiter
Posts: 3,105
Default

Like this?

Code:
Date   Employee Code Hours_On_WMS Hours_On_TA
01/01 100001            2.5
01/02 100002                                  8
01/02 100003            6.5
01/02 100004            3                    5.5
In a proper system you need a Full outer join... but Access doesn't support this
Have a google, there are ways of simulating a full outer join
http://bytes.com/forum/thread190756.html

Alternatively... create a numbers/dates table and use 2 left outer joins

Last edited by Spacecadet : 1st October 2008 at 08:19.
Spacecadet is offline   Reply With Quote
Old 1st October 2008, 10:28   #4
PRC1964
Contractor Among Contractors
 
PRC1964's Avatar
 
Join Date: Jul 2005
Location: Behind you
Posts: 1,578
Default

This should do it:

SELECT Table1.Date, Table1.EmployeeCode, Table1.Hours_On_TA_System, 0 AS Hours_On_WMS_System
FROM Table1
UNION
Select Table2.Date, Table2.EmployeeCode, 0 As Hours_On_TA_System, Table2.Hours_On_WMS_System
FROM Table2
__________________
I need a new sig.
PRC1964 is offline   Reply With Quote
Old 1st October 2008, 10:54   #5
Sockpuppet
Super poster
 
Sockpuppet's Avatar
 
Join Date: Oct 2006
Location: Leicester
Posts: 4,286
Default

Cheers guys.

Help much appreciated.
__________________
We're not just doing this for the money. We're doing this for a sh*t load of money - Spaceballs

Note: You should never take anything I say seriously, especially when said on an internet forum
Sockpuppet is offline   Reply With Quote
Old 1st October 2008, 11:55   #6
TimberWolf
Super poster
 
TimberWolf's Avatar
 
Join Date: Oct 2007
Posts: 2,979
Default

Quote:
Originally Posted by PRC1964 View Post
This should do it:

SELECT Table1.Date, Table1.EmployeeCode, Table1.Hours_On_TA_System, 0 AS Hours_On_WMS_System
FROM Table1
UNION
Select Table2.Date, Table2.EmployeeCode, 0 As Hours_On_TA_System, Table2.Hours_On_WMS_System
FROM Table2
How's about summing by EmployeeCode (for all dates - danger!). It's been a while, just clearing away cobwebs...

Code:
SELECT tmptable.EmployeeCode, Sum(tmptable.TA_HOURS) AS SumOfTA_HOURS, Sum(tmptable.WMS_HOURS) AS SumOfWMS_HOURS
FROM [SELECT  Table1.EmployeeCode as EmployeeCode, Table1.Hours_On_TA_System as TA_HOURS, 0 AS WMS_HOURS
      FROM Table1
      UNION ALL
      Select  Table2.EmployeeCode  as EmployeeCode, 0 As TA_HOURS, Table2.Hours_On_WMS_System as WMS_HOURS
      FROM Table2
   ]. AS tmptable
GROUP BY tmptable.EmployeeCode;
TimberWolf is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT. The time now is 20:14.


Advertisers
PayStream

CUK Navigation

Contractor Alliance
Formed a new Ltd Co?

20% off business insurance
£10 off Bauer & Cottrell contract reviews
Find co-workers & client introductions

Increase your value to clients here

Fast Company Formation
Same day online company formation £75 + VAT

Form your Ltd Co Here

Contractor Services


 
Content Relevant URLs by vBSEO 2.4.0 © 2005, Crawlability, Inc.