Mysql Order By month days - mysql

01-09-2016 01,September 2016 12
02-09-2016 02,September 2016 21
03-09-2016 03,September 2016 19
04-09-2016 04,September 2016 15
05-09-2016 05,September 2016 19
23-08-2016 23,August 2016 21
24-08-2016 24,August 2016 22
25-08-2016 25,August 2016 24
26-08-2016 26,August 2016 24
27-08-2016 27,August 2016 18
I want result like below
23-08-2016 23,August 2016 21
24-08-2016 24,August 2016 22
25-08-2016 25,August 2016 24
26-08-2016 26,August 2016 24
27-08-2016 27,August 2016 18
01-09-2016 01,September 2016 12
02-09-2016 02,September 2016 21
03-09-2016 03,September 2016 19
04-09-2016 04,September 2016 15
05-09-2016 05,September 2016 19

You should change the date format. Period.
The proper date format your database supports is chosen not by a whim but for the very reason of making the proper sorting and comparison possible.
Therefore, change your values format to 2016-09-01 and the field type to date. And you will never have not a single problem neither with sorting nor comparison nor any date conversions.

Related

Time Difference between per person between consecutive rows

I have some data which (broadly speaking) consist of following fields:
Person TaskID Start_time End_time
Alpha 1 'Wed, 18 Oct 2017 10:10:03 GMT' 'Wed. 18 Oct 2017 10:10:36 GMT'
Alpha 2 'Wed, 18 Oct 2017 10:11:16 GMT' 'Wed, 18 Oct 2017 10:11:28 GMT'
Beta 1 'Wed, 18 Oct 2017 10:12:03 GMT' 'Wed, 18 Oct 2017 10:12:49 GMT'
Alpha 3 'Wed, 18 Oct 2017 10:12:03 GMT' 'Wed, 18 Oct 2017 10:13:13 GMT'
Gamma 1 'Fri, 27 Oct 2017 22:57:12 GMT' 'Sat, 28 Oct 2017 02:00:54 GMT'
Beta 2 'Wed, 18 Oct 2017 10:13:40 GMT' 'Wed, 18 Oct 2017 10:14:03 GMT'
For this data, my required output is something like:
Person TaskID Time_between_attempts
Alpha 1 NULL ['Wed, 18 Oct 2017 10:10:03 GMT' - NULL]
Alpha 2 0:00:40 ['Wed, 18 Oct 2017 10:11:16 GMT' -'Wed, 18 Oct 2017 10:10:36 GMT']
Beta 1 NULL ['Wed, 18 Oct 2017 10:12:03 GMT' - NULL]
Alpha 3 0:00:35 ['Wed, 18 Oct 2017 10:12:03 GMT' -'Wed, 18 Oct 2017 10:11:28 GMT']
Gamma 1 NULL ['Fri, 27 Oct 2017 22:57:12 GMT' - NULL]
Beta 2 0:00:51 ['Wed, 18 Oct 2017 10:13:40 GMT' -'Wed, 18 Oct 2017 10:12:49 GMT']
My requirements are as below:
a. For a given person (Alpha, Beta or Gamma), the first occurrence of the variable 'time_between_attempts' would be zero/NULL - in the example I have shown it as NULL.
b. The second (and the subsequent) times, the same person appears will have a non NULL or non-zero 'time_between_attempts'. This variable is calculated by taking the difference between the ending time of the previous task and the starting time of the next task.
I have following question in this regard:
How to write a SQL script which can help me achieve the desired output?
Please note that the TaskID is written as integer just for simplification. In the original data, TaskID is complicated and consists of non-continuous strings as:
'q:1392763916495:441',
'q:1392763916495:436'
Any advice on this would be greatly appreciated.
Using self Join() method.
SELECT a.person,
a.taskid,
TIMEDIFF (DATE_FORMAT(STR_TO_DATE(a.Start_time, '%a, %d %b %Y %H:%i:%s'), '%Y-%m-%d %H:%i:%s') ,DATE_FORMAT(STR_TO_DATE(b.End_time, '%a, %d %b %Y %H:%i:%s'), '%Y-%m-%d %H:%i:%s') ) as Time_between_attempts,
a.Start_time,
b.End_time
FROM test a
LEFT JOIN test b
ON a.person = b.person
AND a.taskid = b.taskid + 1
ORDER BY 1, 2;
But this will ignore timezone.
This answers the original version of the question.
You can use lag() and timestampdiff() for the calculation. Assuming your value is a real date/time or timestamp, then you can easily calculate the value in seconds:
select t.*,
timestampdiff(start_time,
lag(end_time) over (partition by person_id order by start_time)
seconds
)
from t;
If the values are stored as string, fix the data! In the meantime, you can use str_to_date() in the function.
To get this as a time value:
select t.*,
(time(0) +
interval timestampdiff(start_time,
lag(end_time) over (partition by person_id order by start_time)
seconds
) second
)
from t;

MySQL select multiple date range

I have a table with 2 dates: startDate and EndDate
I pass to my query 2 dates and I want to check if in this date range the offer are available.
I have an offer that is valid From 1 may 2019 to 30 may 2019.
When I search in offers, I want to show this only if the date range that I pass is included in the date range of the offer.
OFFER From 1 may 2019 to 30 may 2019
Value pass: 01 jan 2019 to 4 jan 2019 NO SHOW
Value pass: 28 april 2019 to 2 may 2019 NO SHOW
Value pass: 10 may 2019 to 12 may 2019 ONLY SHOW IN THIS CASE
Value pass: 29 may 2019 to 2 june 2019 NO SHOW
Value pass: 10 nov 2019 to 12 now 2019 NO SHOW
How can I do this?
This will work for MySQL.
date_from < '2019-10-05' AND date_to > '2019-10-12'

Sql Server Query not getting desire Result

I want show record month wise and date wise. My query is this but I am not getting how I can achieve this.
Query is
select
iWorkItemId
, convert(varchar(20),Progress.dtProgressDate,106) as dtProgressDate
, vsDescription
, WPD.rWeightage
, Progress.ProgressPerc as ProgressPerc
, Convert(decimal(6,2)
, ((WPD.rWeightage * Progress.ProgressPerc)/100)) as TotalProgress
from tblPkgWorkItems WPD
join tblPkgWorkItemsProgress Progress on WPD.iWorkItemId = Progress.iWorkItemsId
where iPackage='7'
group by iWorkItemId, dtProgressDate,vsDescription,rWeightage,ProgressPerc,progressmonth
order by progressmonth ASC,dtProgressDate ASC , iWorkItemId ASC
Column name : (1) progressmonth (Store Month Name)
(2) dtProgressDate (Store complete date)
Example :
In our table we are storing the records like in this manner:
30 Apr 2013
31 Aug 2013
28 Feb 2013
30 Jan 2013
Then I want to show records like this
30 Jan 2014
if some record is inserted in 15 jan 2014 then After 30 jan 2014 then 15 jan 2014 record will show.
28 Feb 2014
30 Apr 2014
30 Aug 2014
Means firstly Jan records will come then Feb record then March records.
Means all records will come month by order then sort by date wise .
My data is being returned like this:
iWorkItemId dtProgressDate progressmonth rWeightage
320 30 Apr 2014 Apr 2
321 20 Apr 2014 Apr 3
320 10 Feb 2014 Feb 4
321 9 Jan 2014 Jan 7
I want record should come first 9 jan 2014 then 10 Feb 2014 (Means Order by Month) then after it 30 April 2014, then 20 april 2014 (Then Sot by Date).
iWorkItemId will repeat win every month.
For my problem is I want to show
Firstly Jan records then Feb records , then March records
And whenever Jan month is coming then All Date which is belong to jan month that will come under JAN month.
Firstly Higher date of JAN month (Means ) 31 Jan 2014, 30 jan 2014, 29 jan 2014, 28 feb 2014, then 27 feb 2014.
Means first records fetch month wise then on particular month that records sort by datewise

MS Access year to date numbers by month

I have a query in an Access database which returns the results below:
MthName 2010
Jan £4.51
Feb £10.20
Mar £17.51
Apr £22.86
May £28.82
Jun £33.30
Jul £37.96
Aug £42.52
Sep £47.88
Oct £54.25
Nov £60.52
Dec £65.80
That is fine but these are year to date numbers and I would like to create a query that could give me the actual month numbers instead.
Taking the above sample the Jan number is clearly £4.51 but the Feb number is
(£10.2-£4.51)= £5.70
I have tried using something like DLOOKUP but it seems that would be really slow and wasn't working correctly.
This can be achieved very easily in Excel but I was hoping to find a query for future use.
Thanks
Avoid using Dlookup as much as you can.
if it is possible to add column [mntNum] with SEQUENCE number of month :
mtnNum MthName 2010
01 Jan £4.51
02 Feb £10.20
03 Mar £17.51
04 Apr £22.86
05 May £28.82
06 Jun £33.30
07 Jul £37.96
08 Aug £42.52
09 Sep £47.88
10 Oct £54.25
11 Nov £60.52
12 Dec £65.80
Query
SELECT table.mntNum, table.mntName, table.[2010], [2010]-nz((SELECT [prev].[2010] FROM [table] as [prev] where [prev].[mntNum]=[table].[mntNum]-1),0) AS JUST_THIS_MONTH
FROM [table];
UPD:
Result of this query
mntNum mntName 2010 JUST_THIS_MONTH
1 Jan 4.51 4.51
2 Feb 10.2 5.69
3 Mar 17.51 7.31
4 apr 22.86 5.35
5 may 28.82 5.96
6 jun 33.3 4.48
7 jul 37.96 4.66
8 Aug 42.52 4.56
9 Sep 47.88 5.36
10 Oct 54.25 6.37
11 Nov 60.52 6.27
12 Dec 65.8 5.27999999999999

How would I filter on yesterday (not last 24 hrs) and show yesterdays date in my headers?

I am creating SQL reports to query results from NetBackup OpsCenter Database (MySQL?)
And am learning how to use SQL, so please forgive my ignorance.
The report I created below, shows the failures for the previous day (as opposed to previous 24 hours, which would be different each time its run).
SELECT (GETDATE()-1) AS 'Date', statusCode AS STATUS, COUNT(*) AS COUNT
FROM domain_JobArchive
WHERE DATEDIFF(day, UtcBigIntToNomTime(endTime), GETDATE()) =1
and masterServerId=59
GROUP BY statusCode;
And I wanted to change this, so it reports the Date being used as a header ... not its own column.
current output:
Count of errors for yesterday (non-PCI)
Date STATUS COUNT
Apr 24, 2013 11:43:10 AM 288 1
Apr 24, 2013 11:43:10 AM 0 6861
Apr 24, 2013 11:43:10 AM 1 52
Apr 24, 2013 11:43:10 AM 6 63
Apr 24, 2013 11:43:10 AM 50 1
Apr 24, 2013 11:43:10 AM 58 2
Apr 24, 2013 11:43:10 AM 191 1
Total 7 Rows , 1 Page(s)
desired output:
Count of errors for Apr 24, 2013 (non-PCI)
STATUS COUNT
0 6861
1 52
6 63
50 1
58 2
191 1
288 1
Total 7 Rows , 1 Page(s)
This way, each time you execute - it pulls up the same exact data.
And it would be even better, if it went from 7am yesterday to 7am today ... regardless of when its executed.
I think this should work. Subtract 7 hours from your date time, then compare it to yesterday's date. You can just remove the date from you SELECT statement. It's not needed.
SELECT statusCode AS STATUS, COUNT(*) AS COUNT
FROM domain_JobArchive
WHERE DATE(DATE_SUB(UtcBigIntToNomTime(endTime), INTERVAL 7 HOUR)) = DATE_SUB(curdate(), INTERVAL 1 DAY)
and masterServerId=59
GROUP BY statusCode;