How to query between two datetimes using Laravel and Eloquent but but datetimes are not ordered? (error Prepared statement contains too many place...) - laravel

Background:
Data is retrieved every minute from an API and stored in a 'sensors' table.
If the data is older than one year. They will be summarized, saved as one new entry in the database and the old data will be deleted. Therefore the column 'date' is not sorted. An update to a specific date is not possible, since I cannot be sure whether the sensor actually generated data at that time.
Now to my problem:
I have a querie that looks something like this
private function getDataFromPeriod()
{
return Sensor::whereBetween('date', [$this->start, $this->end])->orderBy('date')->get();
}
Where $start and $end are in this format for example: '2022-12-31 01:59:59'.
This function is called during the function render() and that works (at the beginning the data from the last year are sorted)). However, when I call this function repeatedly in a different place, the error 1390 Prepared statement contains too many placeholders comes up.
I suspect this is because the query is not good.
Does anyone have an idea how I can improve the query or where else the error could be?
Thanks for your help

you don't need to sort by date;
just replace your code with this:
return Sensor::where('date','>=', $this->start)->where('date','<=', $this->end)->get();
this line will give you the sensors between your start and end date

Related

only show events who are greater than or equal laravel

Im working in a project where I display different events in my view. The goal for now is to only show events that are upcoming, so it doesn't show events that are older than today.
The users of my page can create an event and set a sertain date for it.
The data is stored into my db like this:
$table->string('datum');
In the view it return it like this:
06.03.2019
My controller to return the data looks like this:
$date = date('d.m.Y');
$evententries = Event::where('datum', '>=', $date)->orderBy('datum', 'asc')->take(3)->get();
but it's somehow not working..
Anyone got any Ideas of how to fix this and what my issue is here?
btw, I'm using laravel 5.7.
Thank you :)
Hmm you are doing it on wrong way from begin...
First: why you are using string to store date value? there is much better options to store date within database...
Second: you are using date format which cannot be compared on that way (using < > =)
when you are comparing strings ie 08.03.2019 will be always smaller than 10.01.2016 because 1 > 0... so if you are comparing dates use:
db format for storing dates
for comparing use format yyyy-mm-dd because on that way even simple string comparison will give you correct result...

Using time field in Eloquent model

I have got a database field containing only a time, it's declared in my migration as
$table->time('time')->default('00:00:00');
Now I want to add a number of seconds to this field using Eloquent, doing something like:
$activetime->time->add(new DateInterval('PT5S'));
(where $activetime is a object of a Eloquent model)
First try on this I got
Call to a member function add() on string
So obviously I have to tell Eloquent that this is not a string but a time. The closest I have got is putting it in protected $dates in the model, but then instead I get
Unexpected data found.
I suspect this is because it isn't really a date but only a time, but I can't seem to find much information about how to handle times. Is there any way of solving this or would I be better off using something else, like dateTime or timestamp (or even an integer for amount of seconds)? What I DO want to record is a users amount of active time for a given date, so for that purpose time seems like the natural choice...
This is because Laravel reads time database type as just a string and you are expecting a DateTime Object.
Add this to your Eloquent model:
public function getTimeAttribute($value)
{
return new \DateTime($value);
}
You can also cast your time to DateTime Object using Laravel Attribute Casting, but you'll have to append some date to your time, because Laravel is expecting correct format

Inconsistent query results

I have a query that builds a cursor to create invoices. Part of it is the expression "IIF(cuPR.curren="EUR",NULL,rate) AS Taux".
My problem is that my query works fine for January through April and for June, but not for May. I checked the query to identify the problem, I checked and rechecked my data, everything looks fine. The data being the only thing that changes, what else should I check, please??
Usually if there exists a possibility that I will have data that may throw things off like you have here I will use a cast to ensure my field is what I expect.
Something like...
SELECT CAST(CAST(IIF(cuPR.curren="EUR", NULL, rate) AS Numeric(10,5)) AS Taux ...
Upon further research I noticed that in May the first record in the cursor cuPR had "EUR" as "curren". I tried sorting my cursor by "curren DESC", making sure EUR would not be in the first record (USD and GBP are other possible values) and my query went through.
DRapp had given the explanation in response to a previous question of mine:
"Bernard (and others new to VFP). VFP queries actually run the query twice, once for the first record just to confirm the final column types and sizes, then for the actual query of ALL records." In my case one of the columns was NULL...

couchdb get session date and query is on veiw

i want get the session date when it open to get all the record after i open my session not before ,i want something like this
function(doc) {
if (doc.created_at) {
if session.date => doc.created_at {
emit(doc.created_at, doc);
{
}
};
I've fallen for this problem myself when I was a couchdb newbie.
You need to understand first, that the map function is not executed when you run the view. The time of execution is only the very first time when the view is called after the document was last updated. And that only if the stale parameter was either not used or set to updateAfter.
What you can do instead is to use the startkey parameter when accessing the view. If you set this to the sessiondate, then only those documents created after the session date will be returned.
You however have to ensure consistent formatting and that the keys will be strictly sorted alphabetically numerically. E.g. by translating them to epoch times or a format in the style of yyyymmdd-ddmmss like 20140618-211259 for the time now (18th of June 2014 # 21:12:59)
Some examples of the parameters you can use are here.

How to get sorted rows out of cassandra when using RandomPartioner and Hector as Client?

To improve my skills on Hector and cassandra I'm trying diffrent methods to query data out of cassandra.
Currently I'm trying to make a simple message system. I would like to get the posted messages in chronological order with the last posted message first.
In plain sql it is possible to use 'order by'. I know it is possible if you use the OrderPreservingPartitioner but this partioner is deprecated and less-efficient than the RandomPartioner. I thought of creating an index on a secondary column with a timestamp als value, but I can't figure out how to obtain the data. I'm sure that I have to use at least two queries.
My column Family looks like this:
create column family messages
with comparator = UTF8Type
and key_validation_class=LongType
and compression_options =
{sstable_compression:SnappyCompressor, chunk_length_kb:64}
and column_metadata = [
{column_name: message, validation_class: UTF8Type}
{column_name: index, validation_class: DateType, index_type: KEYS}
];
I'm not sure if I should use DataType or long for the index column, but I think that's not important for this question.
So how can I get the data sorted? If possible I like to know hows its done white the CQL syntax and whitout.
Thanks in advance.
I don't think there's a completely simple way to do this when using RandomPartitioner.
The columns within each row are stored in sorted order automatically, so you could store each message as a column, keyed on timestamp.
Pretty soon, of course, your row would grow large. So you would need to divide up the messages into rows (by day, hour or minute, etc) and your client would need to work out which rows (time periods) to access.
See also Cassandra time series data
and http://rubyscale.com/2011/basic-time-series-with-cassandra/
and https://www.cloudkick.com/blog/2010/mar/02/4_months_with_cassandra/
and http://pkghosh.wordpress.com/2011/03/02/cassandra-secondary-index-patterns/

Resources