在工会中使用工会

时间:2018-06-26 20:10:31

标签: ruby-on-rails ruby ruby-on-rails-4

是否可以在滑轨中使用union all?我正在整理一个查询,但是我需要使用union all,有什么方法可以使其适应Rails吗?这是我的查询:

select sum(DV.Importe + DV.iva) 
from venta INNER JOIN DetalleVet DV ON DV.RutaId = [venta].RutaID AND DV.IdEmpresa = [venta].IdEmpresa AND DV.Docto = [venta].Documento 
where venta.Rutaid=8 and diao=611 and venta.TipoVta='Contado'
union all 
select SUM(DP.Importe + DP.Iva) from pedidosliberados PL inner join detallepedidoliberado DP on PL.PEDIDO=DP.PEDIDO and PL.RUTA=DP.RUTA and  PL.IDEMPRESA=DP.idempresa
where  PL.cancelada=0 and  PL.status=5 and PL.Tipo='Contado' and PL.Ruta=8 and PL.DiaO_entrega=611

1 个答案:

答案 0 :(得分:1)

使查询适应Rails的最简单方法可能是使用http://localhost:8080/index方法。看起来像这样:

  

Model.find_by_sql

因此翻译您的查询可能看起来像这样。

find_by_sql

或者,如果您没有模型,而只想执行原始SQL,则ActiveRecord会提供一个名为Client.find_by_sql(" SELECT Sum(DV.importe + DV.iva) FROM venta INNER JOIN detallevet DV ON DV.rutaid = [venta].rutaid AND DV.idempresa = [venta].idempresa AND DV.docto = [venta].documento WHERE venta.rutaid = 8 AND diao = 611 AND venta.tipovta = 'Contado' UNION ALL SELECT Sum(DP.importe + DP.iva) FROM pedidosliberados PL INNER JOIN detallepedidoliberado DP ON PL.pedido = DP.pedido AND PL.ruta = DP.ruta AND PL.idempresa = DP.idempresa WHERE PL.cancelada = 0 AND PL.status = 5 AND PL.tipo = 'Contado' AND PL.ruta = 8 AND PL.diao_entrega = 611 ") # => [<Client id: 1, sum: 22 >, <Client id: 2, sum: 44>...] 的简洁帮助函数。

execute

参考here