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
Related
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.
I want to accomplish a join like this:
MyModel.joins(:other_model).
where(column: 123, other_model { other_column: column_in_my_model })
In practice, this would be something like
SELECT * FROM my_model m
INNER JOIN other_model o on m.column = o.foreign_key
WHERE m.column = 123 AND o.other_column = m.column_in_my_model
In the activerecord version, there's no way to refer to column_in_my_model that I know of without passing an SQL string. Is it possible to do this with activerecord syntax without passing a raw SQL string? Arel is OK too.
You can do something like this
MyModel.joins(:other_model)
.where(my_model_column: 123)
.where('column_in_my_model = other_model_column')
MyModel.find(:all,
:joins=>" JOIN other_model_table_name ON other_model_table_name.column = my_models.column",
:conditions=>"my_models.column = '123' AND other_model_table_name.column = my_models.column"
)
Assume I have an arbitrary number of Group records and I wanna query User record which has_many :groups, the catch is that users are queries by two bound fields from the groups table.
At the SQL level, I should end up with something like this:
SELECT * FROM users where (categories.id = 1 OR users.status = 0) OR(categories.id = 2 OR users.status = 1) ... -- to infinity
This is an example of what I came up with:
# Doesn't look like a good solution. Just for illustration.
or_query = groups.map do |g|
"(categories.id = #{g.category.id} AND users.status = #{g.user_status.id} )"
end.join('OR')
User.joins(:categories).where(or_query) # Works
What I think I should be doing is something along the lines of this:
# Better?
or_query = groups.map do |g|
"(categories.id = ? AND users.status = ? )".bind(g.category.id, g.user_status.id) #Fake method BTW
end.join('OR')
User.joins(:categories).where(or_query) # Works
How can I achieve this?
There has to be a better way, right?
I'm using Rails 4.2. So the shiny #or operator isn't supported for me.
I would collect the condition parameters separately into an array and pass that array (splatted, i.e. as an arguments list) to the where condition:
or_query_params = []
or_query = groups.map do |g|
or_query_params += [g.category_id, g.user_status.id]
"(categories.id = ? AND users.status = ?)"
end.join(' OR ')
User.joins(:categories).where(or_query, *or_query_params)
Alternatively, you might use ActiveRecord sanitization:
or_query = groups.map do |g|
"(categories.id = #{ActiveRecord::Base.sanitize(g.category_id)} AND users.status = #{ActiveRecord::Base.sanitize(g.user_status.id)})"
end.join(' OR ')
User.joins(:categories).where(or_query)
I'm able to update the custom-field of a jira-ticket by using the following code.
a = Jira4R::V2::RemoteFieldValue.new
a.id = "customfield_10000"
a.values = 'blah'
b = Jira4R::V2::RemoteFieldValue.new
b.id = "customfield_10022"
b.values = 'test'
ticket = jira.getIssue('blah-105')
jira.updateIssue(ticket.key, [a, b])
However, I am unable to update the description field using the same method.
Help would be appreciated.
finally found simple solution.
a = Jira4R::V2::RemoteFieldValue.new
a.id = "description" #id of description field
a.values = 'custom value'
ticket = jira.getIssue('TST-164')
jira.updateIssue(ticket.key, [a])
I have follow query
notes = Note.where('notes.id IN
(
SELECT "notes"."id" FROM "notes"
WHERE "notes"."circle_id" = ?
)
OR notes.id IN
(
SELECT "notes"."id" FROM "notes"
INNER JOIN "circles_dreams"
ON "circles_dreams"."dream_id" = "notes"."dream_id"
WHERE "circles_dreams"."circle_id" = ?
)', #circle.id, #circle.id)
How to simplify this query?
Thanks.
First of all you can collect all needed notes id.
I supposed to think what you already have relations between Note and CirclesDream
note_ids = Note.where(circle_id: #circle.id).pluck(:id) # your first SELECT
dream_ids = CirclesDream.where(id: #circle.id).pluck(:note_id) # your second SELECT
notes_ids = note_ids | dreams_ids # combine them
notes = Note.where(id: notes_ids) # Now your
upd: I've just fixed typo. Changed id to note_id in second request
Try this
note_ids = Note.where('circle_id = ?', #circle.id).pluck(:id)
dream_note_ids = Note.joins(:circle_dreams).where('circle_dreams.circle_id = ?', #circle.id).plunk(:note_id)
notes_ids = note_ids | dream_note_ids
If your circle_dreams table can contain records having note_id = null, then you have to apply join. So i think this will work in your case....