Grouping the date columns in week range - mysql

i have a table and the columns like
Start_date timestamp,
end_date timestamp,
id number,
cost number(10,2).
And the data which i inserted into the table 'll be like this
1,'2013-02-03 00:00:00','2013-02-03 00:00:00',75*0.06
1,'2013-02-04 00:00:00','2013-02-04 00:00:00',75*0.06
1,'2013-02-05 00:00:00','2013-02-05 00:00:00',75*0.06
1,'2013-02-06 00:00:00','2013-02-06 00:00:00',75*0.06
1,'2013-02-07 00:00:00','2013-02-07 00:00:00',75*0.06
1,'2013-02-08 00:00:00','2013-02-08 00:00:00',75*0.06
1,'2013-02-09 00:00:00','2013-02-09 00:00:00',75*0.06
and now i want to group the columns Start_date and end_date by Sunday to Saturday.
can you please help me on this.
Thanks in Advance.

This is the best I can offer you as your question is impossible to understand as well as that sample you provided. ISO week table for 2013:
SELECT start_date -- 1/1/2013 --
, TRUNC(start_date, 'iw') wk_starts
, TRUNC(start_date, 'iw') + 7 - 1/86400 wk_ends
, TO_NUMBER (TO_CHAR (start_date, 'IW')) ISO_wk#_iw
, TO_CHAR(start_date, 'DAY') wk_day
FROM
(
SELECT TRUNC(SYSDATE, 'YEAR')-1 + LEVEL AS start_date
FROM dual
CONNECT BY LEVEL <=
(
SELECT TRUNC(ADD_MONTHS (SYSDATE, 12), 'Y')-TRUNC(SYSDATE, 'Y') "Num of Days in 2013"
FROM dual
)
)
/
START_DATE WK_STARTS WK_ENDS ISO_WK# WK_DAY
----------------------------------------------------------------------
1/1/2013 12/31/2012 1/6/2013 11:59:59 PM 1 TUESDAY
1/2/2013 12/31/2012 1/6/2013 11:59:59 PM 1 WEDNESDAY
.....
1/7/2013 1/7/2013 1/13/2013 11:59:59 PM 2 MONDAY
1/8/2013 1/7/2013 1/13/2013 11:59:59 PM 2 TUESDAY
.....
You can add any other formats for your date and order by it I guess...

Related

Get running totals for each month based on created_at date

I'm trying to get a running total for each month of this year. So my ideal result would be something such as:
January | 4
February | 5
March | 8
April | 10
May | 12
June | 14
July | 16
August | 17
September | 18
October | 21
November | 22
December | 22
The basic count would just check against first of the month such as:
January 1 (sum where created_at < January 1, 2018)
February 1 (sum where created_at < February 1, 2018)
March 1 (sum where created_at < March 1, 2018)
April 1 (sum where created_at < April 1, 2018)
...
Doing it for one month at a time is easy, as I can just do something like this:
SELECT *
FROM businesses
WHERE created_at < CONCAT(YEAR(CURDATE()), "-12-01 00:00:01")
I tried using one of the examples from another Stackoverflow answer, but it's not working quite as desired as it seems like the sort or something is messed up so the counts are not lining up right.
You can see schema build and current SQL here:
http://sqlfiddle.com/#!9/0c23cc/20
Try something like this.
SELECT
MONTHNAME( created_at ) AS theMonth,
(
SELECT
count( * )
FROM
businesses AS u1
WHERE
YEAR ( u1.created_at ) = YEAR ( businesses.created_at )
AND u1.created_at <= date_ADD( last_day( businesses.created_at ), INTERVAL 1 DAY )
) AS totals
FROM
businesses
WHERE
YEAR ( businesses.created_at ) = YEAR ( CURDATE( ) )
GROUP BY
MONTHNAME( businesses.created_at )
ORDER BY
MONTH ( businesses.created_at ) ASC

SQL: Query periods of time given date

I have a list of periods during a year, and they are the same every year. You can think of it as a Season. They have a startDate and a endDate.
Because there can be Seasons that leap each other, what I need to to is query all the matching Seasons given a date, no matter what year.
As an example:
Season1: from 1st of January to 10th of January
Season2: from 6th of January to 8th of January
Season3: from 11th of January to 20th of January
Given the date 7th of January, I'd need to retrieve the Season1 and Season2.
I've tried converting all dates to the same year, but It doesn't work when the Start Date of a season in "later" than the End Date (for example, there's a period starting on November and ending of February).
Thanks in advance for the help.
Edit, sample data:
StartDate EndDate SeasonId
2000-08-01 2000-08-31 4
2000-12-29 2000-01-02 3
2000-06-01 2000-07-30 3
2000-09-01 2000-09-30 3
2000-01-06 2000-01-08 3
2000-04-07 2000-04-17 3
2000-04-28 2000-05-01 3
2000-06-02 2000-06-05 3
2000-06-23 2000-06-25 3
2000-09-08 2000-09-11 3
2000-09-22 2000-09-25 3
2000-10-12 2000-10-15 3
2000-11-01 2000-11-05 3
2000-12-01 2000-12-10 3
2000-12-22 2000-12-26 3
2000-03-01 2000-05-31 2
2000-10-01 2000-10-31 2
2000-11-01 2000-02-28 1
And I'd need, for example, the season for the date 2000-02-08, and retrieve seasonId = 1, or the date 2000-10-13and retrive seasonId = 3, seasonId = 2
I would do it in 2 'options': (the following SQL assumes you already got rid of the year in the table, and left only month-date format. )
select ... from seasons s where
(s.startDate <= s.endDate and s.startDate <= #mydate and s.endDate >= #mydate) or
(s.startDate > s.endDate and s.startDate >= #mydate and s.endDate <= #mydate)
You could query like this for the Season1:
select * from myTable where (month(myDate) = 1 and DAY(myDate) between 1 and 10)
If you have a season in more than one month, like start date January 20th, and finish date Febrery 10th, you could query this way:
select * from myTable where (month(myDate) = 1 and DAY(myDate) >= 20) or (month(myDate) = 2 and DAY(myDate) <= 10)
UPDATED WITH YOUR UPDATE
It is a little bit tricky, but it should work...
select * from seasons_table
where cast(cast(day(myDate) as char) + '/' + cast(month(myDate) as char) + '/' + '2000' as date) between
cast(cast(day(StartDate) as char) + '/' + cast(month(StartDate) as char) + '/' + '2000' as date) and
cast(cast(day(EndDate) as char) + '/' + cast(month(EndDate) as char) + '/' + '2000' as date)
given tblSeason with columns Id, startdate, enddate and your date as #myDate you would query as
Select Id From tblSeason WHERE #myDate BETWEEN startdate AND enddate
would give list of Id's of the seasons that match.
if you can't work from that, please give more information in your examples as to the structure you are querying and the expected outcome.
*Edit to ignore the year part you could do similar to
Declare #myDate datetime = '2016-10-13'
SELECT [StartDate]
,[EndDate]
,[SeasonId]
FROM [dbo].[Table_1]
where DATEPART(dy, #myDate) >= DATEPART(dy,StartDate)
AND (DATEPART(dy,#myDate) =< DATEPART(dy,EndDate) OR DATEPART(dy,StartDate) > DATEPART(dy,EndDate))
Why are you including the year in the table? That seems strange.
In any case, you only care about the MM-DD format, so use date_format() to convert the values to strings:
select t.*
from t
where (start_date <= end_date and
date_format(#date, '%m-%d') >= date_format(start_date, '%m-%d') and
date_format(#date, '%m-%d') <= date_format(end_date, '%m-%d')
) or
(start_date > end_date and
date_format(#date, '%m-%d') <= date_format(start_date, '%m-%d') and
date_format(#date, '%m-%d') >= date_format(end_date, '%m-%d')
);
The strings are fine for comparison, because you are only looking at the month and day components of the date.
Given the nature of your problem, I would recommend that you store start_date and end_date in a non-date format, such as MM-DD.

order by date starting from current month in MYSQL

I have a date type column in mysql table. I need to order the date starting from the current month independent of year.
tblEvent
event_date
__________
2016-01-07
2016-01-13
2016-05-16
2016-05-22
2016-05-30
2018-06-21
2016-06-23
2016-07-14
2018-08-16
2016-10-20
1990-12-09
2016-12-15
As the current month is Jun therefore, the required Output should be:
event_date
__________
2018-06-21
2016-06-23
2016-07-14
2018-08-16
2016-10-20
1990-12-09
2016-12-15
2016-01-07
2016-01-13
2016-05-16
2016-05-22
2016-05-30
Order must start from the current month
month
for example if the current month is Nov then the order must be
Nov, Dec, Jan, Feb, Mar, ....., Oct
also the respective dates must be in asc order.
order must be independent of year(i.e. no matter what year is there
in the date)
SELECT event_date
FROM tblEvent
ORDER BY
IF(MONTH(event_date) < MONTH(NOW()), MONTH(event_date) + 12, MONTH(event_date)),
DAY(event_date)
Info:
Date and Time Functions
Control Flow Functions
If I understand your question correctly, a soultion can be like this:
select
event_date
from
tblevent
order by
(12+month(event_date)-month(current_date)) mod 12,
day(event_date)
this query will sort all rows starting from the current month, ordered by month and day not considering the year. Please see a working fiddle here.
I just try below query, Getting all records, starts from current month in desc order. like:- month 6, 5, 4, 3, 2, 1
SELECT *
FROM `schedules`
WHERE date_format( `ScheduleDate` , "%m" ) <= date_format( NOW( ) , "%m" )
ORDER BY `ScheduleDate` DESC
LIMIT 0 , 30;
You can try in this way, Hope this will help.

Finding month difference and then remaining days after months - mysql

I need to find the difference between two dates to do some calculations based on the result.
Let's say column start_date is having value 1/Jan/2014 and column and end_date is having value 15/Mar/2014. The result I want is following format:
months | days_remain |
----------------------
2 15
I can find MONTH difference and also DAY difference between separately (as 2 Months & 74 days) using TIMESTAMPDIFF function. But how to find out the remaining 15 days ?
You can use DATEDIFF to see the difference between 2 dates
SELECT DATEDIFF('2006-04-01','2005-04-01');
http://lists.mysql.com/mysql/196414
Try This, I hope it will Work For You.
select DateDiff(d, datepart(month,[Start_Date]),datepart(month,End_Date)) as Months,
(30-day(end_date)) as Days_remain
from Sdate
This will work for sure.
Declare #StartDate datetime
Declare #EndDate datetime
Declare #years varchar(40)
Declare #months varchar(30)
Declare #days varchar(30)
set #StartDate ='2014/01/01'
set #EndDate = '2014/03/15'
select #years=datediff(year,#StartDate,#EndDate)
select #months=datediff(month,#StartDate,#EndDate)-(datediff(year,#StartDate,#EndDate)*12)
select #days=datepart(d,#EndDate)-datepart(d,#StartDate)
select #years +' years, ' +#months +' months, '+#days +' days' asYearMonthDay
I think this might be what you want. It does however return 14 days for remaining, but as Jaugar Chang pointed out in a comment that should be correct if as the difference between March 1st and March 15th is 14 days.
select
timestampdiff(
month,
start_date,
end_date
) as months,
datediff(
end_date,
timestampadd(
month,
timestampdiff(
month,
start_date,
end_date
)
,start_date
)
) as days_remain
from test;
Sample SQL Fiddle
Sample result:
| START_DATE | END_DATE | MONTHS | DAYS_REMAIN |
|------------------|----------------|--------|-------------|
| January, 01 2014 | March, 15 2014 | 2 | 14 |
| January, 10 2014 | March, 13 2014 | 2 | 3 |
Try this. See the sample data as well:
select
dt1, dt2,
trunc( months_between(dt2,dt1) ) mths,
dt2 - add_months( dt1, trunc(months_between(dt2,dt1)) ) days
from
(
select date '2012-01-01' dt1, date '2012-03-25' dt2 from dual union all
select date '2012-01-01' dt1, date '2013-01-01' dt2 from dual union all
select date '2012-01-01' dt1, date '2012-01-01' dt2 from dual union all
select date '2012-02-28' dt1, date '2012-03-01' dt2 from dual union all
select date '2013-02-28' dt1, date '2013-03-01' dt2 from dual union all
select date '2013-02-28' dt1, date '2013-04-01' dt2 from dual
) sample_data;
Hope it helps.

TSql Query for Time Range

I have a simple table with bunch of applicants, where they have their Start Time, End Time and Date data. I want to figure out who is available for a specific date within a Time Range.
Table below shows where Start and End Date Columns tells us they are booked for that Date/Time .........
AppID StartTime EndTime Date
H12 8:00 13:00 12/1/2013
H12 14:00 16:00 12/1/2013
H12 19:00 21:00 12/1/2013
H14 17:00 18:00 12/1/2013
H13 14:00 16:00 12/1/2013
H13 11:00 15:00 12/2/2013
H15 8:00 13:00 12/2/2013
So in the Table above how can I write a Query that will say...Give me all the applications for 12/1/2013 which are NOT working between 17:00 - 18:00? So basically it should return H12 & H13 (Because its time slot for 17-18 pm is not available within table for 12/1/2013).
This query returns the apps ID which were not working at a time range on a specific date.
SELECT
[AppID] = FreeApps
FROM table_name t1
WHERE AppID NOT IN (-- Not in the set of apps that were busy at the time range
SELECT AppID
FROM table_name t2
WHERE ((StartTime >= '17:00' AND StartTime <= '18:00')
OR (EndTime >= '17:00' AND EndTime <= '18:00'))
AND Date = '12/1/2013'
AND t1.AppID=t2.AppID)
GROUP BY AppID
;WITH t1 as
(
SELECT DISTINCT AppID
FROM <table>
WHERE date = '20130112'
)
SELECT AppID
FROM t1
WHERE
NOT EXISTS
(SELECT * FROM <table> t2
WHERE t2.STARTTIME < '18:00'
AND t2.ENDTIME > '17:00'
AND t1.AppID = t2.AppID
AND t2.date = '20130112'
)