Increment value depending on months count -SQL that isn't working - sql

Employee annual leave limit per years is 30. Per month 2.5 is added as a annual leave balance based on his joining date.
For example say employee completed 3 months his annual leave balance count should be 7.5.
1st month - 2.5
2nd month - 5
3rd month - 7.5
How to achieve this using sql / .net
Regards,

select (sysdate - job_start_date) / 30 * 2.5 anlBalance from myEmpTable
and to round up the balance you can use ceil and to round down floor
select ceil((sysdate - job_start_date) / 30 * 2.5) anlBalance from myEmpTable
select floor((sysdate - job_start_date) / 30 * 2.5) anlBalance from myEmpTable

Related

Update date column with sysdate based on the interval

I need to create a plsql script that must update the population_date column with sysdate based on the population interval (hourly, daily, weekly, monthly) when executed.
For example, if it is a weekly population interval, the existing date in the population_date column must be updated as follows:
Rec_sid (existing date) Population_date
1 20-jan-2020 sysdate(03-aug-2020)
2 20-jan-2020 sysdate (03-aug-2020)
3 19-jan-2020 sysdate-1(02-aug-2020)
4 19-jan-2020 sysdate-1(02-aug-2020)
5 18-jan-2020 sysdate-2(01-aug-2020)
6 18-jan-2020 sysdate-2(01-aug-2020)
7 17-jan-2020 sysdate-3 (31-jul-2020)
8 17-jan-2020 sysdate-3(31-jul-2020)
9 17-jan-2020 sysdate-3(31-jul-2020)
10 16-jan-2020 sysdate-4(30-jul-2020)
11 16-jan-2020 sysdate-4)30-jul-2020)
12 15-jan-2020 sysdate-5(29-jul-2020)
13 15-jan-2020 sysdate-5(29-jul-2020)
14 14-jan-2020 sysdate-6(28-jul-2020)
And this column exists in 100 tables with different structure and the population_date column alone must be updated with the current date based on the interval.
If I understand correctly, you want the most recent date on the same day of the week on or before today. That logic would be:
population_date + floor( (sysdate - population_date) / 7) * 7
You can put this into an update as:
update t
set population_date = population_date + floor( (sysdate - population_date) / 7) * 7;
I would advise you to run a select first to validate the results.

how to calculate the two days or 3 days total amounts in my sql

how to calculate the 2 days or 3 days total amounts in MySQL.
a sum of 3 days amounts and sum of 2 days amounts
how to write a query in MySql
I don't know how your relation looks like but if I get it right you want to SUM one column over the past 2 or 3 days which will be something like:
SELECT FORMAT(SUM(yourColumnName)
FROM tableName
WHERE entry.date >= CURDATE() - INTERVAL 2 days
For 3 days you have to set INTERVAL 3 days

Price Annualizing in SQL

Hi I am using the following calculation to annualize a price field.
(price * 365 / close _date - begin_date ) * 9.35/100
This calculation is working when leap year is involved
for example when price = 6000
begin date = 1-jan- 2016
close date = 31-dec-2017
so the annualized value is 280.50
But when I change begin dates & end date to 1-jan-2017 and 31-dec-2018, the value changes to 280.88
How do I get the values to 280.50 in both the cases?can any averaging be done so that the value comes perfectly when any date is involved ?
2016 is a Leap Year.. 366 days. While 2017 has 365 days
Your Denom slips from 730 days to 729 days
You may want to use months_between():
select price * 12 / months_between(close _date, begin_date ) * 9.35/100
You may have to adjust the boundaries by one day for it to work for your dates:
select price * 12 / months_between(close _date + 1, begin_date) * 9.35/100
Note: months_between() is a little tricky because it returns fractional months. However, if the difference is always to the first of the month, then you are safe.

Filter date with intervals of 1 hour between two random dates in the same day

The rule
Any records between one hour should be counted as one.
Data
ID DATE
1 06/07/2017 09:20:35
2 06/07/2017 10:20:35
3 06/07/2017 10:25:30
4 06/07/2017 10:40:35
5 06/07/2017 10:50:35
6 06/07/2017 11:25:30
7 06/07/2017 11:50:20
8 06/07/2017 15:25:30
9 06/07/2017 17:25:30
10 06/07/2017 17:30:30
11 06/07/2017 17:40:55
Expected result
count date
5 06/07/2017
Why? Based on the minimum date, the records between one hour after are counted as one. Something like this:
count range_date
1 09:20:35 - 10:20:35
1 10:20:36 - 11:20:36
1 11:20:37 - 12:20:37
0 12:20:38 - 13:20:38
0 13:20:39 - 14:20:39
0 14:20:40 - 15:20:40
1 15:20:41 - 16:20:41
1 17:20:42 - 18:20:42
Any suggestions to do so?
Something in one statment since I don't have a rule for the dates (min/max). I just know that all dates it's on the same day.
And I dont want to make N selects between every hour...
If I correctly understood your problem, this is a query that obtain the result you're looking for:
SELECT
TRUNC(dt) AS day,
COUNT(DISTINCT TRUNC(dt - 20 / (24 * 60) - (35 + TO_NUMBER(TO_CHAR(dt, 'HH24')) - 9) / (24 * 60 * 60), 'HH24')) AS hours
FROM yourtable
GROUP BY TRUNC(dt)
TRUNC(dt, 'HH24') truncates the date to hour (minutes and seconds
are set to 0)
I subtract the minutes and seconds of starting hour to
"shift" the TRUNC to correct time period
TO_NUMBER(TO_CHAR(dt, 'HH24')) - 9) is mandatory to add one second for every hour
With COUNT DISTINCT you
count the number of different hours.
If the initial hour is variable (as I suppose) I think the easiest way is to keep it with separate query, extract hour, minute and second and use them as variables in input to the main query
You want to count the number of hours since the earliest date/time for each record. Here is one approach:
select trunc(dt), count(distinct trunc((dt - min_dt) / 24)) as num_hours
from (select t.*, min(dt) over (partition by trunc(dt) order by dt) as min_dt
from t
) t
group by trunc(dt);
This expression (dt - min_dt) / 24 calculates the time between each date/time and the earlier in hours. the count(distinct) counts the number of distinct hours seen in the date.

How to convert and display an Integer into YYYYMMDD format using Select query without neglecting days and leap year logic?

I have a query to retrieve a set of non null records from a column x consisting of DATE format.
If count(x) = 35 then i need to display the value as 1 Month & 5 days
If 369 days then 1 year & 4 days or If 400 days then 1 year 1 month 5 days respectively
Query: In the above instance,unfortunately i am neglecting 0.25 days but How to tweak my actual requirement in such a way that i don't end up neglecting days and handle leap year logic too
How to solve this issue?
it is not clear if you need of a time's generic computing in years, months and days, based on averages of number of days of a month, etc.. or if you want an exact compute of the number of the months that pass starting froma adate.. for example if your total sum of 400 days start from 15 march, then the month is counted by 1 after, let's say, 15 days, and so the remaining days are 20.. I don't know if I explained..
In the first hypotesis, you may use the following pseudo-coded solution, that is a very approximative method..
however, if you know a start date, it is possible to compute exactly how many "bissextile" days are comprehended between your interval of days starting from your start date
let's say to have output variables Years, Months, Days .. and an input totalDaysdays assigned with your data retrieved from the db, then:
(pseudocode)
Years = trunc((totalDays / 365)
bissextileDays = trunc((totalDays / 365) / 4)
numDaysOffset = (totalDays Mod 365) + bissextileDays
Months = trunc(numDaysOffset / 30)
Days = numDaysOffset Mod 30
Actually i found something that will suit my requirement.
https://community.oracle.com/thread/2587161?start=0&tstart=0
select days,
floor(days / 365.25) years,
floor(mod(days,365.25) / (365.25 / 12)) months,
round(mod(days,365.25 / 12)) days
from periods
So this can produce expected output when number is given. This produces output as years,months and remaining days