get all documents from previous month in couchbase server 5.1 - couchbase

I'm trying to get all documents from previous month in couchbase server 5.1.1.
I have a field dateCreation in timestamp
my query will be launched every month(M) at 15 day to retreive M-1 documents.
i use N1QL.
Thanks in advance.

CREATE INDEX ix1 ON bucket(dateCreation);
SELECT * FROM bucket WHERE dateCreation >= DATE_ADD_STR(CLOCK_STR(), -1, 'month');
You can change CLOCK_STR() with constant or change as query parameter and supply the value.
Checkout DATE functions https://developer.couchbase.com/documentation/server/current/n1ql/n1ql-language-reference/datefun.html

To supplement vsr's answer, since you want all documents from the previous calendar month, try this:
SELECT * FROM bucket
WHERE
dateCreation >= SUBSTR(DATE_ADD_STR(CLOCK_STR(), -1, 'month'),0,8) || "01"
and
dateCreation < SUBSTR(CLOCK_STR(),0,8) || "01";

Related

How to search data for a date value after than the specified one?

I have a basic SQL knowledge and I am trying to retrieve data from my database based on the date a user chooses. So, if the user selects 2018-03-01 I want to display the data for the date value 2018-03-02.
I can search data for a specified date in the following way:
select * from FLIGHTS where DEPARTURE_DATE = '2018-03-01';
Now I want to search the data for the date next to the specified so for '2018-03-02' in this case. I cant directly search for 2nd March because I don't know what date the user will choose
So is there any way to query data for the date next to the one specified?
I have already tried looking up everywhere but couldn't find anything that makes sense to me.
Thx
I think this works well. By the documentation for date/time functions, you should be able to do something like,
select * from FLIGHTS where DEPARTURE_DATE = '2018-03-01' + INTERVAL 1 DAY
What sort of element are you using for the user to be able to get their date and what is your codebase? We have mysql for the DB but we need to know if your codebase is c#, PHP etc.
Most languages generally have a function to allow you to add days to a datetime, if for instance you used C# and had a datetime selector posting its value you can do:
DateTime dateField = postedDateTime.AddDays(1);
from there you can easily escape the value and push it through your db call.
or PHP:
$dateField = date('Y-m-d', strtotime($postedDateTime. ' + 1 days'));
If you would like a date range (date selected and the next day) then you would adjust your php code to have:
$dateFrom = $postedDateTime;
$dateTo = date('Y-m-d', strtotime($postedDateTime. ' + 2 days'));
$dateTo = date('Y-m-d', strtotime($dateTo. ' - 1 seconds'));
//DB Query
$query = "select * from FLIGHT where DEPARTURE_DATE >= :dateFrom AND DEPARTURE_DATE <= :dateTo";
The above assuming you're using pdo, but you can see how to adjust for your situation. I am also assuming the datetime is based on midnight for each day so you may need to adjust your dateTo to take from 1 second before midnight the day after just so you can get the entire days data
You can make use of interval as day as mentioned in the answer. Along with day you can also have day_minute, day_hour,day_second but be sure when you are adding decimals on these.

mysql filter by date and time separately

I have to perform a query on a MySQL database.
I have a table with records, have a column called "date" (the date type), and a column called "time" (type. Integer is stored by multiplying the time of day by 60. eg 8 am is stored as 480).
Unfortunately, the format of this table can not be modified.
My table stores attentions of doctors on call. The doctors on duty working in two shifts: from 8-20, and 20-8.
I need to know the amount of attention for every doctor.
My query must be filtered by date range and shift.
The problem is that, in the case of doctors working at the turn of 20-8, I have to consider a change of day. (sorry for my bad English).
What I have done is this, this would be an example to date of yesterday, and doctors shift 20-8.
SELECT * FROM attentions WHERE (date >= '2015-07-23' and time >=1200) and (date <= '2015-07-24' and time <480)
the query does not work at all.
Supposing the date field is called: 'a_date' with format 'yyyy-mm-ss' and the time field is a number, the query should be:
SELECT * FROM attentions WHERE (date(a_date) >= '2015-07-23' and time >=1200) and (date(a_date) <= '2015-07-24' and time <480)
Can you check using between?
SELECT * FROM attentions WHERE date between '2015-07-23' and '2015-07-24' and time between 1200 and 480
I think you can also use this -
SELECT * FROM ***** where CREATED_DATETIME between '2015-03-12 00:00:00' and '2015-05-11 00:00:00';

SQL query to select data between dates does not show last date

im using a query to get data between dates but for some reason it does not pull the data of the last date selected here is my query:
SELECT * FROM order WHERE status = "completed" AND orderdate >= ? AND orderdate <= ? ORDER BY orderid DESC
Im using is equal to or less then... but still?
what am i doing wrong ?
SELECT * FROM order WHERE status = "completed" AND date(orderdate) >= date(?) AND date(orderdate) <= date(?) ORDER BY orderid DESC
It happened with me also, but in my case instead of passing a date I was querying using a datetime variable, Please make sure you are querying with date variable only.
Make sure that orderdate is date as well as your query parameter is also date, or use appropriate function to convert them in date, than query.
Your dates are actually datetimes - so you are actually, in the case of the upperbound, saying "12 midnight" on whichever date you choose. Hence, if it tries to test a value at say 10am in the morning, it fails as being outside the range.
Either set the upperbound date one day forward, or explicitly only test the date part of the datetime...

How to get count of records added since the start of the current month

I am building a webservice using php & mysql, and would like to limit requests from each apikey to (x) amount within (y) time.
To achieve this, i count the records added to the the log from each apikey within a period using a sql statement like this:
SELECT COUNT(*) FROM ws_account_log WHERE account_log_account_id='1' AND account_log_timestamp > DATE_SUB(now(), INTERVAL 1 HOUR)
This gives me the a rolling hourly count, which is fine, but I would like a hard limit on the monthly count. eg how many rows have been added since 00:00 of the first day of the current month.
I have seen examples using stored procedures and different syntax, I would like an answer that will work on MYSQL please. Also, if possible the fastest implementation as one of the services provides autocomplete functions, therefore the log table is rather large.
Thanks
SELECT COUNT(*)
FROM ws_account_log
WHERE
account_log_account_id='1'
AND account_log_timestamp >= SUBDATE(CURDATE(), DAYOFMONTH(CURDATE())-1)
Calculate timestamp to the moment you want using php, then pass it to mysql query.
$date = mktime(0, 0, 0, date("n"), 1, date("Y"));
$mysqldate = date( 'Y-m-d H:i:s', $date );
$query = " ... AND account_log_timestamp > ".$mysqldate;

How can I group data by month, day and hour in mysql?

I have a mysql database that looks like this:
id | userid | timestamp | activity
Timestamp is a datetime data type, I need to get the data grouped by month, day and hour. I am using mysql and php for my scripts.
I am able to do it by month and day with the following query:
$query = "SELECT COUNT(id) as totals FROM security_transactions WHERE YEAR(timestamp) = 2012 GROUP BY MONTH(timestamp), DAY(timestamp)";
I need to do it by month day and hours.
Please help.
Thanks.
You can add , HOUR(TIME(timestamp)) to your group by query providing your column is of DATETIME format
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_hour
Also, from the error messages put in the comments below, it looks like #Aprentice is not using mysql, but I've improved this answer for others looking for mysql.
I have never used mssql, so I can't test this but the following might work to group by nearest hour:
GROUP BY dateadd(hour, datediff(hour, 0, timestamp, 0)
Just take it one step further and use HOUR() as well. You will first need to extract the time portion of the timestamp. But guess what, there is a function for that as well ;)