Untitled

 avatar
unknown
sql
3 years ago
3.1 kB
10
Indexable
CREATE TABLE aux(idpedido string,
             fechapedido string,
             fechaenvio string,
             modoenvio string,
             idcliente string,
             cliente string,
             segmento string,
             ciudad string,
             pais string,
             idarticulo string,
             categoria string,
             subcategoria string,
             desc_producto string,
             ventas string,
             cantidad string,
             descuento string,
             beneficio string,
             costoenvio string
             )
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ';'
;

LOAD DATA INPATH '/user/hadoop/SuperTiendaCSV_kmeza.csv'
OVERWRITE INTO TABLE kmeza.aux
;

--DROP TABLE supertienda;

CREATE TABLE SuperTienda(idpedido string,
                         fechapedido date,
                         fechaenvio date,
                         modoenvio string,
                         idcliente string,
                         cliente string,
                         segmento string,
                         ciudad string,
                         pais string,
                         idarticulo string,
                         categoria string,
                         subcategoria string,
                         desc_producto string,
                         ventas float,
                         cantidad int,
                         descuento float,
                         beneficio float,
                         costoenvio float
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde'      
STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat'  
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat'
TBLPROPERTIES ('transactional'='true', 'transactional_properties'='default')
;

INSERT INTO supertienda
SELECT  idpedido,
        cast(concat_ws('-',split(fechapedido,'-')[2],split(fechapedido,'-')[1],split(fechapedido,'-')[0]) as date) as fechapedido,
        cast(concat_ws('-',split(fechaenvio,'-')[2],split(fechaenvio,'-')[1],split(fechaenvio,'-')[0]) as date) as fechaenvio,
        modoenvio,
        idcliente,
        cliente,
        segmento,
        ciudad,
        pais,
        idarticulo,
        categoria,
        subcategoria,
        desc_producto,
        cast(replace(replace(ventas , ',' , '.') , '$' , '') as float) as ventas,
        cast(cantidad as int) as cantidad,
        cast(split(descuento, ',')[0]/100 as float) as descuento,
        cast(replace(replace(beneficio , ',' , '.') , '$' , '') as float) as beneficio,
        cast(replace(replace(costoenvio , ',' , '.') , '$' , '') as float) as costoenvio
FROM aux
where ciudad != 'Ciudad'
;

ALTER TABLE supertienda
ADD COLUMNS (costototal float, iva float, delta_enviopedido int)
;

SELECT ventas-beneficio-costoenvio as CostoTotal,
       (ventas*0.19)/1.19 as IVA,
       datediff(fechaenvio, fechapedido) as delta_enviopedido
FROM supertienda
;

SELECT *
FROM supertienda
LIMIT 100
;

UPDATE supertienda
SET costototal = ventas-beneficio-costoenvio,
    iva = (ventas*0.19)/1.19,
    delta_enviopedido = datediff(fechaenvio, fechapedido)
;
Editor is loading...