pgsql PROC IMPORT

导入Excel文件#SAS

proc-import-excel.sas
PROC IMPORT DATAFILE="path/file-name.xlsx" DBMS=XLSX
                          OUT=output-table <REPLACE>;
        SHEET=sheet-name;
RUN;
proc-import-csv.sas
proc import datafile="FILEPATH/storm_damage.csv" dbms=csv
            out=storm_damage_import replace;
run;

pgsql 创建库

使用LIBNAME语句创建SAS库。 #SAS

create-library.sas
LIBNAME libref engine "path";

/* example w/base engine */
libname mylib base "s:/workshop/data";

/* base engine is actually default */
libname mylib "s:/workshop/data";

pgsql 列出表和列属性

报告表#SAS的描述符部分

proc-contents.sas
proc contents data="filepath/class_birthdate.sas7bdat;
run;

pgsql 交错SAS数据集

要交错两个或多个SAS数据集,请在SET语句后使用BY语句:#SAS

interleave.sas
data april;
   set payable recvable;
   by account;
run;

pgsql 连接两个或多个数据集

#SAS

set.sas
data one;
  input name $ age;
datalines;
Chris 36
Jane 21
Jerry 30
Joe 49
;

data two;
  input name $ age group;
datalines;
Daniel 33 1
Terry 40 2
Michael 60 3
Tyrone 26 4
;

data both;
  set one two;
run;

proc print data=both;
run;

pgsql 读表 - 复制表 - 打印表

#SAS

read-copy-print-table.sas
data myclass;
    set sashelp.class;
run;

proc print data=myclass;
run;

pgsql enum检查员

Postgres命令找出枚举可能的值。

enum_list
select n.nspname as enum_schema,  
       t.typname as enum_name,  
       e.enumlabel as enum_value
from pg_type t 
   join pg_enum e on t.oid = e.enumtypid  
   join pg_catalog.pg_namespace n ON n.oid = t.typnamespace

pgsql 使用整数数组

将一个varchar数组转换为一个整数数组,并选择该数组,就像它是一个记录/表/行一样

types_and_functions.pgSQL
CREATE or replace FUNCTION my_method(
    varchar)
  RETURNS varchar
  AS
$body$ 
DECLARE

       reg varchar;
BEGIN
    
    select into reg cast(string_agg(f_descripcion, ', ') as varchar) from public.t_almacen where f_iddepto in (
    select unnest(string_to_array($1,',')::int[])
    );
    if found then

          return reg;

    else

        return '';

    end if;
    
END;
$body$
  LANGUAGE 'plpgsql'
  VOLATILE
  CALLED ON NULL INPUT
  SECURITY INVOKER
  COST 100;

select my_method(CAST('1,3,5,6' as varchar));

pgsql 从更新中返回查询

使用“With”返回更新的选择等效项,但返回值是更新之前的值

update_returns_before_modified
CREATE OR REPLACE FUNCTION public.fix_ncf_sequence (
)
RETURNS trigger AS
$body$
 begin with ncf_duplicados as(
		select
			d.f_ncf as ncf,
			d.f_beneficiario as beneficiario,
			d.f_concepto as concepto,
			d.f_base_imponible as base_imponible,
			d.f_idsuplidor as idsuplidor,
			d.f_monto as monto,
			d.f_rnc_cedula as rnc_cedula,
			d.f_wholenum as documento,
			d.f_fecha as fecha,
			d.f_idcomprobante as id
		from
			t_comprobantes_caja d
		inner join(
				select
					f_ncf,
					count( f_ncf ) as repeticion
				from
					t_comprobantes_caja
				group by
					f_ncf
				having
					count( f_ncf )> 1
			) r on
			r.f_ncf = d.f_ncf
		where
			r.f_ncf is not null
			and r.f_ncf <> ''
			and d.f_ncf ilike 'A0100100113%'
	), ncf_actualizados as (
	
	update
		t_comprobantes_caja as tcc
	set
		f_ncf = get_secuencia_ncf(7) --(select f_codigo from t_ncf where tcc.f_ncf ilike f_tipo||'%' and f_tipo is not null and f_tipo <> '' and f_codigo <= 9)
	where
		f_ncf in(
			select
				ncf
			from
				ncf_duplicados
		)
		and f_idcomprobante not in(
			select
				min( id )
			from
				ncf_duplicados
			group by
				ncf
		) returning *
	)

insert
	into
		t_fixed_dup_ncf(
			f_wholenum,
			f_old_ncf,
			f_new_ncf,
			f_fecha
		)
		(
		select
			dup.documento,
			dup.ncf,
			tcc.f_ncf,
			dup.fecha
		from
			(select f_ncf as ncf, f_fecha as fecha, f_wholenum as documento, f_idcomprobante as id from ncf_actualizados) dup
		inner join t_comprobantes_caja tcc on
			tcc.f_idcomprobante = dup.id
	);

return new;
end;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 100;

pgsql 触发

创建执行过程/函数的触发器

sample_trigger
CREATE TRIGGER check_fix_ncf_duplicados
  AFTER INSERT 
  ON public.t_comprobantes_caja FOR EACH ROW 
  EXECUTE PROCEDURE public.fix_ncf_sequence();