Reworded title: Calculating the experience an employee accumulates across multiple jobs
P粉212971745
2023-09-04 21:03:55
<p>I have a table called employee experience with columns id, userId, startDate and endDate. </p>
<p>I want to calculate the work experience of employees. Can anyone help provide code for mysql query or JPA specification? </p>
<p>For example, for the following data: </p>
<table class="s-table">
<thead>
<tr>
<th>id</th>
<th>User ID</th>
<th>Start Date</th>
<th>End date</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>2021-01-01</td>
<td>2022-01-01</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2019-01-01</td>
<td>2020-01-01</td>
</tr>
<tr>
<td>3</td>
<td>2</td>
<td>2020-01-02</td>
<td>2021-01-01</td>
</tr>
<tr>
<td>4</td>
<td>3</td>
<td>2021-01-01</td>
<td>2022-01-01</td>
</tr>
</tbody>
</table>
<p>The output should be: </p>
<table class="s-table">
<thead>
<tr>
<th>User ID</th>
<th>Experience</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
</tr>
</tbody>
</table></p>
Successfully accomplished this using the following code: