Rails Mysql2::Error: Operand should contain 1 column(s) - sql

I wanted to take this method:
self.spend_on_scholarships_for_african_people = training_programs.scholarships.joins(:participants).where('participants.race' => AFRICAN_RACES).sum('participants.spend')
and now want break it into different genders. We capture gender within the participants table. so i tried using active records array conditions within the query to do so like this:
self.bursary_spend_on_potential_female_employees = training_programs.scholarships.joins(:participants).where('participants.race = ? AND participants.gender = ?', AFRICAN_RACES, 'Female').sum('participants.spend')
self.bursary_spend_on_potential_male_employees = training_programs.scholarships.joins(:participants).where('participants.race = ? AND participants.gender = ?', AFRICAN_RACES, 'Male').sum('participants.spend')
But i keep getting the following error:
ActiveRecord::StatementInvalid - Mysql2::Error: Operand should contain 1 column(s): SELECT SUM(participants.spend) AS sum_id FROM `training_programs` INNER JOIN `participants` ON `participants`.`training_program_id` = `training_programs`.`id` WHERE `training_programs`.`skills_development_id` IS NULL AND `training_programs`.`skills_development_type` = 'SkillsDevelopment' AND `training_programs`.`scholarship` = 1 AND (participants.race = 'African','Coloured','Indian' AND participants.gender = 'Female'):
I have cut up my query and i can't seem to find what i have done wrong?

participants.race = 'African','Coloured','Indian'
should be
participants.race IN ('African','Coloured','Indian')
To get that SQL result, change the way you build your query to something like:
participants.race IN (?)

Related

Redshift - ERROR: Target table must be part of an equijoin predicate

I am trying to make an update on a temporal table I created in Redshift. The code I am trying to run goes like this:
UPDATE #new_emp
SET rpt_to_emp_id = CAST(ht.se_value AS INTEGER),
rpt_to_extrnl_email = ht.extrnl_email_addr,
rpt_to_fst_nm = ht.first_nm,
rpt_to_lst_nm = ht.last_nm,
rpt_to_mdl_init = ht.mdl_nm,
rpt_to_nm = ht.full_nm,
rpt_to_ssn = CAST(ht.ssn AS INTEGER),
FROM #new_emp,
(SELECT DISTINCT t.se_value,h.first_nm,h.last_nm,
h.mdl_nm,h.full_nm,h.ssn,h.extrnl_email_addr
FROM spec_hr.dtbl_translate_codes_dw t, spec_hr.emp_hron h
WHERE t.inf_name = 'system'
AND t.fld_name = 'HRONDirector'
AND h.foreign_emp_id = t.se_value
) ht
WHERE #new_emp.foreign_emp_id <> ht.se_value
AND (#new_emp.emp_status_cd <> 'T'
AND (#new_emp.ult_rpt_emp_id = #new_emp.foreign_emp_id
OR #new_emp.ult_rpt_emp_id = #new_emp.psoft_id
OR #new_emp.ult_rpt_emp_id IS NULL));
I've tried both with and without specyfing the updated table from the FROM command. But it keeps throwing me this error:
ERROR: Target table must be part of an equijoin predicate
Any ideas why is this failing? Thank you!
Redshift needs an equality join condition to know what value to update and with which values. Your join condition is "#new_emp.foreign_emp_id <> ht.se_value" which is an inequality or Redshift speak - this is not "an equijoin predicate". You have a SET of "rpt_to_lst_nm = ht.last_nm" but if the only join condition is an inequality then which value of last_nm is Redshift putting in the table?
To put it the other way around - you need to tell Redshift exactly which rows of the target table are receiving which values (equijoin). The join condition you have doesn't meet this requirement.

How to dynamic, handle nested WHERE AND/OR queries using Rails and SQL

I'm currently building a feature that requires me to loop over an hash, and for each key in the hash, dynamically modify an SQL query.
The actual SQL query should look something like this:
select * from space_dates d
inner join space_prices p on p.space_date_id = d.id
where d.space_id = ?
and d.date between ? and ?
and (
(p.price_type = 'monthly' and p.price_cents <> 9360) or
(p.price_type = 'daily' and p.price_cents <> 66198) or
(p.price_type = 'hourly' and p.price_cents <> 66198) # This part should be added in dynamically
)
The last and query is to be added dynamically, as you can see, I basically need only one of the conditions to be true but not all.
query = space.dates
.joins(:price)
.where('date between ? and ?', start_date, end_date)
# We are looping over the rails enum (hash) and getting the key for each key value pair, alongside the index
SpacePrice.price_types.each_with_index do |(price_type, _), index|
amount_cents = space.send("#{price_type}_price").price_cents
query = if index.positive? # It's not the first item so we want to chain it as an 'OR'
query.or(
space.dates
.joins(:price)
.where('space_prices.price_type = ?', price_type)
.where('space_prices.price_cents <> ?', amount_cents)
)
else
query # It's the first item, chain it as an and
.where('space_prices.price_type = ?', price_type)
.where('space_prices.price_cents <> ?', amount_cents)
end
end
The output of this in rails is:
SELECT "space_dates".* FROM "space_dates"
INNER JOIN "space_prices" ON "space_prices"."space_date_id" = "space_dates"."id"
WHERE "space_dates"."space_id" = $1 AND (
(
(date between '2020-06-11' and '2020-06-11') AND
(space_prices.price_type = 'hourly') AND (space_prices.price_cents <> 9360) OR
(space_prices.price_type = 'daily') AND (space_prices.price_cents <> 66198)) OR
(space_prices.price_type = 'monthly') AND (space_prices.price_cents <> 5500)
) LIMIT $2
Which isn't as expected. I need to wrap the last few lines in another set of round brackets in order to produce the same output. I'm not sure how to go about this using ActiveRecord.
It's not possible for me to use find_by_sql since this would be dynamically generated SQL too.
So, I managed to solve this in about an hour using Arel with rails
dt = SpaceDate.arel_table
pt = SpacePrice.arel_table
combined_clauses = SpacePrice.price_types.map do |price_type, _|
amount_cents = space.send("#{price_type}_price").price_cents
pt[:price_type]
.eq(price_type)
.and(pt[:price_cents].not_eq(amount_cents))
end.reduce(&:or)
space.dates
.joins(:price)
.where(dt[:date].between(start_date..end_date).and(combined_clauses))
end
And the SQL output is:
SELECT "space_dates".* FROM "space_dates"
INNER JOIN "space_prices" ON "space_prices"."space_date_id" = "space_dates"."id"
WHERE "space_dates"."space_id" = $1
AND "space_dates"."date" BETWEEN '2020-06-11' AND '2020-06-15'
AND (
("space_prices"."price_type" = 'hourly'
AND "space_prices"."price_cents" != 9360
OR "space_prices"."price_type" = 'daily'
AND "space_prices"."price_cents" != 66198)
OR "space_prices"."price_type" = 'monthly'
AND "space_prices"."price_cents" != 5500
) LIMIT $2
What I ended up doing was:
Creating an array of clauses based on the enum key and the price_cents
Reduced the clauses and joined them using or
Added this to the main query with an and operator and the combined_clauses

getting error The multi-part identifier "e.EpVisitCount" could not be bound. in sql server

I am working on converting mysql query to sql server, but when i run the query it gives me error The multi-part identifier "e.EpVisitCount" could not be bound. , here i have posted my both query can you please look into it, why it gives me error in sql server ?
Mysql Query(Working Fine) :
UPDATE
tb_EpVisitRange as v left JOIN tb_Episode as e ON (v.company_id = e.CustID) AND (v.CMW = e.CMW)
SET
e.EpVisitCount = If(PayerType='NonEp',0,If(LUPA=1,0,v.High)),
e.VisitAlert = If( e.TotVisits > v.High,1,NULL)
where UploadID = '23'
SQl Query(Getting Error) :
UPDATE v
SET
e.EpVisitCount = IIF(PayerType='NonEp',0,IIF(LUPA=1,0,v.High)),
e.VisitAlert = IIF( e.TotVisits > v.High,1,NULL)
FROM tb_EpVisitRange v
JOIN tb_Episode as e ON (v.company_id = e.CustID) AND (v.CMW = e.CMW)
where UploadID = '613'
You seem to want to update e rather than v. So, you might try:
UPDATE e
SET EpVisitCount = (CASE WHEN PayerType = 'NonEp' THEN 0
WHEN LUPA = 1 THEN 0
ELSE v.High
END),
VisitAlert = (CASE WHEN e.TotVisits > v.High THEN 1 END)
FROM tb_Episode e JOIN
tb_EpVisitRang v
ON v.company_id = e.CustID AND v.CMW = e.CMW
WHERE UploadID = '613';
Notes:
The problem appears to be the table alias used for the update.
SQL Server only allows updating one table in a statement. There is no need to qualify the column names for the SET.
Use CASE for conditions. It is the ANSI standard and supported by almost all database.
This is especially true for nested expressions. CASE supports multiple conditions.
You are updating e, so I made that the first table in the FROM clause. I find that logic easier to follow.
You should qualify UploadId.

How can I write a following sql in rails?

update table_name set filename = CONCAT(filename, "test") where id = 111
How can I write the above SQL with ActiveRecord syntax in Rails ?
If you have model associated with given table, you can do:
m = Model.find(111)
m.update_column(:filename, m.filename + 'test')
If you want to to update multiple records:
Model.update_all('filename = CONCAT(filename, "test")')
or limit scope of elements with conditions:
Model.where(...).update_all(...)

django using .extra() got error `only a single result allowed for a SELECT that is part of an expression`

I'm trying to use .extra() where the query return more than 1 result, like :
'SELECT "books_books"."*" FROM "books_books" WHERE "books_books"."owner_id" = %s' % request.user.id
I got an error : only a single result allowed for a SELECT that is part of an expression
Try it on dev-server using sqlite3. Anybody knows how to fix this? Or my query is wrong?
EDIT:
I'm using django-simple-ratings, my model like this :
class Thread(models.Model):
#
#
ratings = Ratings()
I want to display each Thread's ratings and whether a user already rated it or not. For 2 items, it will hit 6 times, 1 for the actual Thread and 2 for accessing the ratings. The query:
threads = Thread.ratings.order_by_rating().filter(section = section)\
.select_related('creator')\
.prefetch_related('replies')
threads = threads.extra(select = dict(myratings = "SELECT SUM('section_threadrating'.'score') AS 'agg' FROM 'section_threadrating' WHERE 'section_threadrating'.'content_object_id' = 'section_thread'.'id' ",)
Then i can print each Thread's ratings without hitting the db more. For the 2nd query, i add :
#continue from extra
blahblah.extra(select = dict(myratings = '#####code above####',
voter_id = "SELECT 'section_threadrating'.'user_id' FROM 'section_threadrating' WHERE ('section_threadrating'.'content_object_id' = 'section_thread'.'id' AND 'section_threadrating'.'user_id' = '3') "))
Hard-coded the user_id. Then when i use it on template like this :
{% ifequal threads.voter_id user.id %}
#the rest of the code
I got an error : only a single result allowed for a SELECT that is part of an expression
Let me know if it's not clear enough.
The problem is in the query. Generally, when you are writing subqueries, they must return only 1 result. So a subquery like the one voter_id:
select ..., (select sectio_threadrating.user_id from ...) as voter_id from ....
is invalid, because it can return more than one result. If you are sure it will always return one result, you can use the max() or min() aggregation function:
blahblah.extra(select = dict(myratings = '#####code above####',
voter_id = "SELECT max('section_threadrating'.'user_id') FROM 'section_threadrating' WHERE ('section_threadrating'.'content_object_id' = 'section_thread'.'id' AND 'section_threadrating'.'user_id' = '3') "))
This will make the subquery always return 1 result.
Removing that hard-code, what user_id are you expecting to retrieve here? Maybe you just can't reduce to 1 user using only SQL.