NHibernate Delete with date arithmatic using HQL - date

I've looked around and can't find too much. But is it possible to do something like this using HQL in nHibernate:
Session.CreateQuery(#"DELETE FROM MyObject mo
WHERE (mo.AlteredDate + mo.ExpiryDetails.ExpiryTimestamp) < :pNow")
.SetDateTime("pNow", DateTime.Now);
So basically I want to delete all MyObjects from the database where the last time the object was altered (mo.AlteredDate - a DateTime) plus an amount of time such as 2 days and 5 hours (ExpiryDetails.ExpiryTimestamp) is less than now.
Or is it best to retrieve the objects and do the caculation in code using the .NET framework?

Super late to answer, but I did something like this & it works:
IQuery query = Session.CreateQuery("select x from OBJECT x where x.DateTimeForCompare > :dateTimeForCompare2");
query.SetDateTime("dateTimeForCompare2", DateTime.Today);
IList<OBJECT> xx = query.List<OBJECT>();

Related

Spark, Scala, Databricks, combine and add columns

Using Spark/Scala to attempt a "simple" query. I have a file which, after line 1 below runs, looks like this
EmpReg,EmpOT,RegPay,OTPay
Alice,Alice,400,20
Bob,Bob,300,0
Carol,Carol,450,120
Dan,Dan,400,200
Ellen,Ellen,360,40
The first and third columns (EmpReg, RegPay) come from one source and the second and third columns (EmpOT, OTPay) come from a second source. My objective is output that looks like this.
Emp,Pay
Alice,420
Bob,300
Carol,570
Dan,600
Ellen,400
Here is the code that I have been trying, at least what I have saved.
var q2 = q.join(q1, q("EmpReg") === q1("EmpOT"), "fullouter")
//q2 = q2.select("EmpReg", ($"RegPay" + $"OTPay"))
//q2 = q2.groupBy($"EmpReg".sum($"RegPay" + $"OTPay"))
var add = q2.select(($"RegPay" + $"OTPay"))
//q2 = q2.sum("RegPay", "OTPay")
//q2 = q2.groupBy("EmpReg", "EmpOT")
//var q2 = q.join(q1).where("EmpReg") === "EmpOT"))
//q2 = q2.select("EmpReg").sum("RegPay", "OTPay")
//q2.show
add.show
[q] is the first file which represents regular pay. [q1] is the second file which represents overtime pay. [q2] is the combination shown in the first example above. Primary keys are [EmpReg] and [EmpOT]. don't really need to combine [EmpReg] and [EmpOT] since they are the same, and it doesn't make any difference which I use.
I really need to add [RegPay] and [OTPay] to get [Pay], but for the life of me I can't get it to work. The lines commented out return various errors. I can add the two pay columns, and select an appropriate employee column, but can't seem to do it in one query. I am constrained to use Scala on Databricks. Othewise, I might do something like this.
select q.EmpReg as Emp, (q.RegPay + q1.OTPay) as Pay
from q join q1 on q.EmpReg = q1.EmpOT
(Why can't things ever be simple?)
You can use a similar approach as in your SQL query:
val q2 = q.join(q1, q("EmpReg") === q1("EmpOT"), "fullouter")
val add = q2.select(q("EmpReg").as("Emp"), (q("RegPay") + q1("OTPay")).as("Pay"))
Your code has this line
q2.select("EmpReg", ($"RegPay" + $"OTPay"))
which should work if you add $ before "EmpReg". You can't have both strings and columns in the select statement. This works in Python but not Scala.

Magento 2 - Category List Sort by Position with Sub Category Not Working

A little background, I'm trying to do a custom Category listing, but at the moment it seems the Category not being sort as I seen on Admin.
Here's the code that I've done so far
$current_store = $this->_storeManager->getStore();
$root_category_id = $this->_storeManager->getStore()->getRootCategoryId();
$collection = $this->_categoryCollectionFactory->create()
->addAttributeToSelect('*')
->addAttributeToFilter('is_active', 1)
->setStore($current_store);
return $collection->addAttributeToFilter('entity_id', array('nin' => $root_category_id))
->setOrder('position','ASC');
And the result, when I tried to echo its ID is like below
3
10
4
11
5
7
12
8
15
9
13
14
16
6
But, from the Admin, it doesn't reflect the order correctly, below is the figure
The problem that I realize is, that, I have sub category, I tried to echo the query from above code, and then copy-paste it into sql GUI, and I realize, the position is kinda weird but, it does make-sense, because it's a sub category.
Here's the result when I execute the query on sql GUI
So, what I tried to achieve is to sort above result, to reflecting what I set on Admin.
Is there anything that I missed? I'm not sure where to look, since I've been stuck around 1-2 days, not sure what's the proper keyword, almost all keyword I did will arrive to product sort or kind of that, not category sort
Thanks in Advance!
For those who still needs some answer relating to this question, here's the answer
...
$store_categories = $this->_categoryFactory->create();
$store_categories = $store_categories->load($this->_root_category_id)->getChildrenCategories();
foreach ($store_categories as $category) {
//get id
$category_id = $category->getId();
//get category model
$category = $this->getCategoryModel($category_id);
$sub_children = $this->getActiveChildCategories($category);
if (count($sub_children) > 0) {
$sub_categories = $this->getSubCategory($sub_children);
$categories = array_merge($categories, $sub_categories);
} else {
$categories[] = $category;
}
}
...
The _categoryFactory comes from Magento\Catalog\Model\CategoryFactory.
It's pretty much covering what I want, but not really as I expected before, because I think it's not really efficient.
PS - I'm still new on Magento 2, so, if someone else has other answer that might be pretty much like I expect, then, I'm happily change it as Accepted Answer. :)
Goodluck!

Find number of days between two given dates in Rcpp

I'm newbie in Rcpp, but I have a task which's connected with Date and Datetime.
Let me have market data in DataFrame in my Rcpp function. So, Date field has formatting like this:
2016-04-19 00:01:00
Dataframe field name which contains Date values is "Date". So, I get 2 vectors:
DatetimeVector datetime = df["Date"];
DateVector pureDate = df["Date"];
Problems:
1) I can't take difference between 2 Date values of Date (I don't know why, but gcc-4.9.3 gives me an error on difference like this:
Date pureDay = pureDate[0];
auto tmp = pureDate[j+1] - pureDay;
error: ambiguous overload for 'operator-' (operand types are
'Rcpp::traits::storage_type<14>::type {aka double}' and 'Rcpp::Date')
auto tmp = tmpDate[j+1] - tmpTradeDay;
But if I use code like this:
Date pureDay = pureDate[0];
auto tmp = pureDate[j+1] - pureDate[j];
It works well.
2) How it possible to format an output for Date and Datetime objects? to_string won't format it well - I give a result like this: 1461176460.000000
3) I expected that syntax like Date(datetime[i]) will give me a Date object. But it won't. I know that pureDate[1] - pureDate[0] should have the same Y-M-D value, but they differ for series lag (60 seconds).
Thnxs. Could anyone helps me with these problems?
You seem a little lost, and there are really multiple questions in this one.
Question 1) will give a short example below.
Question 2) is mostly about formatting, you may want to look into the class documentation and header; both Date and Datetime have a format() method that works just like the R equivalent or C library function for date(time) formating, the fabled strftime().
Question 3) is unclear; I am not sure what you are asking. Maybe the answer to question 1) below helps.
Simple example for question 1:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double question1(DateVector dv) {
double d = dv[1] - dv[0];
return d;
}
/*** R
set.seed(123)
datevector <- Sys.Date() + cumsum(runif(3)*30);
datevector
diff(datevector)
question1(datevector)
*/
and its outcome:
R> Rcpp::sourceCpp("/tmp/datequestion.cpp")
R> set.seed(123)
R> datevector <- Sys.Date() + cumsum(runif(3)*30);
R> datevector
[1] "2018-03-28" "2018-04-21" "2018-05-03"
R> diff(datevector)
Time differences in days
[1] 23.6492 12.2693
R> question1(datevector)
[1] 23.6492
R>
Same answer as from R. Your code still has an index calculation, that sometimes confuses the compiler. Making it simpler (ie more steps) often helps.
Lastly, maybe look at some Rcpp documentation and examples. The RcppExamples package has a function on dates and datetimes ...

Carbon Difference in Time between two Dates in hh:mm:ss format

I'm trying to figure out how I can take two date time strings that are stored in our database and convert it to a difference in time format of hh:mm:ss.
I looked at diffForHumans, but that does give the format I'd like and returns things like after, ago, etc; which is useful, but not for what I'm trying to do.
The duration will never span days, only a max of a couple hours.
$startTime = Carbon::parse($this->start_time);
$finishTime = Carbon::parse($this->finish_time);
$totalDuration = $finishTime->diffForHumans($startTime);
dd($totalDuration);
// Have: "21 seconds after"
// Want: 00:00:21
I ended up grabbing the total seconds difference using Carbon:
$totalDuration = $finishTime->diffInSeconds($startTime);
// 21
Then used gmdate:
gmdate('H:i:s', $totalDuration);
// 00:00:21
If anyone has a better way I'd be interested. Otherwise this works.
$finishTime->diff($startTime)->format('%H:%I:%S');
// 00:00:21
$start = new Carbon('2018-10-04 15:00:03');
$end = new Carbon('2018-10-05 17:00:09');
You may use
$start->diff($end)->format('%H:%I:%S');
which gives the difference modulo 24h
02:00:06
If you want to have the difference with more than 24h, you may use :
$start->diffInHours($end) . ':' . $start->diff($end)->format('%I:%S');
which gives :
26:00:06
I know this is an old question, but it still tops Google results when searching for this sort of thing and Carbon has added a lot of flexibility on this, so wanted to drop a 2022 solution here as well.
TL;DR - check out the documentation for different/more verbose versions of the diffForHumans method, and greater control over options.
As an example, we needed to show the difference between two Carbon instances (start and end) in hours & minutes, but it's possible they're more than 24 hours apart—in which case the minutes become less valuable/important. We want to exclude the "ago" or Also wanted to join the strings with a comma.
We can accomplish all of that, with the $options passed into diffForHumans, like this:
use Carbon\CarbonInterface;
$options = [
'join' => ', ',
'parts' => 2,
'syntax' => CarbonInterface::DIFF_ABSOLUTE,
];
return $end->diffForHumans($start, $options);
Which will result in values like what's seen in the Duration column:
Hope that's helpful for someone!
You can do it using the Carbon package this way to get the time difference:
$start_time = new Carbon('14:53:00');
$end_time = new Carbon('15:00:00');
$time_difference_in_minutes = $end_time->diffInMinutes($start_time);//you also find difference in hours using diffInHours()

Cypher: Problems with comparison

I'm trying to excecute a cypher query for a no4j database on gwt.
I stored in some nodes int values as property detail. If I'm using neoclipe right, I noticed now, that this values are stored in the database as String values.
In my query I have the following part which does not work:
START ...
MATCH node-[:SomeTag]->intnode
WHERE intnode.detail < 10
RETURN ...
and I get:
Don't know how to compare that. Left: 15; Right: 10: Don't know how to compare that: Left: 15; Right: 10
So intnode.detail < 10 does not work. I also tried this: intnode.detail < STR(10), because I thought it will compare the hash values or ascii values, but I got the same error.
EDIT:
I read, that it is possible to set the #GraphProperty while storing data, but how can I do that in gwt?
I mean if I have a node and I could e.g. write
Object obj = (Object) 10;
node.setProperty("detail", obj);
How can I now tell neo4j, that obj is an int?
This answer is mostly focused on your initial question - not on the question you´ve added in the EDIT-part.
I just had a similar problem with a comparison inside the WHERE-part of a cypher query. I tried to do something like
MATCH ...
WHERE value > 1
which caused an error message very similar to yours. After some testing I´ve found out that the query works, if I add single quotes. This is my solution:
MATCH ...
WHERE value > '1'
(note the quotes)
Ive also noticed, that this doesnt work with double quotes
I hope this helps you and/or anyone else who encounters this problem :)
I think the intnode.detail value in stored as string , so you wont able to compare with integer value.
You have to do like this
START ...
MATCH node-[:SomeTag]->intnode
WHERE intnode.detail < "10"
RETURN ...