date-arithmetic

Daily average for the month (needs number of days in month)

回眸只為那壹抹淺笑 提交于 2019-12-06 02:22:52
I have a table as follow: CREATE TABLE counts ( T TIMESTAMP NOT NULL, C INTEGER NOT NULL ); I create the following views from it: CREATE VIEW micounts AS SELECT DATE_TRUNC('minute',t) AS t,SUM(c) AS c FROM counts GROUP BY 1; CREATE VIEW hrcounts AS SELECT DATE_TRUNC('hour',t) AS t,SUM(c) AS c,SUM(c)/60 AS a FROM micounts GROUP BY 1; CREATE VIEW dycounts AS SELECT DATE_TRUNC('day',t) AS t,SUM(c) AS c,SUM(c)/24 AS a FROM hrcounts GROUP BY 1; The problem now comes in when I want to create the monthly counts to know what to divide the daily sums by to get the average column a i.e. the number of

How do I exclude weekends in SQL?

ぃ、小莉子 提交于 2019-12-05 22:18:39
I have a date column SLA_Date in the Orders table. My SLA_Date should exclude weekends (Saturday & Sunday). Data for weekdays should be shown alone. How do I do that in SQL? You just need to add the following filter : WHERE TO_CHAR(date_column, 'DY','NLS_DATE_LANGUAGE=AMERICAN') NOT IN ('SAT', 'SUN') Your query would look like: SELECT SLA_Date FROM orders WHERE TO_CHAR(SLA_Date, 'DY','NLS_DATE_LANGUAGE=AMERICAN') NOT IN ('SAT', 'SUN') For example(the WITH clause is only to build a test case), the below query is to display only the weekdays(i.e. excluding the Sat and Sun) ranging from 1st May

SQL date conversion results to “invalid number format model parameter.”

穿精又带淫゛_ 提交于 2019-12-05 18:11:55
I have to select some data from Oracle 11g database, but I can't seem to figure out why following select query is failing: SELECT INFO_ID, INFO_DETAIL, IMPORTANT_FLG, DELETE_FLG, CREATE_TIME, DISPLAY_ORDER FROM TABLE_NAME WHERE TO_DATE(TO_CHAR(CREATE_TIME, 'YYYY/MM/DD'), 'YYYY/MM/DD') BETWEEN TO_DATE(TO_CHAR(:fromDate, 'YYYY/MM/DD'), 'YYYY/MM/DD') AND TO_DATE(TO_CHAR(:toDate, 'YYYY/MM/DD'), 'YYYY/MM/DD') ORDER BY IMPORTANT_FLG DESC NULLS LAST , DISPLAY_ORDER ASC NULLS LAST, CREATE_TIME DESC, INFO_ID ASC The query is failing with following error message: ORA-01481: invalid number format model

Subtracting n Days from a date using SQL

丶灬走出姿态 提交于 2019-12-05 12:26:45
I am quite a beginner when it comes to Oracle. I am having trouble figuring out how to do something similar to this : SELECT ID, NAME, TO_CHAR(DATEBIRTH, 'DD/MM/YYYY HH24:MI:SS') FROM PEOPLE WHERE DATEBIRTH >= ANOTHERDATE - NDAY To put it short, I want to select everyone who were born N days before a specific date and time but I am not quite sure that this is the way to do it nor that it would give me the results I expect. PS: I am developping under oracle8i. Your query looks correct to me. That's how you subtract days from dates in Oracle. This link holds some more insight for you, should you

Fill in missing year in ordered list of dates

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 10:20:43
I have collected some time series data from the web and the timestamp that I got looks like below. 24 Jun 21 Mar 20 Jan 10 Dec 20 Jun 20 Jan 10 Dec ... The interesting part is that the year is missing in the data, however, all the records are ordered, and you can infer the year from the record and fill in the missing data. So the data after imputing should be like this: 24 Jun 2014 21 Mar 2014 20 Jan 2014 10 Dec 2013 20 Jun 2013 20 Jan 2013 10 Dec 2012 ... Before lifting my sleeves and start writing a for loop with nested logic.. is there a easy way that might work out of box in R to impute

Difference in time between records

馋奶兔 提交于 2019-12-05 08:33:49
I have a table that has (among others) a timestamp column (named timestamp; it's a standard Oracle DATE datatype). The records are about 4-11 minutes apart, about 7 or 8 records every hour, and I'm trying to determine if there is any pattern to them. Is there an easy way to see each record, and the number of minutes that record occurred after the previous record? Thanks, AndyDan This is Oracle 9i+, using the LAG function to get the previous timestamp value without needing to self join: SELECT t.timestamp - LAG(t.timestamp) OVER (ORDER BY t.timestamp) AS diff FROM YOUR_TABLE t ...but because

Find NSDate for the next closest specific day of week for any date

不问归期 提交于 2019-12-05 04:10:51
Let's suppose that today is Wednesday. I can break an NSDate down into NSDateComponents , but I need to find the NSDate with the next upcoming Monday. If today is Monday, then the next upcoming Monday is today. What's the right way to achieve this? You can use nextDateAfterDate: method on NSCalendar object to achieve this, let now = Date() // today var matchingComponents = DateComponents() matchingComponents.weekday = 2 // Monday let comingMonday = Calendar.current.nextDate(after: now, matching: matchingComponents, matchingPolicy:.nextTime) Here, is a simple method to find next monday. If

Test if date occurs in multiple date ranges with R

梦想与她 提交于 2019-12-05 02:36:46
问题 I have a data frame with multiple date ranges (45 to be exact): Range Start End 1 2014-01-01 2014-02-30 2 2015-01-10 2015-03-30 3 2016-04-20 2016-10-12 ... ... ... They will never overlap I also have a data frame with various event dates (200K+): Event Date 1 2014-01-02 2 2014-03-20 3 2015-04-01 4 2016-08-18 ... ... I want to test if these dates fall within any of these ranges: Event Date InRange 1 2014-01-02 TRUE 2 2014-03-20 FALSE 3 2015-04-01 FALSE 4 2016-08-18 TRUE ... What is the best

How to calculate next Friday at 3am?

穿精又带淫゛_ 提交于 2019-12-05 02:04:00
How can you calculate the following Friday at 3am as a datetime object? Clarification: i.e., the calculated date should always be greater than 7 days away, and less than or equal to 14. Here's a function and a test that it meets the OP's requirements: import datetime _3AM = datetime.time(hour=3) _FRI = 4 # Monday=0 for weekday() def next_friday_3am(now): now += datetime.timedelta(days=7) if now.time() < _3AM: now = now.combine(now.date(),_3AM) else: now = now.combine(now.date(),_3AM) + datetime.timedelta(days=1) return now + datetime.timedelta((_FRI - now.weekday()) % 7) if __name__ == '__main

jQuery: pass string variable to date object

亡梦爱人 提交于 2019-12-05 01:10:43
I have a page with a number of variables that contain a date in string format (yyyy-mm-dd) which originates from using moment.js. Is there a way I can pass such a variable to a Javascript date object resp. to convert it to a Javascript date object ? I am not interested in the time so as long as I can get the date converted to a date object that would be great. I tried the following but this doesn't work and I couldn't find a way using moment.js: var newVar = new Date(dateVar); Many thanks for any help with this, Tim. first of all i will say following should work for you.. var dateVar = "2010