Oracle SQL - future date/employers age problem - sql

I've got a problem with age of employeers.
In one table I have e_date_of_birth, e_employee_number, e_employee_name, and
in a second table w_employee_since.
I have an inquiry that returns employees with valid contracts for a particular day (e.g. 2016/01/01) and shows the current age of the employee on that day.
I need to add an inquiry that shows employees with valid contracts as of a specific date (e.g. 2016/01/01), and also their age of another date (e.g. 2017/01/01) in the same query results.

Without more details about your tables, particularly wherever you have the contracts information, a generic query to find employee age on a given date would look something like this.
SELECT FLOOR(MONTHS_BETWEEN(DATE '2017-01-01', e_date_of_birth)/12) AS age
FROM DUAL;

Related

SQL Querying a Database Using The Current Date and Date associated with an Attribute

I am trying to query a from scratch database I created. For each job posting in my database, I store the date of which it was posted in the 'date' datatype, which is in the format of "DD-MON-YY". I want to know which job postings haven't been filled after one month after its posted date.
So far I can query my database and find which jobs haven't been filled, due to a particular attribute being null in my table. But I don't know how to add the condition that it has been at least one month.
SELECT JOBID
FROM JOB
WHERE HIRED IS NULL;
For reference I have created this database in Oracle, and am using SQL developer.
I guess to rephrase, how would I make that condition, would I add one month to the date stored and the compare it with today's date?
Compare your date column to the current date with -1 months added to it:
SELECT JOBID
FROM JOB
WHERE HIRED IS NULL
AND date_column <= ADD_MONTHS(SYSDATE, -1);

Calculating whether a date in a range occurs during another date range in SQL

I have two tables in SQL, Contract_Period and Payment_Period, both of which have an Employee Key and start/end dates amongst other columns. The dates are linked to a calendar dimension but simplified just as date fields for the purpose of this. The employee key can occur multiple times on each table, or not at all.
Contract_Period:
Contract_Period_Key (PK), Employee_Key, Contract_Start_Date, Contract_End_Date
Payment_Period:
Payment_Period_Key (PK), Employee_Key, Payment_Period_Start_Date, Payment_Period_End_Date
What I am trying to achieve is to look at the Payment_Period table and establish whether, at any point between the start and end dates, the employee was also under contract. It doesn't have to be for the whole period, there just needs to be at least 1 day which exists in both date ranges.
The output I'd like to achieve is something like:DaysInPeiod
Would really appreciate any help!
TIA.

sql query to find missing records

I am using a transaction table called Student_Details which contain Batch_No, PF_No, Emp_name,DOB, DOR and DOJ along with other details. There is another master table called Batch_Master which contains Batch_No, From_date, To_date and Due_date.
I want to get the details of staff whose due date is within Enter_date1 and Enter_date2 who actually fall due within this period and such of those staff whose due_date is earlier to Enter_date1 but still not come (i.e there is no record for the same person with a DOJ after due_date.)
Please help in designing a query in MS-Access
You should look specifically at SQL WHERE clause. I think this would be most beneficial for what you are trying to achieve. WHERE is used in these types of conditional statements to say if this is true (or false depending on the situation) then return this.
For example:
SELECT *
FROM <location>
WHERE Due_date > Enter_date1 AND Due_date < Enter_date2.
However I am really confused on this statement:
"such of those staff whose due_date is earlier to Enter_date1 but still not come (i.e there is no record for the same person with a DOJ after due_date"
To me this sounds like you just want to return every less than Enter_date2, but you want to ensure that there is only Unique records return... or so I assume.

sql server updating new column based on another column from same table

I have a table with composite primary key based on three columns.
(Unit, Account, service)
I have another two columns: DOB (this holds date of birth) & Age (this is a new int column which needs to be updated based on DOB and the result will be integer value of years)
I know how to retrieve the reslt for Age
select datediff(Year,DOB,GETDATE()) as AGE
But not sure how to update entire table based on the rows DOB data.
Columns are Unit, Account, Service, DOB, Age
As per the comments, it isn't wise to persist Age as it varies daily (and possibly more frequently, if you have users in different timezones).
Also, your Age vs DOB algorithm isn't accurate - see here for a better one
As a consequence, IMO this is one scenario where a non-persisted COMPUTED column makes sense, like so:
ALTER TABLE Person add Age
AS DateDiff(yy,DOB,CURRENT_TIMESTAMP)
- CASE WHEN DATEPART (mm,DOB) <= DATEPART(mm,CURRENT_TIMESTAMP)
and DATEPART(dd, DOB) <= DATEPART(dd,CURRENT_TIMESTAMP)
THEN 0
ELSE 1
END
To answer your question:
UPDATE dob.MyTable SET Age = datediff(Year,DOB,GETDATE());
This will update the entire table as per your requirements.
However, I strongly recommend you look at all the other answers here. Especially the ones about the calculation error in above formula.
Get rid of the age column and calculate the age like so:
SELECT DATEDIFF(yy, DOB, getdate()) AS age FROM daffyduck
There are very rare cases that you need to store items such as age. Since age changes daily, you will have to update your records often. It is instead better to store a fixed value, such as the date of birth that can be calculated. Most DBMS's provide the functionality for doing the date arithmetic for this reason.

Limiting results in a SQL query based on distinctness in an arbitrary column

I need to write a SQL query that pulls from a table that stores records for each time one of our salespeople speaks to a client. The relevant columns are: (1) the salesperson's employee ID, (2) the client's account number, and (2) the date of the conversation.
It's often the case that salespeople have spoken to clients multiple times within the report period (a calendar month) so there will be several entries that are nearly identical except for the date.
Where I'm getting tripped up is that, for the purpose of this query, I need to return only one record per salesperson/client combination, but I can't use DISTINCT because I need to include the date of the most recent conversation within the reporting period.
So, if salesperson John has spoken to client ABC on 10/10, 10/18, and 10/25 I need to pull the 10/25 record but not the others.
It's a Sybase database.
I have the feeling that I may be missing something simple here but I've tried searching and remain stumped. Any help is greatly appreciated.
Thanks for your time,
John
Guessing at the column names...
SELECT employee_id, client_acct_no,
MAX(conversation_date) AS MOST_RECENT_CONV_DATE
FROM mytable
WHERE conversation_date BETWEEN DATE '2010-10-01' AND DATE '2010-10-31'
GROUP BY employee_id, client_acct_no
Documentation for GROUP BY clause.