.sum data from query - ruby-on-rails

I'm trying to .sum a field from the following query:
def self.busqueda_general(params)
query = select('venta.Id,venta.TOTAL')
.distinct
.joins('left outer join detallevet ON venta.Documento=detallevet.Docto and venta.RutaId=detallevet.RutaId')
.where("(venta.RutaId = :rutaId or :rutaId = '') AND (detallevet.Articulo = :articulo or :articulo = '') AND (venta.CodCliente = :codcliente or :codcliente = '') AND (venta.IdEmpresa = :idempresa)",{rutaId: params[:search], articulo: params[:search3], codcliente: params[:search2], idempresa: params[:search6]})
query = query.where('venta.Fecha >= ? AND venta.Fecha <= ?', (params[:search4].to_date.beginning_of_day).strftime('%Y-%m-%d %T'), (params[:search5].to_date.end_of_day).strftime('%Y-%m-%d %T')) if params[:search4].present? and params[:search5].present?
query
end
In the method of the controller I call the query and sum it as follows:
#monto_total = Vent.busqueda_general(params).sum(:TOTAL)
but the problem is that the query is showing me records that are not repeated thanks to .distinct but with the .sum is adding up all the records including the repeated ones, ignoring the .distinct

Try this
#monto_total = Vent.busqueda_general(params).sum(:TOTAL).to_f
you can to use to_s (to convert in string) to_f -> float, to_i -> integer

You have a group by clause, so you get one sum(Total) for every combination of ID/Total.
.group('venta.Id,venta.TOTAL')
To me looks like you don't need this group by clause.
UPDATE::
query = where(Id: select('venta.Id')
.joins('left outer join detallevet ON venta.Documento=detallevet.Docto and venta.RutaId=detallevet.RutaId')
.where("(venta.RutaId = :rutaId or :rutaId = '') AND (detallevet.Articulo = :articulo or :articulo = '') AND (venta.CodCliente = :codcliente or :codcliente = '') AND (venta.IdEmpresa = :idempresa)",{rutaId: params[:search], articulo: params[:search3], codcliente: params[:search2], idempresa: params[:search6]}))
Change your main query to this keeping everything else same. Ideal way would be to move join on detallevet to where clause.

Related

sum total column of a table

I have a this query in rails:
def self.nominas(params)
query = select("payroll_employees.*,payroll_employees.total, concat(e.name, ' ', e.surname, ' ', e.other_surname) as nombre_empleado, ec.code as contrato, CASE WHEN payroll_employees.is_wage_integral = TRUE THEN 'Si' WHEN payroll_employees.is_wage_integral = FALSE THEN 'No' END AS salario_es_integral, pc.integral_wage AS p_c_integral_wage")
query = query.joins("inner join payrolls p on (p.id = payroll_employees.payroll_id) inner join employee_contracts ec on (ec.id = payroll_employees.employee_contract_id) inner join employees e on (e.id = ec.employee_id) inner join payroll_companies pc on (pc.company_id = p.company_id) ")
query = query.where('p.id = :nomina', {nomina: params[:id] })
query = query.group(:id, 'e.name', 'e.surname', 'e.other_surname', 'ec.code', 'pc.integral_wage')
query = query.having("(lower(concat(e.name, ' ', e.surname, ' ', e.other_surname)) LIKE :campo_busqueda or :campo_busqueda = '') OR (lower(ec.code) LIKE :campo_busqueda or :campo_busqueda = '')", {campo_busqueda: "%#{params[:buscador].try(:downcase)}%"})
query = query.order('nombre_empleado')
end
in which I have a column "total", and I need to do the summation of the column "total" of all the records that the query brings me, my problem comes at the time of doing this:
#payroll_employees = PayrollEmployee.nominas(params)####
#sum_total = #payroll_employees.sum(:total)
it brings me something like this:
{[345, "Angel", "BONILLA", "MONTAÑO", "2010", true]=>0.106215575500000000000001e7, [079, "Bill f", "CABRERA", "RICO", "1846", true]=>0.1330346e7, ...
it seems to me that it is because my query has a group. Is it possible to do the summation and get a single number instead of a grouped array of totals?
Try to use sum function on total sum(payroll_employees.total)
def self.nominas(params)
query = select("payroll_employees.*,sum(payroll_employees.total) as total, concat(e.name, ' ', e.surname, ' ', e.other_surname) as nombre_empleado, ec.code as contrato, CASE WHEN payroll_employees.is_wage_integral = TRUE THEN 'Si' WHEN payroll_employees.is_wage_integral = FALSE THEN 'No' END AS salario_es_integral, pc.integral_wage AS p_c_integral_wage")
query = query.joins("inner join payrolls p on (p.id = payroll_employees.payroll_id) inner join employee_contracts ec on (ec.id = payroll_employees.employee_contract_id) inner join employees e on (e.id = ec.employee_id) inner join payroll_companies pc on (pc.company_id = p.company_id) ")
query = query.where('p.id = :nomina', {nomina: params[:id] })
query = query.group(:id, 'e.name', 'e.surname', 'e.other_surname', 'ec.code', 'pc.integral_wage')
query = query.having("(lower(concat(e.name, ' ', e.surname, ' ', e.other_surname)) LIKE :campo_busqueda or :campo_busqueda = '') OR (lower(ec.code) LIKE :campo_busqueda or :campo_busqueda = '')", {campo_busqueda: "%#{params[:buscador].try(:downcase)}%"})
query = query.order('nombre_empleado')
end

Chaining multiple ActiveRecord `or` queries

I've got an array of columns that I want to loop through and optionally chain an or query onto an ActiveRecord query chain. I can get it to work, but the resulting query appends the or onto the query chain, therefore making the columns in my inital query optional. Here's my class:
class Claim
class MatchingAttributeFinder
ATTRIBUTE_GROUPS_TO_MATCH = [
["teacher_reference_number"],
["email_address"],
["national_insurance_number"],
["bank_account_number", "bank_sort_code", "building_society_roll_number"],
].freeze
def initialize(source_claim, claims_to_compare = Claim.submitted)
#source_claim = source_claim
#claims_to_compare = claims_to_compare
end
def matching_claims
claims = #claims_to_compare.where.not(id: #source_claim.id)
ATTRIBUTE_GROUPS_TO_MATCH.each do |attributes|
vals = values_for_attributes(attributes)
next if vals.blank?
concatenated_columns = "CONCAT(#{attributes.join(",")})"
claims = claims.or(
Claim.where("LOWER(#{concatenated_columns}) = LOWER(?)", vals.join)
)
end
claims
end
private
def values_for_attributes(attributes)
attributes.map { |attribute|
#source_claim.read_attribute(attribute)
}.reject(&:blank?)
end
end
end
The generated SQL looks like this:
SELECT "claims".* FROM "claims" WHERE (((("claims"."submitted_at" IS NOT NULL AND "claims"."id" != 'a7b25b99-4477-42b1-96ab-8262582c5541' OR (LOWER(CONCAT(teacher_reference_number)) = LOWER('0902344'))) OR (LOWER(CONCAT(email_address)) = LOWER('genghis.khan#mongol-empire.com'))) OR (LOWER(CONCAT(national_insurance_number)) = LOWER('QQ891011C'))) OR (LOWER(CONCAT(bank_account_number,bank_sort_code,building_society_roll_number)) = LOWER('34682151972654123456789/ABCD')))
But what I actually want is more like this:
SELECT "claims".* FROM "claims" WHERE "claims"."submitted_at" IS NOT NULL AND "claims"."id" != 'd6a53b4d-c569-49e6-a2ea-ac44b69b0451' AND (LOWER(concat(teacher_reference_number)) = LOWER('0902344') OR LOWER(concat(email_address)) = LOWER('genghis.khan#mongol-empire.com') OR LOWER(concat(national_insurance_number)) = LOWER('QQ891011C') OR LOWER(concat(bank_account_number,bank_sort_code,building_society_roll_number)) = LOWER('34682151972654123456789/ABCD'))
Is there any way to set up something like an empty scope that I can chain my OR queries to?
Try chaning all the "or" together first and then chain the original query
def matching_claims
claims = #claims_to_compare.where.not(id: #source_claim.id)
ors = nil
ATTRIBUTE_GROUPS_TO_MATCH.each do |attributes|
vals = values_for_attributes(attributes)
next if vals.blank?
concatenated_columns = "CONCAT(#{attributes.join(",")})"
aux = Claim.where("LOWER(#{concatenated_columns}) = LOWER(?)", vals.join)
if ors.nil?
ors = aux
else
ors = ors.or(aux)
end
end
claims.merge(ors)
end

ActiveRecord : ordering the output of a where request

I'm trying to order the output of a where ActiveRecord query :
result = Class.where('a = ? AND b = ?', params[:a], params[:b])
I tried chaining order both before and after without succeeding, what am I missing ?
#Not working, the order is not modified compared to previous line
result = Class.where('a = ? AND b = ?', params[:a], params[:b]).order('c DESC')
Try to unscope the model, something like
result = Class.unscoped.where('a = ? AND b = ?', params[:a], params[:b]).order('c DESC')
Or delete the default scope if it is not used elsewhere

how to convert sql query in ruby and rails?

SELECT A.FirstName, A.LastName, B.PatientId, B.RoomNumber, B.AdmissionDate, B.DischargeDate, B.MeasureCategory
FROM DimPatient A, DimPatientStay B
WHERE A.Id = B.PatientId AND A.FirstName = 'Anuj' AND B.MeasureCategory = 'ED'
hi some updation for this
i solved this prob by
MODELNAME.find_by_sql("your sql query")
You can try this to find the result from sql query in Rails
query_params = Hash.new
sql_query = "SELECT A.FirstName, A.LastName, B.PatientId, B.RoomNumber, B.AdmissionDate, B.DischargeDate, B.MeasureCategory
FROM DimPatient A, DimPatientStay B
WHERE A.Id = B.PatientId AND A.FirstName = :first_name AND B.MeasureCategory = :measure_category"
query_params[:first_name] = first_name
query_params[:measure_category] = measure_category
#query_results = ActiveRecord::Base.connection.select_all(
ActiveRecord::Base.send("sanitize_sql_array",[sql_query, query_params] )
)
I guess you could try:
ActiveRecord::Base.connection().execute(#your_sql_here)
Suppose A is one class and B is another, you should use includes as following:
A.includes(:b).where(...) # add you condition in where
I suggest to check good video tutorials of ActiveRecord here

sql distinct statement and use of more than one table on rails

How would you write this line in the "rails way"?
unique_attendees = CourseSessionsUser.count_by_sql(["SELECT count(DISTINCT csu.user_id) AS count_user_id FROM course_sessions_users csu, course_sessions cs WHERE cs.course_id = ? AND csu.course_session_id = cs.id"], #course.id)
The query itself is:
SELECT count(DISTINCT csu.user_id) AS count_user_id
FROM course_sessions_users csu,
course_sessions cs
WHERE cs.course_id = ?
AND csu.course_session_id = cs.id
Use count method of the rails
count = CourseSessionsUser.count('csu.user_id',
:distinct => true, :conditions => ["cs.course_id = ?", #course.id]
:joins => "cs LEFT JOIN course_sessions_users csu ON cs.id = csu.course_session_id")
This will return directly non zero integer if condition matches otherwise return zero

Resources