PDA

Ver la Versión Completa : Como obtener equivalente de Last en Firebird


marcoszorrilla
30-11-2012, 11:05:53
Tengo este SQL en Firebird 2.5, y me piden añadir el último precio, pero observo que no existe la instrucción Last.

procedure TfrPcons1Cli.btAceptarClick(Sender: TObject);
begin
IbDtsV.Close;
IbDtsV.SelectSQL.Clear;
IbDtsV.SelectSQL.Add('Select Max(Codclient) as Codclient, Max(Fecha) as Fecha, ');
IbDtsV.SelectSQL.Add('Producto, Sum(Unidades) as Unidades,');
IbDtsV.SelectSQL.Add('Max(Precio) as Precio, Sum(Subtotal) as Subtotal, ');
IbDtsV.SelectSQL.Add('Avg(Precio) as PMedio, Min(Precio) as MPrecio');
IbDtsV.SelectSQL.Add('from LineasVentas');
IbDtsV.SelectSQL.Add('Where Codclient = :C');
IbDtsV.SelectSQL.Add('And Fecha between :F1 and :F2');
IbDtsV.SelectSQL.Add('Group By Producto');
IbDtsV.SelectSQL.Add('Having Sum(Unidades) <> 0');
IbDtsV.SelectSQL.Add('Order by Producto');

IbDtsV.ParamByName('C').AsString:=IbDTSCliCodigo.Value;
IbDtsV.ParamByName('F1').AsDate:=DateOf(DtpkIni.Date);
IbDtsV.ParamByName('F2').AsDate:=DateOf(DtpkFin.Date);
IbDtsV.open;
end;

Quisiera:
procedure TfrPcons1Cli.btAceptarClick(Sender: TObject);
begin
IbDtsV.Close;
IbDtsV.SelectSQL.Clear;
IbDtsV.SelectSQL.Add('Select Max(Codclient) as Codclient, Max(Fecha) as Fecha, ');
IbDtsV.SelectSQL.Add('Producto, Sum(Unidades) as Unidades,');
IbDtsV.SelectSQL.Add('Max(Precio) as Precio, Sum(Subtotal) as Subtotal, ');
IbDtsV.SelectSQL.Add('Avg(Precio) as PMedio, Min(Precio) as MPrecio,');
IbDtsV.SelectSQL.Add('Last(Precio) as UltimoP');
IbDtsV.SelectSQL.Add('from LineasVentas');
IbDtsV.SelectSQL.Add('Where Codclient = :C');
IbDtsV.SelectSQL.Add('And Fecha between :F1 and :F2');
IbDtsV.SelectSQL.Add('Group By Producto');
IbDtsV.SelectSQL.Add('Having Sum(Unidades) <> 0');
IbDtsV.SelectSQL.Add('Order by Producto');

IbDtsV.ParamByName('C').AsString:=IbDTSCliCodigo.Value;
IbDtsV.ParamByName('F1').AsDate:=DateOf(DtpkIni.Date);
IbDtsV.ParamByName('F2').AsDate:=DateOf(DtpkFin.Date);
IbDtsV.open;
end;

Un Saludo.

Casimiro Notevi
30-11-2012, 12:41:03
'Last' es ir al último registro en un dataset.
¿A qué te refieres exactamente con último precio?, ¿el último precio al que se le vendió a un cliente?, para eso tendrás que buscar su última factura donde está el artículo que buscas y extraer de ahí el precio del artículo, no creo que exista eso de 'Last' para lo que pretendes hacer.

marcoszorrilla
30-11-2012, 13:15:09
Ésta instrucción existe por ejemplo en Access.

SELECT Diario.SUBCTA, Sum(Diario.EURODEBE) AS SumaDeEURODEBE, Last(Diario.FECHA) AS ÚltimoDeFECHA
FROM Diario
GROUP BY Diario.SUBCTA;

Un Saludo.

Casimiro Notevi
30-11-2012, 13:38:22
Por eso te pregunto, ¿qué quieres hacer exactamente?, para "traducirlo" a firebird :)

marcoszorrilla
30-11-2012, 13:44:42
Obtener el último precio al que se ha vendido un producto.

Un Saludo.

Casimiro Notevi
30-11-2012, 13:51:07
Obtener el último precio al que se ha vendido un producto.
Ya, eso lo supongo, marcos :)
Pregunto que dónde está ese precio, es que nunca he usado access, ese Last(Diario.Fecha), ¿Diario es una tabla y Fecha es un campo?
Y qué devuelve ¿el último registro de esa tabla?, ¿el de mayor fecha?, ¿el último precio que se le vendió a ese cliente o el último precio que se vendió a cualquier cliente?.

En lugar de Last(Diario.Fecha), puedes hacer un (select ..........), pero para responderte necesito saber lo preguntado antes :)

marcoszorrilla
30-11-2012, 15:02:18
La consulta que quiero hacer es la segunda del mensaje de inicio, la parte que está en color rojo, esa línea es la que quiero añadir la base de datos es Firebird, el ejemplo abreviado en Access es lo que quiero hacer.

La tabla se llama LineasVentas y entre otros contien los campos, fecha, codigoproducto, producto, Unidades, Precio...

Lo tengo funcionando, lo que es la primera consulta que figura en el mensaje de inicio de este hilo y lo que quiero obtener es la segunda, pero me encuentro con que Firebird no posee la instrucción Last.

Un Saludo.

egostar
30-11-2012, 16:04:43
Hola

Yo estoy de acuerdo con Casimiro, hay que obtener el precio de la última fecha. A ver si esto funciona para lo que deseas.

Select
Max(Codclient) as Codclient,
Max(Fecha) as Fecha,
Producto,
Sum(Unidades) as Unidades,
Max(Precio) as Precio,
Sum(Subtotal) as Subtotal,
Avg(Precio) as PMedio,
Min(Precio) as MPrecio,
(select first 1 X.precio from LineasVentas X where X.Codclient = :C And X.Fecha between :F1 and :F2 order by X.fecha desc) as UltimoP

from LineasVentas
Where Codclient = :C And Fecha between :F1 and :F2
Group By Producto
Having Sum(Unidades) <> 0
Order by Producto

Saludos

RONPABLO
30-11-2012, 17:15:58
Iba a decir algo, pero egostar se me adelanto :P :D

marcoszorrilla
01-12-2012, 13:53:40
Gracias a todos, el lunes haré la prueba y os digo como resultó.


Un Saludo.

egostar
01-12-2012, 20:35:10
Ahora que lo veo nuevamente, me parece que me ha faltado un detalle importante.

Select
Max(Codclient) as Codclient,
Max(Fecha) as Fecha,
Producto,
Sum(Unidades) as Unidades,
Max(Precio) as Precio,
Sum(Subtotal) as Subtotal,
Avg(Precio) as PMedio,
Min(Precio) as MPrecio,
(select first 1 X.precio from LineasVentas X
where X.Codclient = :C And
X.Fecha between :F1 and :F2 And
X.Producto = Producto
order by X.fecha desc) as UltimoP

from LineasVentas
Where Codclient = :C And Fecha between :F1 and :F2
Group By Producto
Having Sum(Unidades) <> 0
Order by Producto


Saludos