map sql column to date based on an end date - mysql

I have a pay period sybase table containing column names day01 - day14. I also have a column containing the pay period end date. Management is requesting the hours used for all the Mondays in the month of July.
How can I walk the column names mapping them to dates so I can get all Mondays in the month of July using sybase?
data example
Time_Sheet_Hours
day01 | day02 | day03 ... day14 | end_date
7.5 | 7.5 | 0 ... | 2017-07-05
day01 | day02 | day03 ... day14 | end_date
7.5 | 7.5 | 7.5 ... | 2017-07-19
day01 | day02 | day03 ... day14 | end_date
7.5 | 7.5 | 7.5 ... | 2017-08-02
Can you somehow do a nested query where the from could be dynamic based on the column sequence and mapped to a date based on the end_date?

You can use datepart in Sybase, or dayofweek in mysql, to determine the numeric day of the week (Sunday is 1) that end_date falls on. Then you can determine how many more days until "day 2" (Monday - not your "day02" column). Then either use a case statement, or execute a dynamic query, in order to sum up the correct day.
Here is an example in Sybase. It assumes that day14 is also the end_date.
Therefore, if end_date is a Sunday, then that means day14 was also a Sunday, and day01 and day08 represent Mondays.
select
sum
(
case
when (datepart(dw, end_date) = 1) then day01 + day08 -- Ends on Sun
when (datepart(dw, end_date) = 7) then day02 + day09 -- Ends on Sat
when (datepart(dw, end_date) = 6) then day03 + day10 -- Ends on Fri
when (datepart(dw, end_date) = 5) then day04 + day11 -- Ends on Thu
when (datepart(dw, end_date) = 4) then day05 + day12 -- Ends on Wed
when (datepart(dw, end_date) = 3) then day06 + day13 -- Ends on Tue
when (datepart(dw, end_date) = 2) then day07 + day14 -- Ends on Mon
else 0
end
)
from my_table

Bit of brute case when should do it:
SELECT
CASE WHEN dayofweek(end_date - INTERVAL 13 day) = 2 THEN day01 END +
CASE WHEN dayofweek(end_date - INTERVAL 12 day) = 2 THEN day02 END +
... -- and so on. Writing this out on an iPad is a pain
CASE WHEN dayofweek(end_date - INTERVAL 1 day) = 2 THEN day13 END +
CASE WHEN dayofweek(end_date - INTERVAL 0 day) = 2 THEN day14 END
as sum_of_mondays
FROM time sheet
WHERE end_date BETWEEN x AND y
Do please examine this for real with your data; check that the dayofweek() calc on the adjusted date really does correctly identify the Mondays- I've no MySQL instance to try this on but the docs say that dayofweek returns 2 for a Monday, and I've assumed that end_date and day14 are the same day hence subrptracting 0 days from it. If your end_date is midnight after the day upon which day14's hours are worked, then you'll need to adjust the subtracts by one (I.e. Subs should start by subtracting 14, and finish by subtracting 1)

Although this doesn't specifically answer my original question, I'd like to share our end solution. The end solution was to just build a view to query against which contained normalized data.

Related

Dynamic due date finder in a single query

id start_date interval period
1 2018-01-22 2 month
2 2018-02-25 3 week
3 2017-11-24 3 day
4 2017-07-22 1 year
5 2018-02-25 2 week
the above is my table data sample. start_dates will be expired based on interval and period(i.e id-1 will have due date after 2 months from the start_date, id-2 will have due after 3 weeks vice versa). period is enum of (day,week,month,year). requirement is, Client can give any period of dates. let's say 25-06-2026 to 13-07-2026 like that.. I have to return the ids whose due dates falls under that period.I hope i made my question clear.
I am using mysql 5.7. I found a way to achieve this with recursive CTE's.(not available in mysql 5.7). and there is a way to achieve this by populating virtual records by using inline sub queries along with unions but its a performance killer and we can't do populate virtual records every time a client request comes.(like given in the link Generating a series of dates) I have reached a point to get results for a single date which is very easy. Below is my query.
SELECT b.*
FROM (SELECT a.*,
CASE
WHEN period = 'week' THEN MOD(Datediff('2018-07-22', start_date), 7 * intervals)
WHEN period = 'month'
AND Day('2018-07-22') = Day(start_date)
AND MOD(Period_diff(201807, Extract(YEAR_MONTH FROM start_date)), intervals) = 0 THEN 0
WHEN period = 'year'
AND Day('2018-07-22') = Day(start_date)
AND MOD(Period_diff(201807, Extract(
YEAR_MONTH FROM start_date)) / 12,
intervals) = 0 THEN 0
WHEN period = 'day' THEN MOD(Datediff('2018-07-22', start_date) , intervals)
end filters
FROM kml_subs a)b
WHERE b.filters = 0;
But I need to do this for a period of dates not a single date. Any suggestions or solutions will be much appreciated.
My desired result shoud be like..
if i give two dates.say 2030-05-21 & 2030-05-27. due dates falls under those 6 dates between(2030-05-21 & 2030-05-27) will be shown in the result.
id
1
4
My question is different from Using DATE_ADD with a Column Name as the Interval Value . I am expecting a dynamic way to check due dates based on start_date
Thanks, Kannan
In MySQL, it would seem that a query along these lines would suffice. (Almost) everything else could and should be handled in application level code...
SELECT *
, CASE my_period WHEN 'day' THEN start_date + INTERVAL my_interval DAY
WHEN 'week' THEN start_date + INTERVAL my_interval WEEK
WHEN 'month' THEN start_date + INTERVAL my_interval MONTH
WHEN 'year' THEN start_date + INTERVAL my_interval YEAR
END due_date
FROM my_table;

Dynamic due dates checking with the given period of dates

id start_date interval period
1 1/22/2018 2 month
2 2/25/2018 3 week
3 11/24/2017 3 day
4 7/22/2017 1 year
5 2/25/2018 2 week
the above is my table data sample. start_dates will be expired based on interval and period(i.e id-1 will have due date after 2 months from the start_date, id-2 will have due after 3 weeks vice versa). period is enum of (day,week,month,year). Client can give any period of dates. let's say 25-06-2026 to 13-07-2026 like that.. I have to return the ids whose due dates falls under that period.I hope i made my question clear.
Here what i have done to resolve this. I am using mysql 5.7. I found ways to achieve this with recursive CTE's.(not available in mysql 5.7). and there is a way to achieve this by populating virtual records by using inline sub queries along with unions and its a performance killer and there is restriction of population of records.(like given in the link Generating a series of dates) I have reached a point to get results for a single date which is very easy. Below is my query(in oracle)
select id
from (select a.*,
case
when period='week'
then mod((to_date('22-07-2018','dd-mm-yyyy')-start_date),7*interval)
when period='month' and to_char(to_date('22-07-2018','dd-mm-yyyy'),'dd')=to_char(start_date,'dd')
and mod(months_between(to_date('22-07-2018','dd-mm-yyyy'),start_date),interval)=0
then 0
when period='year' and to_char(to_date('22-07-2018','dd-mm-yyyy'),'dd-mm')=to_char(start_date,'dd-mm')
and mod(months_between(to_date('22-07-2018','dd-mm-yyyy'),start_date)/12,interval)=0
then 0
when period='day'
and mod((to_date('22-07-2018','dd-mm-yyyy')-start_date),interval)=0
then 0 else 1 end filter from kml_subs a)
where filter=0;
But I need to do this for a period of dates not a single date. Any suggestions or solutions will be much appreciated.
Thanks,
Kannan
Assuming this is an Oracle question and not MySQL:
I think the first thing that you need to do is calculate when the due date is. I think a simple case statement can handle that for you:
case when period = 'day' then start_date + numtodsinterval(interval,period)
when period = 'week' then start_date + numtodsinterval(interval*7,'day')
when period = 'month' then add_months(start_date,interval)
when period = 'year' then add_months(start_date,interval*12)
end due_date
Then, using that new due_date field, you can check if the due date falls between the desired date range.
select *
from(
select id,
start_date,
interval,
period,
case when period = 'day' then start_date + numtodsinterval(interval,period)
when period = 'week' then start_date + numtodsinterval(interval*7,'day')
when period = 'month' then add_months(start_date,interval)
when period = 'year' then add_months(start_date,interval*12)
else null end due_date
from data)
where due_date between date '2018-02-25' and date '2018-03-12'
The above query checking between 2/25/18 and 3/12/18 produces the following output using your data:
+----+-------------+----------+--------+-------------+
| id | start_date | interval | period | due_date |
+----+-------------+----------+--------+-------------+
| 2 | 05-FEB-2018 | 3 | week | 26-FEB-2018 |
| 5 | 25-FEB-2018 | 2 | week | 11-MAR-2018 |
+----+-------------+----------+--------+-------------+

SQL - Last Day of Month

I got a Table which looks like this:
DATE | Number
01-01-16 00:00:00 10
02-01-16 00:00:00 10
03-01-16 00:00:00 11
04-01-16 00:00:00 12
05-01-16 00:00:00 13
....
31-01-16 00:00.00 15
........
29-02-16 00:00:00 18
I got this table for the last few months.
I now want to retrieve the value of the rows, which contain the last day of the previous month and the month before the last month. So for today I would like to retrieve the Value of the 31-1-16 and 29-2-16.
My result should look like:
lastmonth | lastmonth2
18-> Corresponding value to Date: 29-02-16 | 15 -> value for 31-01-16
Would appreciate any help.
Cheers
Here is logic for the last day of this month and the previous month:
select last_day(curdate()) as last_day_of_this_month,
last_day(date_sub(curdate(), interval 1 month)) as last_day_of_prev_month
You can get the last day of any month relative to the current month by changing the "1".
And, I have no idea what date "30-2-16". When describing dates, you should use ISO standard formats. The last day of February 2016 was 2016-02-29.
This is Gordon's code for determining the correct dates plus subqueries to fetch the Number values for those rows:
SELECT
(SELECT Number FROM cc_open_csi_view
WHERE last_day(date_sub(curdate(), interval 1 month)) = date(`DATE`)) as lastmonth,
(SELECT Number FROM cc_open_csi_view
WHERE last_day(date_sub(curdate(), interval 2 month)) = date(`DATE`)) as lastmonth2
FROM DUAL;
Hope that's what you wanted! Works for me in a simple example. I don't know if you need the date() part around DATE but it seemed safest.
SELECT CASE
WHEN last_day(curdate()) = `DATE` THEN number
END as number_last_month,
CASE
WHEN last_day(date_sub(curdate(), interval 1 month)) = `DATE`
THEN number
END as number_last_month2
FROM cc_open_csi_view
I can't test it right now on sqlfiddle.

Generate Monthly XML from a table containing date range

I have a table as -
test_table(booking_id, booking_description, start_date, end_date)
Sample Data -
1 | Some booking | 06/30/2013 | 08/01/2013
2 | Some new one | 08/05/2013 | 09/01/2013
3 | Some new two | 09/03/2013 | 09/05/2013
Now I want to generate a monthly xml file from using some java code (No problem in it, I would write), I would be passing the month and year (basically start and end date of the month) to mysql query and I want some table as -
month = 7, year 2013
1 | Some booking | 07/01/2013
1 | Some booking | 07/02/2013
...
Month = 9, year = 2013
2 | Some new one | 09/01/2013
| | 09/02/2013
3 | Some new two | 09/03/2013
...
I was looking to use a java loop from start date to end date and query mysql to find out whether this date comes in the date range or not, if it comes I would add the details else I would add blanks. But that is going to be horrible approach (will go for 30 times mysql look ups) and I am considering it as last option.
Is there any other way around with one or two mysql query and get the data in the format.
EDIT:
month = 7, year = 2013
Select *
from booking_details
where month(start_date) <= 7 and year(start_date) <= 2013 and
month(end_date) >= 7 and year(end_date) >= 2013
I developed this query but still not sure would it over all the possible scenarios.
Based on my understanding of the question you want something like this:
declare #date datetime
Select booking_id, booking_description, start_date --you don't indicate which date field you want in the results
from test_table
where (start_date between #date and date_add(#date, INTERVAL 1 MONTH))
or (end_date between #date and date_add(#date, INTERVAL 1 MONTH))
SQL is probably not exact, I know TSQL not MySQL but this should be close.

Mysql date less than week operation

Suppose you have two datetime values, date_a and date_b
What expression evaluates to true if date_a occurs within or less than the calendar week of date_b?
Just use MySQL's week() function.
Example:
mysql> create table daten (a date, b date);
mysql> insert into daten values ('2011-04-14', '2011-04-12');
mysql> insert into daten values ('2011-04-14', '2011-04-22');
mysql> select * from daten where (week(a)=week(b) and year(a)=year(b)) or a<b;
+------------+------------+
| a | b |
+------------+------------+
| 2011-04-14 | 2011-04-12 |
+------------+------------+
This will give you all records with a in the same or an earlier calendar week than b. Note that week(a)<=week(b) would not work, because weeks start again each year.
Note: There is more than one convention for counting calendar weeks (week starts on Sunday or on Monday, different start of first week of the year). You can pass an optional second parameter "mode" to week() to tell it which convention to use; see the docs. Of course for this problem only the start day of the week matters, not what the first week of the year is.
A more brute force method:
Select ...
From DateValues
Where date_a >= Date_Add( date_b, Interval -DayOfWeek( date_b ) + 1 Day )
And date_a <= Date_Add( date_b, Interval -DayOfWeek( date_b ) + 7 Day )