Can someone please tell me how do we compare a table column against NULL using slick's lifted embedding.
What i want to achieve in mysql:
select * from Users where email = '<some_email_addr>' and ( removed = NULL OR removed <= 1 )
It gives me an errors at x.removed === null when i try:
val q = for {
x <- Users
if x.email === email && ( x.removed === null || x.removed <= 1 )
} yield x.removed
Thanks
Try:
x.removed.isNull
I think that is what you are looking for
As daaatz said and per http://slick.typesafe.com/doc/2.1.0/upgrade.html#isnull-and-isnotnull you should use isEmpty, but it only works on Option columns. A workaround is x.removed.?.isEmpty.
Try
x.removed.isEmpty()
There are no nulls in scala ;-)
Related
I built a dataframe in pyspark and now I want to select the value of two columns. How can I do this?
I tried this:
df.where((df['E'] ==0 ).where(df['C']=='non'))
Thanks
You use & (and) logic operator for this:
df.where((df['E'] == 0) & (df['C'] == 'non'))
You can either where or filter -
df.where((df.E == 0) & (df.C == 'non'))
##OR
df.filter((df.E == 0) & (df.C == 'non'))
You can use sql syntax directly:
df.where("E=0 and C='non'")
I am aware of how to implement a simple CASE-WHEN-THEN clause in SPARK SQL using Scala. I am using Version 1.6.2. But, I need to specify AND condition on multiple columns inside the CASE-WHEN clause. How to achieve this in SPARK using Scala ?
Thanks in advance for your time and help!
Here's the SQL query that I have:
select sd.standardizationId,
case when sd.numberOfShares = 0 and
isnull(sd.derivatives,0) = 0 and
sd.holdingTypeId not in (3,10)
then
8
else
holdingTypeId
end
as holdingTypeId
from sd;
First read table as dataframe
val table = sqlContext.table("sd")
Then select with expression. There align syntaxt according to your database.
val result = table.selectExpr("standardizationId","case when numberOfShares = 0 and isnull(derivatives,0) = 0 and holdingTypeId not in (3,10) then 8 else holdingTypeId end as holdingTypeId")
And show result
result.show
An alternative option, if it's wanted to avoid using the full string expression, is the following:
import org.apache.spark.sql.Column
import org.apache.spark.sql.functions._
val sd = sqlContext.table("sd")
val conditionedColumn: Column = when(
(sd("numberOfShares") === 0) and
(coalesce(sd("derivatives"), lit(0)) === 0) and
(!sd("holdingTypeId").isin(Seq(3,10): _*)), 8
).otherwise(sd("holdingTypeId")).as("holdingTypeId")
val result = sd.select(sd("standardizationId"), conditionedColumn)
I have the following UPDATE statement
UPDATE stuff
SET stuff.total = t.total
FROM (
SELECT SUM(price) FROM things WHERE stuff_id = ? GROUP BY stuff_id
) t
WHERE stuff.id = ?
This works fine when there are actually rows in things, but when not no UPDATE is executed (which I guess makes sense). What would be an elegant way to set stuff.total to 0 in that case? I'd like to do it in one query.
I already tried SET stuff.total = coalesce(t.total, 0) but it had no effect.
You haven't used coalesce in the right place. Also, GROUP BY can be omitted. Try this query:
UPDATE stuff
SET stuff.total = t.total
FROM (
SELECT coalesce(SUM(price), 0) FROM things WHERE stuff_id = ?) t
WHERE stuff.id = ?
I need to update all records that match my criteria. But the Sql below is giving this error:
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
-- Set MasterAccountId = NULL where there is no Receivable with equivalent BillingAccountId and TaskAccountId
UPDATE R
SET R.MasterAccountId = NULL
FROM Receivable R
WHERE EXISTS ( SELECT * FROM MasterAccount M
WHERE (ISNULL(M.BillingAccountId, 0) > 0 AND M.BillingAccountId = R.BillingAccountId) OR
(ISNULL(M.TaskAccountId, 0) > 0 AND M.TaskAccountId = R.TaskAccountId))
Basically, I need to update all records that return in that subquery.
Does any one know how to fix it?
Can you give a try on this. This is base from the repond of https://stackoverflow.com/users/40655/robin-day on this link How do I UPDATE from a SELECT in SQL Server?.
UPDATE
R
SET
R.MasterAccountId = NULL
FROM
Receivable R
INNER JOIN
MasterAccount M
ON
(ISNULL(M.BillingAccountId, 0) > 0 AND M.BillingAccountId = R.BillingAccountId) OR
(ISNULL(M.TaskAccountId, 0) > 0 AND M.TaskAccountId = R.TaskAccountId))
I don't think you are getting the said error in posted query May be somewhere else. Again in your EXISTS subquery, instead of saying select * ... it's always better to say WHERE EXISTS ( SELECT 1 FROM MasterAccount M
Also try using the JOIN version of this query instead like
UPDATE R
SET R.MasterAccountId = NULL
FROM Receivable R
JOIN MasterAccount M ON M.BillingAccountId = R.BillingAccountId
OR M.TaskAccountId = R.TaskAccountId
WHERE ISNULL(M.BillingAccountId, 0) > 0
OR ISNULL(M.TaskAccountId, 0) > 0;
Doing a simple Squeryl database lookup, but trying to exclude a value. I've tried:
j.id not jobExclude and j.id != jobExclude
however the first triggers a compiler error and the second triggers a runtime error.
The whole transaction:
from(DB.jobs)(j =>
where((j.startTime >= todayStart)
and (j.startTime <= todayEnd)
and (j.userId === userId)
and (j.teamId === teamId)
and (j.startOrder >= index)
and (j.id not jobExclude))
select (j)).toList
Thanks!
Courtesy of the Squeryl Groups:
Not equals is <>
so in the bigger picture:
(job.id <> jobExclude)
See http://squeryl.org/functions.html
Credit https://groups.google.com/forum/?fromgroups#!topic/squeryl/Hw7iVyvLLNM