I have one line of code that is like this
| extend Has_Changed = iff(NextBIP15 == PreviousBIP15, 0, 1)
and I was wondering if it is possible to add extra conditions to "iff" and in that case what would be the syntax in order to do this?
I would like to do something like
| extend Has_Changed = iff(NextBIP15 == PreviousBIP15 AND NextBIP14 == PreviousBIP14, 0, 1)
extend Has_Changed = iff((NextBIP15 == PreviousBIP15) and (NextBIP14 == PreviousBIP14), 0, 1)
?
That is, collect the predicates in parentheses....
Related
I do not need to know the actual results or even a count - just if the result is null or not.
I am currently doing it like this and then looking at the count:
int itemsNeedingUpdated =
(from i in cDb.DistributionLineItems
where (i.CompanyNo == item.dt_company_no && i.UniqueIdNo == item.Unique_Id_No) &&
(i.DatetimeUpdated >= startingDateTimeToSearch) &&
(i.ReceivingScanPieces > 0 || i.LoadingScanPieces > 0 || i.ActualPieces > 0)
select i.UniqueIdNo).Count();
but as this is going to churn through a lot of times I want to know if this is the fastest way to check this?
Using EF 6 against Azure SQL.
You can use Any:
bool itemsNeedingUpdated =
(from i in cDb.DistributionLineItems
where (i.CompanyNo == item.dt_company_no && i.UniqueIdNo == item.Unique_Id_No) &&
(i.DatetimeUpdated >= startingDateTimeToSearch) &&
(i.ReceivingScanPieces > 0 || i.LoadingScanPieces > 0 || i.ActualPieces > 0)
select i.UniqueIdNo).
Any();
Which will bail out as soon as an item matching the predicate is found.
I am new to spark SQL and Dataframes. I have a Dataframe to which I should be adding a new column based on the values of other columns. I have a Nested IF formula from excel that I should be implementing (for adding values to the new column), which when converted into programmatic terms, is something like this:
if(k =='yes')
{
if(!(i==''))
{
if(diff(max_date, target_date) < 0)
{
if(j == '')
{
"pending" //the value of the column
}
else {
"approved" //the value of the column
}
}
else{
"expired" //the value of the column
}
}
else{
"" //the value should be empty
}
}
else{
"" //the value should be empty
}
i,j,k are three other columns in the Dataframe. I know we can use withColumn and when to add new columns based on other columns, but I am not sure how I can achieve the above logic using that approach.
what would be an easy/efficient way to implement the above logic for adding the new column? Any help would be appreciated.
Thank you.
First thing, lets simplify that if statement:
if(k == "yes" && i.nonEmpty)
if(maxDate - targetDate < 0)
if (j.isEmpty) "pending"
else "approved"
else "expired"
else ""
Now there are 2 main ways to accomplish this
Using a custom UDF
Using spark built in functions: coalesce, when, otherwise
Custom UDF
Now due to the complexity of your conditions, it will be rather tricky to do number 2. Using a custom UDF should suit your needs.
def getState(i: String, j: String, k: String, maxDate: Long, targetDate: Long): String =
if(k == "yes" && i.nonEmpty)
if(maxDate - targetDate < 0)
if (j.isEmpty) "pending"
else "approved"
else "expired"
else ""
val stateUdf = udf(getState _)
df.withColumn("state", stateUdf($"i",$"j",$"k",lit(0),lit(0)))
Just change lit(0) and lit(0) to your date code, and this should work for you.
Using spark built in functions
If you notice performance issues, you can switch to using coalesce, otherwise, and when, which would look something like this:
val isApproved = df.withColumn("state", when($"k" === "yes" && $"i" =!= "" && (lit(max_date) - lit(target_date) < 0) && $"j" =!= "", "approved").otherwise(null))
val isPending = isApproved.withColumn("state", coalesce($"state", when($"k" === "yes" && $"i" =!= "" && (lit(max_date) - lit(target_date) < 0) && $"j" === "", "pending").otherwise(null)))
val isExpired = isPending.withColumn("state", coalesce($"state", when($"k" === "yes" && $"i" =!= "" && (lit(max_date) - lit(target_date) >= 0), "expired").otherwise(null)))
val finalDf = isExpired.withColumn("state", coalesce($"state", lit("")))
I've used custom udf's in the past with large input sources without issues, and custom udfs can lead to much more readable code, especially in this case.
I need to convert the following C statement to SQL query.
if((object->num1 == 10 && object->num2 == 11) || (object->num3 == 0 && object->num4 == 1)){
//something
}
I want something like
SELECT * FROM `table` WHERE (conditions here)
Thank you in advance.
You can use the following query:
SELECT *
FROM YOUR_TABLE
WHERE (num1=10 AND num2=11) OR (num3=0 AND num4=1);
I am new in ROR
i want to set condition parameter with find attribute.
See below my code
#navmenu = MenuItem.find(:all,:conditions=>[MenuItem.menu_item_id == nil OR MenuItem.id != 1 AND MenuItem.is_active == 1] )
this query given error. Please help
Hope this will helps you:
#navmenu = MenuItem.find(:all,:conditions=>["menu_items.menu_item_id = ? OR menu_items.id <> ? AND menu_items.is_active = ?", nil, 1, true])
Thanks.
I have this piece of code, that checks whether a returned object is null. If so, it will return 0, or else it will return a property inside the object.
var userPoints = (from Point p in entities.Point
where p.UserName == userName
select p).SingleOrDefault();
if (userPoints == null)
{
return 0;
}
else
{
return userPoints.Points;
}
Is it possible to simplify the if statements with the nullable operator? I've tried this, but the system throws an exception when attempting to read the property
return userPoints.Points ?? 0;
No, unfortunately there's nothing which will do exactly that. Options:
Use the conditional operator:
return userPoints == null ? 0 : userPoints.Points;
Change your query to make that do the defaulting:
return (from Point p in entities.Point
where p.UserName == userName
select p.Points).SingleOrDefault();
Personally I'd go for the latter approach :) If you wanted a default other than 0, you'd need something like:
return (from Point p in entities.Point
where p.UserName == userName
select (int?) p.Points).SingleOrDefault() ?? -1;
You can do this:
var userPoints = (from Point p in entities.Point
where p.UserName == userName
select p.Point).SingleOrDefault();
return userPoints;
If there are no results then userPoints will be 0, otherwise it will be the value of Points.
You can't use it in your context.
Explanation:
You want to check whether userPoints is null, but want to return userPoints.Points if it is not null.
The ?? operator checks the first operand for null and returns it, if it is not null. It doesn't work if what you want to check and what you want to return are two different things.
Hover over var with the mouse and see what type it is.