Data Comparison Query

AndrewKent

Well-known Member
Joined
Jul 26, 2006
Messages
889
Hi there,

I have a bit of a complex ask here:

I have a table in Access which contains individual records for staff salary as at Week 1, Week 2 ect ect and is stored as such:

Employee_ID Name Salary Week
0123 Andrew 10,000 1
0123 Andrew 10,500 2

...and so on


I need to be able to create a Query that:

1) Contains ALL staff as at Week 1, with a column for what their salary was in Week 1 and a column for what their salary was in Week 2.

Code:
SELECT tblData_Colleagues_Historical.ch_employeeid, tblData_Colleagues_Historical.ch_firstname, tblData_Colleagues_Historical.ch_lastname, tblData_Colleagues_Historical.ch_week, tblData_Colleagues_Historical_1.ch_weekFROM tblData_Colleagues_Historical AS tblData_Colleagues_Historical_1 RIGHT JOIN tblData_Colleagues_Historical ON tblData_Colleagues_Historical_1.ch_employeeid = tblData_Colleagues_Historical.ch_employeeid
WHERE (((tblData_Colleagues_Historical.ch_week)=5) AND ((tblData_Colleagues_Historical_1.ch_week)=9));

The issue is that the staff population is different from one week to the next (as people join/leave) so I want to be able to show ALL staff from Week 1, not just where the join between the two tables is equal. What am I doing wrong?

A
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
You don't need to Join the table to itself, you can do it in a single Aggregate Query.
Here is an example, based on the data table you posted (I was going to try to do it off of your SQL code, but did not see any Salary field referenced in your code).
Code:
SELECT 
    MyTableName.Employee_ID, 
    MyTableName.Name, 
    Sum(IIf([MyTableName]![Week]=1,[MyTableName]![Salary],0)) AS Week_1_Salary, 
    Sum(IIf([MyTableName]![Week]=2,[MyTableName]![Salary],0)) AS Week_2_Salary
FROM 
    MyTableName
GROUP BY 
    MyTableName.Employee_ID, 
    MyTableName.Name
HAVING 
    (Sum(IIf([MyTableName]![Week]=1,[MyTableName]![Salary],0))>0) 
    OR (Sum(IIf([MyTableName]![Week]=2,[MyTableName]![Salary],0))>0);
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,547
Members
449,089
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top