Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8556061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:15:40+00:00 2026-06-11T15:15:40+00:00

I have a salary table like this: id | person_id | start_date | pay

  • 0

I have a salary table like this:

id | person_id | start_date  | pay
1  | 1234      | 2012-01-01  | 3000
2  | 1234      | 2012-05-01  | 3500
3  | 5678      | 2012-01-01  | 5000
4  | 5678      | 2013-01-01  | 6000
5  | 9101      | 2012-09-01  | 2000
6  | 9101      | 2014-04-01  | 3000
7  | 9101      | 2011-01-01  | 1500
and so on...

Now I want to query the sum of the salaries of a specific month for all persons of a company.

I already have the ids of the persons who worked in the specific month in the specific company, so I can do something like WHERE person_id IN (…)

I have some problems with the salaries query though. The result for e.g. the month 2012-08 should be:

10000

which is 3500+5000+1500.

So I need to find the summed up pay value (for all persons in the IN clause) for the maximum start_date <= the specific month.

I tried various INNER JOINS but it’s been a long day and I can’t think straight at the moment.

Any hint is highly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:15:41+00:00Added an answer on June 11, 2026 at 3:15 pm

    You need to get the active record. This following does this by calculating the max start date before the month in question:

    select sum(s.pay)
    from (select person_id, max(start_date) as maxstartdate
          from salary
          where person_id in ( . . . ) and
                start_date < <first day of month of interest>
          group by person_id
       ) p join
       salary s
       on s.person_id = p.person_id and
          s.maxstartdate = p.start_date
    

    You need to fill in the month and list of ids.

    You can also do this with ranking functions, but you don’t specify which SQL engine you are using.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have something like this var emp = db.Employees.toList(); in my employees table i
i have an employee table with fields like id, name, age and salary. Iam
I have a query like this SELECT [Year], Month, AVG(salary) AS [salary] FROM table1
I have two table Employee and Salary table, salary consists Salary of employee in
I have a table emp with following structure and data: name dept salary -----
I have a table called Employee with the following fields: EmpID Salary Name I
I have a emp table as follows empnum deptno salary ----- ------ ------ 1
I have passed Python list to template like l=['text','there is no salary','I need alcohol','and
I have 2 tables: employee and employeedetails . employee looks like this: id name
I am a beginner of SQL, having this table instructor: ID name dept_name salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.