Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS ACCESS Cross table DATA

Tags:

sql

ms-access

I have two tables, one with employee details looks like this

Employee ID | Employee Name | Start Date | Termination Date |

2234        |  John Smith  | 2014-01-03  |                  |   

3333        | Jane Doe     | 2014-04-22  |  2014-10-31      |

1234        | Bobby Wilson | 2013-12-10  |                  |

My second table has there sales by month-year

looks like so

Employee ID | 2013-12 |   2014-01   | 2014-02  | 2014-03  |  2014-04 | etc | etc 

2234        |         |   199.99    |  130.00  |  300.00  |  230.99  | etc | etc

3333        |         |             |          |          |  204.02  | etc | etc

1234        |  455.99 |   332.32    |  334.00  |  553.00  |  334.99  | etc | etc

So what i need is to make a new Query that helps me show the trend of the employee on how their first month employeed sales number looks like, second month sales, third moneth sales etc. based on when the month one being when the employee started. Over a spand of 12 month or more

So the new table would look as follows

Employee    |  Month 1  |  Month 2  |  Month 3  | Month 4  | etc |  etc |  

2234        |   199.99  |  130.00   |  300.00   |  230.99  | etc |  etc |

3333        |  240.02  |  (month 2) |  (month 3)| (month 4)| etc |  etc |       

1234        |  455.99  |  332.32    |   334.00  |  553.00  | etc |  etc |  

So I know we can join the two tables by Employee ID and I know we can determine an employee's month one based on their start date but how do you write this query out in SQL?

like image 605
Sao Tith Avatar asked May 30 '26 10:05

Sao Tith


1 Answers

As i mentioned in my comment to the question, you need to change the design of your second table as follow (pseudo-code):

CREATE TABLE Sales(
    SaleId Autonumber PK
    [Employee ID] FK (reference to Employees table)
    SalesDate DateTime
    Sales Decimal/Double
);

Then you'll be able to save your data this way:

SaleID  EmpId   SalesDate   Sales
1       2234    2014-01-01  199,99
2       1234    2013-12-01  455.99
3       1234    2014-01-01  332.32
4       2234    2014-02-01  130
5       1234    2014-02-01  334
6       2234    2014-03-01  300
7       1234    2014-03-01  553
8       2234    2014-04-01  230.99
9       3333    2014-04-01  204.02
10      1234    2014-04-01  334.99

Finally, your pivot table might look like:

TRANSFORM Sum(S.Sales) AS SumOfSales
SELECT E.[Employee Id], E.[Employee Name]
FROM Employees AS E INNER JOIN SalesByMY AS S
     ON E.[Employee Id] = S.[Employee Id]
GROUP BY E.[Employee Id], E.[Employee Name]
PIVOT 'Month-' & DateDiff('m',[E].[StartDate],[S].[SalesDate])+1;

The result:

EmpId   EmpName        Month-1  Month-2 Month-3 Month-4 Month-5
1234    Bobby Wilson   455.99   332.32  334     553     334.99
2234    John Smith     199.99   130     300     230.99  
3333    Jane Doe       204.02
like image 62
Maciej Los Avatar answered May 31 '26 22:05

Maciej Los



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!