FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour calcular stock dentro de un select con funciones de usuario
Posts: 400
Joined: Fri May 11, 2007 08:20 PM
calcular stock dentro de un select con funciones de usuario
Posted: Sat Mar 16, 2019 12:11 AM
Gente:
Estoy migrando de dbf a sql y hasta ahora voy avanzando pero ahora tengo que mostrar el stock de art铆culos, mi tabla de art铆culos esta dise帽ada
asi:
[img]http://s2.subirimagenes.com/otros/previo/thump_9890276select-de-articulo.jpg" alt="" loading="lazy">
[/img]

la tabla detguiat es una tabla transaccional que contiene los movimientos y esta relacionada con la tabla art铆culos para mostrar el stock:
Code (fw): Select all Collapse
聽 聽select detguiat
聽 聽SET RELATION TO detguiat->articu INTO articulo additive

聽 聽REDEFINE XBROWSE oBrwDet 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ; 聽 聽 聽 聽
聽 聽 聽 DATASOURCE detguiat 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ;
聽 聽 聽 COLUMNS "articu", "Articulo->DesG","Articulo->UM","Articulo->Cap","Cantid" 聽 聽 聽 聽 聽 聽 聽 ;
聽 聽 聽 聽 聽 聽 ,"salant('articulo')+ingmes('articulo')-salmes('articulo')-cantid" ;
聽 聽 聽 聽 聽 聽 ,"PRECIO" 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ;
聽 聽 聽 FIELDSIZES 65,195,30,30,40,80,40 聽 聽 聽 聽 聽 聽 聽 聽 ;
聽 聽 聽 HEADERS "CODIGO" 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ;
聽 聽 聽 聽 聽 聽 聽,"DESCRIPCI脫N" 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽;
聽 聽 聽 聽 聽 聽 聽,"U.M." 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ;
聽 聽 聽 聽 聽 聽 聽,"CAP" 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽;
聽 聽 聽 聽 聽 聽 聽,"CANT." 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽;
聽 聽 聽 聽 聽 聽 聽,"STOCK" 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽;
聽 聽 聽 聽 聽 聽 聽,"PRECIO"


y estas son las definiciones de las funciones que invoco en la definici贸n del xbrowse

Code (fw): Select all Collapse
聽 聽 FUNCTION INGANO(cAlias, nMes) // devuelve la suma todo los ingresos hasta el mes que le indique en el parametro
聽 聽 聽 MEMVAR mMes
聽 聽 聽 LOCAL nSuma := 0, i
聽 聽 聽 LOCAL cMes
聽 聽 聽 DEFAULT nMes 聽 := mMes
聽 聽 聽 nMes -= 1
聽 聽 聽 FOR i = 0 to nMes
聽 聽 聽 聽 聽 cMes := 'CONING'+STRZERO(i,2)
聽 聽 聽 聽 聽 nSuma += (cAlias)->&cMes
聽 聽 聽 NEXT
聽 聽 Return(nSuma)
/* --------------------------------------------------------- */
聽 聽 FUNCTION SALANO(cAlias, nMes) //devuelve la suma de todas las salidas hasta el mes que le indique en el parametro
聽 聽 聽 MEMVAR mMes
聽 聽 聽 LOCAL nSuma := 0, i
聽 聽 聽 LOCAL cMes
聽 聽 聽 DEFAULT nMes 聽 := mMes
聽 聽 聽 nMes -= 1
聽 聽 聽 FOR i = 0 to nMes
聽 聽 聽 聽 聽 cMes := 'CONSAL'+StrZero(i,2)
聽 聽 聽 聽 聽 nSuma += (cAlias)->&cMes
聽 聽 聽 NEXT
聽 聽 RETURN(nSuma)
/* --------------------------------------------------------- */
聽 聽 FUNCTION INGMES(cAlias, nMes) //devuelve los ingresos de un mes
聽 聽 聽 MEMVAR mMes
聽 聽 聽 LOCAL cMes, nIng
聽 聽 聽 DEFAULT nMes 聽 := mMes

聽 聽 聽 cMes := 'CONING'+ STRZERO(nMes,2)
聽 聽 聽 nIng := (cAlias)->&cMes
聽 聽 RETURN( nIng )
/* --------------------------------------------------------- */
聽 聽 FUNCTION SALMES(cAlias, nMes) //devuelve las salidas de un mes
聽 聽 聽MEMVAR mMes
聽 聽 聽LOCAL cMes, nSal 聽 
聽 聽 聽DEFAULT nMes 聽 := mMes

聽 聽 聽cMes := 'CONSAL' + 聽STRZERO(nMes,2)
聽 聽 聽nSal := (cAlias)->&cMes
聽 聽RETURN( nSal )
/* --------------------------------------------------------- */
聽 聽 FUNCTION SALANT(cAlias, nMes) 聽//devuelve las salidas de un mes
聽 聽 聽 MEMVAR mMes
聽 聽 聽 LOCAL cMes, nIng, nSal
聽 聽 聽 DEFAULT nMes 聽 := mMes

聽 聽 聽 nIng :=INGANO(cAlias,nMes)
聽 聽 聽 nSal :=SALANO(cAlias,nMes)
聽 聽 RETURN ( nIng - nSal )


La pregunta del TRILLON como hacer esto mismo con sentencias sql? que me recomiendan usar VISTAS? No he usado vistas, tendr谩n un ejemplo

Code (fw): Select all Collapse
cSql:="SELECT d.cod, a.descripcion, a.um, a.cap, d.cantid, [FUNCION DE USUARIO QUE CALCULE STOCK], a.precio from #ppru as d left join articulo as a on d.cod=a.cod "
fwh 17.12, harbour 3.2.0, pelles C, bcc7, Ms-Sql
Posts: 346
Joined: Mon Oct 05, 2009 03:35 PM
Re: calcular stock dentro de un select con funciones de usuario
Posted: Sat Mar 16, 2019 12:25 PM
es un ejemplo con mysql, para que te oriente un poco

http://forums.fivetechsupport.com/viewtopic.php?f=6&t=32245
SkyPe: armando.lagunas@hotmail.com

Mail: armando.lagunas@gmail.com
Posts: 400
Joined: Fri May 11, 2007 08:20 PM
Re: calcular stock dentro de un select con funciones de usuario
Posted: Thu Mar 21, 2019 12:37 AM
Hola Armando gracias por responder
quiero convertir esta funci贸n en fivewin en una funci贸n sql
Code (fw): Select all Collapse
FUNCTION INGANO(cAlias, nMes) // devuelve la suma todo los ingresos hasta el mes que le indique en el parametro
聽 聽 聽 MEMVAR mMes
聽 聽 聽 LOCAL nSuma := 0, i
聽 聽 聽 LOCAL cMes
聽 聽 聽 DEFAULT nMes 聽 := mMes
聽 聽 聽 nMes -= 1
聽 聽 聽 FOR i = 0 to nMes
聽 聽 聽 聽 聽 cMes := 'CONING'+STRZERO(i,2)
聽 聽 聽 聽 聽 nSuma += (cAlias)->&cMes
聽 聽 聽 NEXT
聽 聽 Return(nSuma)


Lo primero que se me vino a la cabeza fue VISTA pero en realidad lo que necesito es FUNCIONES en sql
estoy intentando asi y el compilador mssql me marca mensaje de error

Code (fw): Select all Collapse
CREATE FUNCTION IngAno(@nMes int, @cCod varchar(7)) 
RETURNS int
AS
BEGIN
聽 聽 DECLARE @nsuma int;
聽 聽 set @nsuma = CASE @nMes
聽 聽 When 1 then select coning00 as nsuming from ARTICULO where numero = @cCod
聽 聽 When 2 then select (coning00+coning01) as nsuming from ARTICULO where numero = @cCod
聽 聽 When 3 then select (coning00+coning01+coning02) as nsuming from ARTICULO where numero = @cCod 
聽 聽 RETURN @nsuma)
END
GO


tampoco me funciona asi:
Code (fw): Select all Collapse
CREATE FUNCTION IngAno(@nMes int, @cCod varchar(7)) 
RETURNS int
AS
BEGIN
聽 聽 DECLARE @nsuma int;
聽 聽 RETURN ( 
聽 聽 CASE @nMes
聽 聽 When 1 then SET @nsuma = select TOTBRUTO from CABGUIA where numero = @cCod
聽 聽 When 2 then SET @nsuma = select (TOTBRUTO+TOTBRUTO) from CABGUIA where numero = @cCod
聽 聽 When 3 then SET @nsuma = select (TOTBRUTO+TOTBRUTO+TOTBRUTO) from CABGUIA where numero = @cCod 
聽 聽 RETURN @nsuma

END
GO


Espero que me puedan dar una mano los gurus en sql, gracias
fwh 17.12, harbour 3.2.0, pelles C, bcc7, Ms-Sql
Posts: 114
Joined: Fri Nov 24, 2006 09:03 PM
Re: calcular stock dentro de un select con funciones de usuario
Posted: Thu Mar 21, 2019 01:16 AM

驴Tus tablas estan migradas a Sql-server?

Posts: 400
Joined: Fri May 11, 2007 08:20 PM
Re: calcular stock dentro de un select con funciones de usuario
Posted: Thu Mar 21, 2019 04:42 AM

As铆 es

fwh 17.12, harbour 3.2.0, pelles C, bcc7, Ms-Sql
Posts: 346
Joined: Mon Oct 05, 2009 03:35 PM
Re: calcular stock dentro de un select con funciones de usuario
Posted: Thu Mar 21, 2019 11:34 AM
deja que el mismo motor sql te realice el trabajo, en vez de funci贸n ocupa una Procedure y que el dato devuelto lo tratas como un select com煤n y corriente.

ejemplo personal para que te oriente un poco

Code (fw): Select all Collapse
USE [VIKING_SYSTEM]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CGE_StockResumen_Fruta]
  @Fruta varchar(5)

AS
BEGIN

SELECT  P.PT_Fruta         AS Codigo,
      SUM(C.ET_PesoCaja)   AS Kilos 
            
FROM  CGE_PalletTerminados AS P

      INNER JOIN CGE_CajasTerminadas AS C
                           ON  C.ET_NroPallet=P.PT_Numero_PK 
                          AND C.ET_Reproceso=0

WHERE     P.PT_Estado NOT IN ('D','B','R','a')
      AND P.PT_Hold = 0  
      AND P.PT_Fruta = @Fruta

GROUP BY
      P.PT_Fruta  
;
END


me suma los kilos asignados a un Numero de Pallet de una fruta especifica que la paso como par谩metro "@Fruta"

Code (fw): Select all Collapse
 
oDbf := oConnect:Query("EXECUTE CGE_StockResumen_Fruta '"+SubStr(cFruta,1,5)+"' ")


oDbf recibe los 2 datos resultantes de la ejecuci贸n del procedimiento, en mi caso el c贸digo de la fruta y la suma total de sus kilos
SkyPe: armando.lagunas@hotmail.com

Mail: armando.lagunas@gmail.com
Posts: 400
Joined: Fri May 11, 2007 08:20 PM
Re: calcular stock dentro de un select con funciones de usuario
Posted: Fri Mar 22, 2019 11:39 PM
Gracias armando lo resolvi con funci贸n
tengo una duda existe macro sustitucion en sql , como era en vfp coningXX por los valores 01,02,03, 04,05, 06 ...12, que son los meses del a帽o
Code (fw): Select all Collapse
CREATE FUNCTION CalcIngAno
(
聽 聽 @Codigo int, @nMes int
)
RETURNS int
AS
BEGIN
聽 聽 -- Declare the return variable here
聽 聽 DECLARE @IngA int
聽
聽 聽 -- Restar las fechas para ebtener las edad
聽 聽 SELECT 
聽 聽 聽 聽 @IngA = 
聽 聽 聽 CASE
聽 聽 聽 When @nMes = 1 then (coning00)
聽 聽 聽 When @nMes = 2 Then (coning00+coning01)
聽 聽 聽 When @nMes = 3 tHEN (coning00+(coning01+coning02)
聽 聽 聽 When @nMes = 4 tHEN (coning00+(coning01+coning02+coning03)
聽 聽 聽 When @nMes = 5 tHEN (coning00+coning01+coning02+coning03+coning04)
聽 聽 聽 When @nMes = 6 tHEN (coning00+coning01+coning02+coning03+coning04+coning05)
聽 聽 聽 When @nMes = 7 tHEN (coning00+coning01+coning02+coning03+coning04+coning05+coning06)
聽 聽 聽 When @nMes = 8 tHEN (coning00+coning01+coning02+coning03+coning04+coning05+coning06+coning07)
聽 聽 聽 When @nMes = 9 tHEN (coning00+coning01+coning02+coning03+coning04+coning05+coning06+coning07+coning08)
聽 聽 聽 When @nMes = 10 tHEN (coning00+coning01+coning02+coning03+coning04+coning05+coning06+coning07+coning08+coning09)
聽 聽 聽 When @nMes = 11 tHEN (coning00+coning01+coning02+coning03+coning04+coning05+coning06+coning07+coning08+coning09+coning10)
聽 聽 聽 else (coning00+coning01+coning02+coning03+coning04+coning05+coning06+coning07+coning08+coning09+coning10+coning11)
聽 聽 聽 end

聽 聽 FROM
聽 聽 聽 聽 Articulo P
聽 聽 WHERE
聽 聽 聽 聽 P.Codigo = @Codigo;
聽
聽 聽 -- Retornar el valor escalar
聽 聽 RETURN @IngA
聽
END
GO
fwh 17.12, harbour 3.2.0, pelles C, bcc7, Ms-Sql

Continue the discussion