Está en la página 1de 4

create database DATAMART USE DATAMART create table HFACTURA( id_codigo integer identity, id_cliente integer, id_producto integer,

id_tiempo integer, cod_serie char(4), num_correlativo varchar(20), fec_factura datetime, non_cliente varchar(100), des_genero char(1), edad integer, dia varchar(10), mes varchar(10), des_producto varchar(100), cantidad integer, total decimal(16,9), fec_nacimiento datetime ) go drop table HFACTURA alter table HFACTURA add primary key (id_codigo,id_tiempo,id_producto,id_cliente ) alter table HFACTURA ALTER COLUMN ID_CLIENTE INTEGER NOT NULL sp_help HFACTURA SELECT IDENTITY(INT,1,1) AS id_codigo , cast(T3.ID_CLIENTE as integer) as ID_cliente, T4.ID_PRODUCTO AS id_producto , 0 AS id_tiempo , T1.COD_SERIE AS cod_serie , T1.NUM_CORRELATIVO AS num_correlativo , T1.FEC_FACTURA AS fec_factura , t3.des_nombre AS nom_cliente , t3.des_genero AS des_genero , datediff(YEAR,fec_nacimiento,GETDATE()) AS edad , DATEPART(YEAR,FEC_FACTURA) AS AO, ( CASE DATEPART(DW,FEC_FACTURA) WHEN 1 THEN 'Lunes' WHEN 2 THEN 'Martes' WHEN 3 THEN 'Miercoles' WHEN 4 THEN 'Jueves' WHEN 5 THEN 'Viernes' WHEN 6 THEN 'Sbado' WHEN 7 THEN 'Domingo' END) AS dia , (case DATEPART(MONTH,FEC_FACTURA) when 1 then 'Enero' when 2 then 'Febrero' when 3 then 'Marzo' when 4 then 'Abril' when 5 then 'Mayo'

when 6 then 'Junio' when 7 then 'Julio' when 8 then 'Agosto' when 9 then 'Setiembre' when 10 then 'Octubre' when 11 then 'Noviembre' when 12 then 'Diciembre' end) AS mes , t4.des_producto AS des_producto , imp_cantidad AS cantidad , imp_cantidad * imp_preciounitario AS total , fec_nacimiento AS fec_nacimiento into HFACTURA FROM VENTASCORP..VEN_FACCAB T1 INNER JOIN VENTASCORP..VEN_FACDET T2 ON T1.ID_FACTURA = T2.ID_FACTURA INNER JOIN VENTASCORP..VEN_CLIENTE T3 ON T3.ID_CLIENTE = T1.ID_CLIENTE INNER JOIN VENTASCORP..VEN_PRODUCTO T4 ON T4.ID_PRODUCTO = T2.ID_PRODUCTO SELECT * FROM VENTASCORP..VEN_FACCAB SELECT * FROM HFACTURA AO MES DIA drop table dtiempo SELECT DISTINCT IDENTITY(INT,1,1) AS ID_TIEMPO, DATEPART(YEAR,FEC_FACTURA) as AO, (case DATEPART(MONTH,FEC_FACTURA) when 1 then 'Enero' when 2 then 'Febrero' when 3 then 'Marzo' when 4 then 'Abril' when 5 then 'Mayo' when 6 then 'Junio' when 7 then 'Julio' when 8 then 'Agosto' when 9 then 'Setiembre' when 10 then 'Octubre' when 11 then 'Noviembre' when 12 then 'Diciembre' end) MES, ( CASE DATEPART(DW,FEC_FACTURA) WHEN 1 THEN 'Lunes' WHEN 2 THEN 'Martes' WHEN 3 THEN 'Miercoles' WHEN 4 THEN 'Jueves' WHEN 5 THEN 'Viernes' WHEN 6 THEN 'Sbado' WHEN 7 THEN 'Domingo' END) DIA

INTO DTIEMPO FROM HFACTURA alter table dtiempo add primary key (ID_tiempo) select *from DTIEMPO SELECT C1 IDENTITY(INT,1,1), () AO, (CASE END) MES, (CASE END) DIA C2.... INTO DTIEMPO FROM HFACTURA

SELECT * FROM DTIEMPO SELECT * FROM HFACTURA -SELECT A.id_tiempo,B.ID_TIEMPO UPDATE HFACTURA SET id_tiempo = B.ID_TIEMPO FROM HFACTURA A, DTIEMPO B WHERE A.AO = B.AO AND A.mes = B.MES AND A.dia = B.DIA

ALTER TABLE HFACTURA ADD NOMENCLATURA......... //TABLA CLIENTE******************************* CREATE TABLE DCLIENTE( id_cliente integer primary key, des_nombre varchar(50), num_ruc varchar(13), fec_nacimiento datetime, des_genero char(1) ) drop table DCLIENTE insert into DCLIENTE select id_cliente, des_nombre, num_ruc, fec_nacimiento,des_genero from VENTASCORP..ven_cliente *** TABLA DPRODUCTO********************* create table dproducto( id_producto integer primary key, des_producto varchar(100) ) drop table dproducto insert into dproducto select id_producto, des_producto

from VENTASCORP..VEN_PRODUCTO SELECT * FROM dproducto **** tabla de tiemo ****************** create table dtiempo( id_tiempo int primary key, dia varchar(5), mes varchar(10), ao varchar(12), ) drop table DTIEMPO select *from DCLIENTE select *from HFACTURA select * from dtiempo Alter table HFACTURA add foreign key(id_cliente) references DCLIENTE(id_cliente) Alter table HFACTURA add foreign key (id_tiempo) references Dtiempo (id_tiempo) Alter table HFACTURA add foreign key (id_producto) references Dproducto (id_producto)

También podría gustarte