PostgreSQL - Dudas Cursor en Postgresql

 
Vista:
sin imagen de perfil
Val: 3
Ha disminuido su posición en 2 puestos en PostgreSQL (en relación al último mes)
Gráfica de PostgreSQL

Dudas Cursor en Postgresql

Publicado por yesid (4 intervenciones) el 14/12/2018 18:12:19
Hola amigos, tengo un problema que no he podido resolver, tengo una tabla historica de ventas, que se llama fact_ventasplu, cree una tabla nueva vacias, quiero que el cursor me inserte solo los registros que doy como rango, mes y años, pero me esta saliendo un error al tratar de grabar la function, y no he podido encontrar el error.
[Err] ERROR: row "contenido" has no field "fecha"
CONTEXT: compilation of PL/pgSQL function "sp_prueba" near line 32

copio la function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
DROP FUNCTION IF EXISTS  sp_prueba();
CREATE OR REPLACE FUNCTION sp_prueba(Mes varchar(10), year_1 INTEGER,year_2 INTEGER) RETURNS VOID AS
$OpCur$
DECLARE
    OperCursor CURSOR FOR  SELECT  																					 to_timestamp(dt.fecha, 'DD/MM/YYYY ') as fecha,
hr.rango as hora,
fv.numtransaccion,
fv.pos::INT,sk.codplu as idCodplu,
sk.plu as nomplu,sk.nivel1,
sk.negocio,sk.nivel2,
sk.categoria,
sk.nivel3,sk.subcategoria,un.codsubdivision as codtienda,
un.subdivision as tienda,
un.division,gf.pais,gf.departamento,gf.ciudad,rs.codresponsable as codvende,rs.responsable,rs.tiporesponsable,rs.cargo,
sum(fv.unidadesplu) as sales_qty,
sum(fv.valorventaxplusiniva) as sales_siva,
sum(fv.valorventaxpluconiva) sales_civa,																							sum(fv.costoxplu) as sales_costo,
sum(fv.valorventaxplusiniva-fv.costoxplu) as contribu
FROM fact_ventasplu fv
	inner join dim_unidadnegocio du on fv.idunidadnegocio = du.idunidadnegocio
	Inner join dim_tiempo dt on fv.idtiempo = dt.idtiempo
	inner join dim_plu sk on fv.idplu=sk.idplu
	inner join dim_unidadnegocio un on fv.idunidadnegocio=un.idunidadnegocio
	inner JOIN dim_geografia gf on fv.idgeografia=gf.idgeografia
	inner join dim_cliente cl on fv.idcliente =cl.idcliente
	inner join dim_responsable rs on fv.idresponsable = rs.idresponsable
	inner join dim_hora hr on fv.idhora=hr.idhora
WHERE dt.mes in (Mes) and dt.anocal in ( year_1,year_2 )
GROUP BY  dt.fecha,hr.rango,fv.numtransaccion,fv.pos,sk.codplu,sk.plu,
    sk.nivel1,sk.negocio,sk.nivel2,sk.categoria,sk.nivel3,sk.subcategoria,
    un.codsubdivision,un.subdivision,un.division,
    gf.pais,gf.departamento,gf.ciudad,rs.codresponsable,
    rs.responsable,rs.tiporesponsable,rs.cargo;
 -- enviamos el resultado a la variable  contenido
 Contenido  fact_ventasplu %ROWTYPE;
BEGIN
	OPEN OperCursor;
	LOOP
	FETCH OperCursor INTO Contenido;
	IF NOT FOUND THEN
  	   EXIT;
	END IF;
	INSERT INTO  sales_actual( fecha,hora,numtransaccion, pos,idcodplu,nomplu,nivel1,negocio ,
        nivel2 ,  categoria,  nivel3 ,  subcategoria,  codtienda ,  tienda , division,  pais ,  departamento,  ciudad ,
        codvende,  responsable,  tiporesponsable,  cargo ,  sales_qty ,  sales_siva ,  sales_civa,  sales_costo,
        contribu)
	VALUES  (Contenido.fecha,Contenido.hora, Contenido.numtransaccion,
Contenido.pos,Contenido.idCodplu,Contenido.nomplu,Contenido.nivel1,Contenido.negocio,Contenido.nivel2,
Contenido.categoria,Contenido.nivel3,Contenido.subcategoria,Contenido.codtienda,Contenido.tienda,
Contenido.division,Contenido.pais,Contenido.departamento,Contenido.ciudad,Contenido.codvende,Contenido.responsable,Contenido.tiporesponsable,Contenido.cargo,Contenido.sales_qty, Contenido.sales_siva, Contenido.sales_civa,Contenido.sales_costo, Contenido.contribu );
	END LOOP;
	CLOSE OperCursor;
END;
$OpCur$
LANGUAGE 'plpgsql'
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Dudas Cursor en Postgresql

Publicado por martin (121 intervenciones) el 15/12/2018 21:02:46
Por lo pronto te falta el campo fecha en el select, solo lo incluiste en el group by, esta fallando la query,no el cursor.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 3
Ha disminuido su posición en 2 puestos en PostgreSQL (en relación al último mes)
Gráfica de PostgreSQL

Dudas Cursor en Postgresql

Publicado por yesid (4 intervenciones) el 17/12/2018 15:43:04
Buenos dias Martin,
to_timestamp(dt.fecha, 'DD/MM/YYYY ') as fecha, este esta en el select, lo que pasa es que no se ve, esta muy a la derecha si lo copias en en un notepad. te lo copia, lo cierto es que aun persiste el problema, me sigue respondiendo el mismo error

[Err] ERROR: row "contenido" has no field "fecha"
CONTEXT: compilation of PL/pgSQL function "sp_prueba" near line 45,
la verdad es que he buscado y cambiado las formas, y guiandome de otros ejemplos, si lanzo el query solo, me funciona y corre perfectamente.

necesito ayuda realmente.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Dudas Cursor en Postgresql

Publicado por martin (121 intervenciones) el 21/12/2018 00:26:12
Proba asi:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
CREATE OR REPLACE FUNCTION public.mi_sp()RETURNS BIGINT
LANGUAGE 'plpgsql'
AS
$$
DECLARE
  mi_cursor refcursor;
  fila fact_ventasplu %ROWTYPE; /*puede ser RECORD*/
BEGIN
 
OPEN mi_cursor FOR SELECT fecha,n... FROM .....;
 
	LOOP
 
	FETCH mi_cursor INTO fila;
 
	IF NOT FOUND THEN
 
  	   EXIT;
 
	END IF;
 
	INSERT INTO  sales_actual( fecha......)
 
	VALUES  (fila.fecha,C...... );
 
	END LOOP;
 
	CLOSE mi_cursor;
 
       RETURN 0;
 
 
END;
$$
o algo asi, el query si podes hacelo view y solo filtralo en la función asi se lee mejor.

SUerte
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Dudas Cursor en Postgresql

Publicado por martin (4 intervenciones) el 21/12/2018 16:52:14
lo ejecute y sigue saliendo el error
[Err] ERROR: row "fila" has no field "fecha"
CONTEXT: compilation of PL/pgSQL function "mi_sp" near line 36

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
DROP FUNCTION IF EXISTS  public.mi_sp();
CREATE OR REPLACE FUNCTION public.mi_sp(Mes varchar(10), year_1 INTEGER,year_2 INTEGER) RETURNS BIGINT
LANGUAGE 'plpgsql'
AS
$$
DECLARE
  mi_cursor refcursor;
  fila fact_ventasplu %ROWTYPE; /*puede ser RECORD*/
BEGIN
 
OPEN mi_cursor FOR SELECT  dt.fecha::TIMESTAMP as fecha,hr.rango as hora,fv.numtransaccion,fv.pos::INT,sk.codplu as idCodplu,sk.plu as nomplu,sk.nivel1,sk.negocio,sk.nivel2,sk.categoria,
									sk.nivel3,sk.subcategoria,un.codsubdivision as codtienda,un.subdivision as tienda,un.division,gf.pais,gf.departamento,gf.ciudad,rs.codresponsable as codvende,rs.responsable,rs.tiporesponsable,rs.cargo,
									sum(fv.unidadesplu) as sales_qty,sum(fv.valorventaxplusiniva) as sales_siva,
									sum(fv.valorventaxpluconiva) sales_civa,sum(fv.costoxplu) as sales_costo,
									sum(fv.valorventaxplusiniva-fv.costoxplu) as contribu
FROM fact_ventasplu fv
						inner join dim_unidadnegocio du on fv.idunidadnegocio = du.idunidadnegocio
						Inner join dim_tiempo dt on fv.idtiempo = dt.idtiempo
						inner join dim_plu sk on fv.idplu=sk.idplu
						inner join dim_unidadnegocio un on fv.idunidadnegocio=un.idunidadnegocio
						inner JOIN dim_geografia gf on fv.idgeografia=gf.idgeografia
						inner join dim_cliente cl on fv.idcliente =cl.idcliente
						inner join dim_responsable rs on fv.idresponsable = rs.idresponsable
						inner join dim_hora hr on fv.idhora=hr.idhora
WHERE dt.mes in (Mes) and dt.anocal in ( year_1,year_2 )
GROUP BY  dt.fecha,hr.rango,fv.numtransaccion,fv.pos,sk.codplu,sk.plu, sk.nivel1,sk.negocio,sk.nivel2,sk.categoria,sk.nivel3,sk.subcategoria, un.codsubdivision,un.subdivision,un.division,
    gf.pais,gf.departamento,gf.ciudad,rs.codresponsable, rs.responsable,rs.tiporesponsable,rs.cargo;
 
	LOOP
 
	FETCH mi_cursor INTO fila;
 
	IF NOT FOUND THEN
 
  	   EXIT;
 
	END IF;
 
	INSERT INTO  sales_actual(fecha,hora,numtransaccion, pos,idcodplu,nomplu,nivel1,negocio, nivel2, categoria, nivel3,subcategoria,codtienda,tienda,division,pais,departamento,ciudad,codvende,responsable,tiporesponsable,cargo ,sales_qty ,sales_siva ,sales_civa,sales_costo,contribu)
 	VALUES  (fila.fecha,fila.hora, fila.numtransaccion,fila.pos,fila.idCodplu,fila.nomplu,fila.nivel1,fila.negocio,fila.nivel2,	fila.categoria,fila.nivel3,fila.subcategoria,fila.codtienda,fila.tienda,fila.division,fila.pais,fila.departamento,fila.ciudad,fila.codvende,fila.responsable,fila.tiporesponsable,fila.cargo,fila.sales_qty, fila.sales_siva, fila.sales_civa,fila.sales_costo, fila.contribu  );
 
	END LOOP;
 
	CLOSE mi_cursor;
 
       RETURN 0;
 
END;
$$

estoy a punto de tirar la pata.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Dudas Cursor en Postgresql

Publicado por martin (4 intervenciones) el 21/12/2018 17:12:18
Hola martin, gracias, ya lo corregi,
fila fact_ventasplu %ROWTYPE; /*puede ser RECORD*/ solo cambien el nombre de fact_ventasplu por sales_actual. fact_ventasplu, este es el historico de ventas, y de ahi debia sacar los datos. para pasarlos a sales_actual.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar