search

Home  >  Q&A  >  body text

Reworded title: Calculating the experience an employee accumulates across multiple jobs

<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>
P粉212971745P粉212971745509 days ago515

reply all(1)I'll reply

  • P粉486138196

    P粉4861381962023-09-05 15:24:01

    Successfully accomplished this using the following code:

    SELECT SUM(TIMESTAMPDIFF(YEAR, START_DATE, END_DATE)) AS experience,
              SOCIAL_PROFILE_ID
       FROM tableName
       GROUP BY SOCIAL_PROFILE_ID
    

    reply
    0
  • Cancelreply