avoid duplicate records in query and date correction

1

I have two small problems with a query

1) I am conducting a search of the clients related to the logs, this query should bring me those bitacoratime logs where the code is equal to the id of the clients, the problem is that the clients should not repeat themselves, if there is 3 logs with the same client I want to only take the first record, try to do it with .distinct but it does not work that way.

2) The next error is that in the comparison between dates because it is a datetime if I search between the date 2014-06-28 and the 2014-06-30 and there is a record of the following form 2014-06- 30 13:06:20 will not take it into account in the consultation because it is in a higher range due to the time it has. How could I fix it?

  def self.total_clientes(params)
   query =  joins("left outer join clientes on bitacoratiempos.Codigo=clientes.IdCli")
                      .where("(bitacoratiempos.RutaId = :rutaId or :rutaId = '') AND (bitacoratiempos.IdEmpresa = :idempresa)",{rutaId: params[:search], idempresa: params[:search0]})
                      .distinct
  query = query.where('bitacoratiempos.HI between ? AND ?', (params[:search1].to_date).strftime('%Y-%m-%d'), (params[:search2].to_date).strftime('%Y-%m-%d')) if params[:search1].present? and params[:search2].present?
  query
  end
    
asked by LuisC 23.12.2016 в 01:26
source

1 answer

0

I do not know the language but to avoid that you can add to your query

SELECT MIN(id_cliente) FROM bitacoras where id_cliente = 1 (por ejemplo)

The second error is easy to solve, add one day to the second date. So if up to (search2) is 2014-01-10 and you add one it will be 2014-01-11 and it will take all the hours until 2014-01-10 23:59

    
answered by 23.12.2016 в 02:30