How to find latest inserted row from db row in YII? - lastinsertid

I have a table "backup_history" which store sites backup details in my database with the following fields:
id
site_id
backup_file_name
file_size
by (cron/manual)
datetime
For the next backup, I need to find out recent last backup date n time of the site and compare with the settings(daily backup) and run next backup by cron.
But am unable to find out last inserted row with the site_id = $id AND by = 'cron' AND datetime = 'this will be recent date-time'
$lastbackup = BackupHistory::model()->findByAttributes(array('site_id' => $single_site->id, 'by' => 'cron'));
This code provides a row but not with recent date n time.
May be am going wrong so plz suggest some solution. Thanks !!!

You'll need to order by your result. So try this:
$lastbackup = BackupHistory::model()->findByAttributes(array('site_id' => $single_site->id, 'by' => 'cron'),array('order'=>'datetime'));

Related

Postgresql date type has a void of one century at 0 AD

I'm working with PostgreSQL and while working with dates around the moment of birth of Christ suddenly encountered a problem with 0 AD point in time.
Say,
update mytable
set mydate='0000-09-01'
where id=1;
gives me
ERROR: date/time field value out of range: "0000-09-01"`
while both
update mytable
set mydate='0001-09-01'
where id=1;
and
update mytable
set mydate='0001-09-01 BC'
where id=1;
work fine.
I.e. the problem is that I cannot have 0 AD dates being stored in my database, which makes a one-century void in my timeline. Has anyone encountered the same problem? Is this a bug which is going to be fixed in the next PostgreSQL release? Is there any workaround (except of storing dates as strings) out there?

Date filter for not showing the recent date data in Tableau

I am building a dashboard for retention. The data that I am getting for the most recent day seems to have huge spikes because the denominator is not having entire data for the day.
So, I just want to show the data till previous day and it should be automated likewise.
Please let me know if anyone had dealt with the same problem.
Thanks,
Sai
The best way to do this is through creating a calculated field:
Create a field called Recent Date as follows:
DATETRUNC('day',[Date]) = {FIXED : MAX(DATETRUNC('day',[Date]))}
What this does is creates a Boolean field where the most recent date will be flagged as TRUE and all others as FALSE.
Drag this field into the filters pane and select FALSE. This will remove the most recent dates data.
Create a calculated field to return a boolean value if the data is from today's date. Then filter on False.
DATETRUNC('day', [Your Date Field]) = DATETRUNC('day', TODAY())

receiving batch error running an update on talend into PostgreSQL database

I have a talend solution where inside it rests a tMap --> tPostgreSQLOutput.
Inside the schema is a integer(key field) and a Date(Timestamp) in the format of "dd-MM-yyyy HH:mm:ss". The intent is to update the date field with the current time/date (Timestamp).
the date is set with this talend function call in the tMap:
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", TalendDate.getDate("yyyy-MM-dd HH:mm:ss"))
I have confirmed the date(timestamp) format, and confirmed that the timestamp data type in the PostgreSQL database. However, I'm getting this error upon runtime:
Batch entry 0 UPDATE "bitcoin_options" SET "last_notified" = 2017-04-08 12:02:40.000000 -05:00:00 WHERE "id" = 3 was aborted. Call getNextException to see the cause.
I took the query it errored and manually ran it into PostgreSQL. I got this response:
ERROR: syntax error at or near "11"
LINE 1: ...bitcoin_options" SET "last_notified" = 2017-04-08 11:53:11.0...
^
Again, I checked the format, the datatype, and compared them against other tables and their UPSERTS. same format. same datatype.
In addition, I attempted to add a second space between date and time, with no avail.
UPDATE 1
I updated the tMap output to:
TalendDate.getCurrentDate();
and got the same error.
Thanks
UPDATE 2
Here's my layout for Talend:
I figured it out. after much trial and error.
The tPostgresSQLCommit x3 was redundant. When I removed the first two and placed just one, it gave me the proper output.
LESSONS LEARNED: You only need 1 commit.
Notice your timestamp is not properly formatted:
UPDATE "bitcoin_options" SET "last_notified" = '2017-04-08 12:02:40.000000 -05:00:00' WHERE "id" = 3
It's missing the single quotes surrounding the timestamp. If you add those you should be good to go.

How to get all rows from last inserted date in a table?

So, if I do an insert everyday, how do I get the latest rows of data that correspond to the latest date from Postgresql using SQlAlchemy?
Do this with two queries. First get the latest date. Then get the rows with that date. Here's an example where the Item model has an updated date column.
latest = session.query(func.max(Item.updated)).scalar()
items = session.query(Item).filter_by(updated=latest).all()

Filtering table rows with higher date than provided

I'm currently trying to do it that way:
// Creating date object
$date = new Zend_Date();
// Adding to it 4 weeks
$date->add('4', Zend_Date::WEEK); // it's expire day
// Getting date in integer(i guess it's unix timestamp yes?)
$date->get();
// Saving it to Mysql in field 'expire' with type Varchar
Then, when needed to get rows, that have date bigger(that haven't yet expired), than current I just add to SQL a simple statement WHERE expire >= $current_date.
Or there is better way to do it? Or how it happens usually?
I would recommend using the native MySQL DATETIME column in your table. This is how you'd retrieve the date for MySQL:
$date->get('yyyy-MM-dd HH:mm:ss');