SQL_ODPS-D2-离线数仓-7-临时查询

SQL_ODPS-D2-离线数仓-7-临时查询

包括ods dwd dws ads数仓的建表语句 以及 手动导入数据语句

test

--odps sql 
--********************************************************************--
--author:i
--create time:0000-00-00 00:00:00
--********************************************************************--
SELECT  *
FROM    ods_base_log
WHERE   ds = '00000000'
;

--测试FlatEventUDTF自定义函数 一进多出
SELECT  FLATEVENTUDTF(GET_JSON_OBJECT(log_string,'$.et')) AS (event_time,event_name,event_json)    --$.et 就是事件
FROM    ods_base_log
WHERE   ds = '00000000'
;

--手动将ods层数据导入到dwd层
INSERT OVERWRITE TABLE dwd_start_log PARTITION(ds,hh,mm)
SELECT  GET_JSON_OBJECT(log_string,'$.cm.mid') mid
        ,GET_JSON_OBJECT(log_string,'$.cm.uid') user_id
        ,GET_JSON_OBJECT(log_string,'$.cm.vc' ) version_code
        ,GET_JSON_OBJECT(log_string,'$.cm.vn') version_name
        ,GET_JSON_OBJECT(log_string,'$.cm.l') lang
        ,GET_JSON_OBJECT(log_string,'$.cm.sr') source
        ,GET_JSON_OBJECT(log_string,'$.cm.os') os
        ,GET_JSON_OBJECT(log_string,'$.cm.ar') area
        ,GET_JSON_OBJECT(log_string,'$.cm.md') model
        ,GET_JSON_OBJECT(log_string,'$.cm.ba') brand
        ,GET_JSON_OBJECT(log_string,'$.cm.sv') sdk_version
        ,GET_JSON_OBJECT(log_string,'$.cm.hw') height_width
        ,GET_JSON_OBJECT(log_string,'$.cm.g') email
        ,GET_JSON_OBJECT(log_string,'$.cm.hw') sv
        ,GET_JSON_OBJECT(log_string,'$.cm.ln') ln
        ,GET_JSON_OBJECT(log_string,'$.cm.la') la
        ,GET_JSON_OBJECT(event_view.event_json,'$.entry') entry
        ,GET_JSON_OBJECT(event_view.event_json,'$.loading_time') loading_time
        ,GET_JSON_OBJECT(event_view.event_json,'$.action') action
        ,GET_JSON_OBJECT(event_view.event_json,'$.open_ad_type') open_ad_type
        ,GET_JSON_OBJECT(event_view.event_json,'$.detail') detail
        ,event_view.event_time
        ,ds
        ,hh
        ,mm
FROM    ods_base_log
LATERAL VIEW FLATEVENTUDTF(GET_JSON_OBJECT(log_string,'$.et')) event_view AS event_time,event_name,event_json
WHERE   ds = '00000000'
AND     event_view.event_name = 'start'
;

--查看导入结果
SELECT  *
FROM    dwd_start_log
WHERE   ds = '00000000'
LIMIT   3
;
-- mid	user_id	version_code	version_name	lang	source	os	area	model	brand	sdk_version	email	height_width	network	lng	lat	entry	open_ad_type	action	loading_time	detail	event_time	ds	hh	mm
-- 0	0	9	1.0.7	es	V	8.1.1	MX	HTC-0	HTC	V2.5.9	640*960	[email protected]	640*960	-112.5	29.2	3	13	1	2		1000001265269	00000000	01	15
--手动将dwd层数据导入到dws层

INSERT OVERWRITE TABLE dws_uv_detail_d PARTITION(ds,hh,mm)
SELECT  mid
        ,user_id
        ,version_code
        ,version_name
        ,lang
        ,source
        ,os
        ,area
        ,model
        ,brand
        ,sdk_version
        ,email
        ,height_width
        ,network
        ,lng
        ,lat
        ,event_time
        ,ds
        ,hh
        ,mm
FROM    (
            SELECT  *
                    ,row_number() OVER(PARTITION BY mid ORDER BY event_time ASC) rn
            FROM    dwd_start_log
            WHERE   ds = '00000000'
        ) 
WHERE   rn = 1
;

--查看导入结果
SELECT  *
FROM    dws_uv_detail_d
WHERE   ds = '00000000'
LIMIT   5
;
-- mid	user_id	version_code	version_name	lang	source	os	area	model	brand	sdk_version	email	height_width	network	lng	lat	event_time	ds	hh	mm
-- 999	999	16	1.2.0	es	W	8.1.0	MX	sumsung-4	Sumsung	V2.2.9	1080*1920	[email protected]	1080*1920	-86.9	12.8	1583581530558	00000000	01	15
-- 1	1	11	1.2.0	es	R	8.1.4	MX	Huawei-4	Huawei	V2.1.7	640*1136	[email protected]	640*1136	-101.2	21.8	1583553705570	00000000	01	15
-- 10	10	16	1.2.2	en	E	8.0.8	MX	sumsung-10	Sumsung	V2.7.8	750*1134	[email protected]	750*1134	-96.7	0.8	1583514130824	00000000	01	15
-- 100	100	18	1.0.5	pt	G	8.1.6	MX	HTC-11	HTC	V2.7.3	640*960	[email protected]	640*960	-84.8	25.9	1583564803755	00000000	01	15
-- 101	101	14	1.2.1	en	L	8.2.0	MX	Huawei-0	Huawei	V2.5.5	750*1134	[email protected]	750*1134	-72.9	-4.6	1583574821599	00000000	01	15

--ods层原数据层建表语句
CREATE TABLE `ods_order_info_di` 
(
    `id` STRING COMMENT '订单编号'
    ,`total_amount` DOUBLE COMMENT '订单金额'
    ,`order_status`STRING COMMENT '订单状态'
    ,`user_id` STRING COMMENT '用户id'
    ,`payment_way` STRING COMMENT '支付方式'
    ,`out_trade_no` STRING COMMENT '支付流水号'
    ,`create_time` STRING COMMENT '创建时间'
    ,`operate_time` STRING COMMENT '操作时间'
    ,`province_id`STRING COMMENT '省份'
)
COMMENT '订单表'
PARTITIONED BY 
(
    ds STRING
)
;

CREATE TABLE `ods_order_deatail_di` 
(
    `id` STRING COMMENT '明细id'
    ,`order_id` STRING COMMENT '订单id'
    ,`sku_id` DOUBLE COMMENT '商品id'
    ,`sku_name` STRING COMMENT '商品名称'
    ,`order_price` DOUBLE COMMENT '购买价格'
    ,`sku_num` BIGINT COMMENT '购物数量'
    ,`create_time` STRING COMMENT '创建时间'
)
COMMENT '订单明细'
PARTITIONED BY 
(
    ds STRING
)
;

CREATE TABLE `ods_sku_info_df` 
(
    `id` STRING COMMENT 'skuid'
    ,`spu_id` STRING COMMENT '订单id'
    ,`price` DOUBLE COMMENT '价格'
    ,`sku_name` STRING COMMENT '商品名称'
    ,`sku_desc` STRING COMMENT '商品描述'
    ,`weight` DOUBLE COMMENT '重量(千克)'
    ,`tm_id` STRING COMMENT '品牌 id'
    ,`category3_id` STRING COMMENT '品类id'
    ,`create_time` STRING COMMENT '创建时间'
)
COMMENT '商品信息'
PARTITIONED BY 
(
    ds STRING
)
;

drop table `ods_user_info_df` ;

CREATE TABLE `ods_user_info_df` 
(
    `id` STRING COMMENT '用户id'
    ,`name` STRING COMMENT '姓名'
    ,`birthday` STRING COMMENT '生日'
    ,`gender` STRING COMMENT '性别'
    ,`email` STRING COMMENT '邮箱'
    ,`user_level` STRING COMMENT '用户等级'
    ,`create_time` STRING COMMENT '创建时间'
)
COMMENT '用户信息'
PARTITIONED BY 
(
    ds STRING
)
;

CREATE TABLE `ods_base_category3_df` 
(
    `id` STRING COMMENT '三级品类id'
    ,`name` STRING COMMENT '名称'
    ,`category2_id` STRING COMMENT '二级品类id'
)
COMMENT '三级品类信息'
PARTITIONED BY 
(
    ds STRING
)
;

CREATE TABLE `ods_base_trademark_df` 
(
    `tm_id` STRING COMMENT '品牌id'
    ,`tm_name` STRING COMMENT '名称'
)
COMMENT '品牌信息'
PARTITIONED BY 
(
    ds STRING
)
;

CREATE TABLE `ods_base_category1_df` 
(
    `id` STRING COMMENT '一级品类id'
    ,`name` STRING COMMENT '名称'
)
COMMENT '一级品类信息'
PARTITIONED BY 
(
    ds STRING
)
;

CREATE TABLE `ods_base_category2_df` 
(
    `id` STRING COMMENT '二级品类id'
    ,`name` STRING COMMENT '名称'
    ,`category1_id` STRING COMMENT '一级品类id'
)
COMMENT '二级品类信息'
PARTITIONED BY 
(
    ds STRING
)
;

CREATE TABLE `ods_payment_info_di` 
(
    `id` BIGINT COMMENT '编号'
    ,`out_trade_no` STRING COMMENT '对外业务编号'
    ,`order_id` STRING COMMENT '订单编号'
    ,`user_id` STRING COMMENT '用户编号'
    ,`alipay_trade_no` STRING COMMENT '支付宝交易流水号'
    ,`total_amount` DOUBLE COMMENT '支付类型'
    ,`subject` STRING COMMENT '交易内容'
    ,`payment_type` STRING COMMENT '支付类型'
    ,`payment_time` STRING COMMENT '支付时间'
)
COMMENT '支付流水表'
PARTITIONED BY 
(
    ds STRING
)
;

CREATE TABLE `ods_base_region_df` 
(
    `id` BIGINT COMMENT '地区id'
    ,`region_name` STRING COMMENT '地区名称'
)
COMMENT '地区'
PARTITIONED BY 
(
    ds STRING
)
;

CREATE TABLE `ods_base_province_df` 
(
    `id` BIGINT COMMENT '品牌id'
    ,`name` STRING COMMENT '名称'
    ,`region_id` STRING COMMENT '地区id'
)
COMMENT '省份'
PARTITIONED BY 
(
    ds STRING
)
;

--dwd明细数据层:清洗,维度退化。
--如ods地区+ods省份=>dwd省份地区
--dwd明细数据层建表语句
CREATE TABLE `dwd_order_info_di` (
    `id` STRING COMMENT '订单id',
    `total_amount` STRING COMMENT '订单总额',
    `order_status` STRING COMMENT ' 1 未支付 2 已支付 3 已发货 4 已收货 5 已完成',
    `user_id` STRING COMMENT '用户id',
    `payment_way` STRING COMMENT '付款方式',
    `out_trade_no` STRING COMMENT '订单流失号',
    `province_id` STRING COMMENT '省市id',
    `create_time` STRING COMMENT '创建时间',
    `operate_time` STRING COMMENT '修改时间'
)
COMMENT '订单表'
PARTITIONED BY (ds STRING);

CREATE TABLE `dwd_order_detail_di` (
    `id` STRING COMMENT '明细id',
    `order_id` STRING COMMENT '订单id',
    `user_id` STRING COMMENT '用户id',
    `sku_id` STRING COMMENT '商品id',
    `sku_name` STRING COMMENT '商品名称',
    `order_price` STRING COMMENT '购买价格',
    `sku_num` STRING COMMENT '购物数量',
    `province_id` STRING COMMENT '省市id',
    `create_time` STRING COMMENT '创建时间'
)
COMMENT '订单明细'
PARTITIONED BY (ds STRING )
;

CREATE TABLE `dim_sku_info_df`
(
    `id` STRING COMMENT '商品id'
    ,`spu_id` STRING COMMENT 'spuid'
    ,`price` DOUBLE COMMENT '商品价格'
    ,`sku_name` STRING COMMENT '商品名称'
    ,`sku_desc` STRING COMMENT '商品描述'
    ,`weight` DOUBLE COMMENT '重量'
    ,`tm_id` STRING COMMENT '品牌id'
    ,`tm_name` STRING COMMENT '品牌名称'
    ,`category3_id` STRING COMMENT '三级分类id'
    ,`category2_id` STRING COMMENT '二级分类id'
    ,`category1_id` STRING COMMENT '一级分类id'
    ,`category3_name` STRING COMMENT '三级分类名称'
    ,`category2_name` STRING COMMENT '二级分类名称'
    ,`category1_name` STRING COMMENT '一级分类名称'
    ,`create_time` STRING COMMENT '创建时间'
)
COMMENT '商品信息'
PARTITIONED BY 
(
    ds STRING
)
;

CREATE TABLE `dim_user_info_df` 
(
    `id` STRING COMMENT 'id'
    ,`name` STRING COMMENT '用户名称'
    ,`birthday` STRING COMMENT '生日'
    ,`gender` STRING COMMENT '性别'
    ,`email` STRING COMMENT '邮箱'
    ,`user_level` STRING COMMENT '等级'
    ,`create_time` STRING COMMENT '注册时间'
)
COMMENT '用户信息表'
PARTITIONED BY 
(
    ds STRING
)
;

CREATE TABLE `dim_base_province_df` 
(
    `id` STRING COMMENT 'id'
    ,`province_name` STRING COMMENT '省市id'
    ,`region_id` STRING COMMENT '地区id'
    ,`region_name` STRING COMMENT '地区名称'
)
COMMENT '地区省市表'
PARTITIONED BY 
(
    ds STRING
)
;

--手动将数据导入dwd层  --why 有的insert后面跟的分区有加值 有的不加
INSERT OVERWRITE TABLE dwd_order_info_di PARTITION(ds)
SELECT  id
        ,total_amount
        ,order_status
        ,user_id
        ,payment_way
        ,out_trade_no
        ,province_id
        ,create_time
        ,operate_time
        ,ds
FROM    ods_order_info_di
WHERE   ds = '${bizdate}'
AND     id IS NOT NULL
;

SELECT * FROM dwd_order_info_di WHERE ds='00000000';

INSERT OVERWRITE TABLE dwd_order_detail_di PARTITION(ds)
SELECT  od.id
        ,order_id
        ,oi.user_id
        ,sku_id
        ,sku_name
        ,order_price
        ,sku_num
        ,oi.province_id
        ,od.create_time
        ,od.ds
FROM    ods_order_deatail_di od
JOIN    ods_order_info_di oi
ON      od.order_id = oi.id
AND     oi.ds = '${bizdate}'
AND     od.ds = '${bizdate}'
AND     od.id IS NOT NULL
;

SELECT * FROM dwd_order_detail_di WHERE ds='00000000' LIMIT 10;

INSERT OVERWRITE TABLE dim_sku_info_df PARTITION(ds)
SELECT  sku.id
        ,sku.spu_id
        ,sku.price
        ,sku.sku_name
        ,sku.sku_desc
        ,sku.weight
        ,sku.tm_id
        ,tm.tm_name
        ,sku.category3_id
        ,c2.id category2_id
        ,c1.id category1_id
        ,c3.name category3_name
        ,c2.name category2_name
        ,c1.name category1_name
        ,sku.create_time
        ,sku.ds
FROM    (
            SELECT  *
            FROM    ods_sku_info_df
            WHERE   ds = '${bizdate}'
            AND     id IS NOT NULL
        ) sku
JOIN    ods_base_category3_df c3
ON      sku.category3_id = c3.id
AND     c3.ds = '${bizdate}'
JOIN    ods_base_category2_df c2
ON      c3.category2_id = c2.id
AND     c2.ds = '${bizdate}'
JOIN    ods_base_category1_df c1
ON      c2.category1_id = c1.id
AND     c1.ds = '${bizdate}'
JOIN    ods_base_trademark_df tm
ON      tm.tm_id = sku.tm_id
AND     tm.ds = '${bizdate}'
;

SELECT * FROM dim_sku_info_df WHERE ds='00000000' LIMIT 10;

INSERT OVERWRITE TABLE dim_user_info_df PARTITION(ds)
SELECT  id
        ,name
        ,birthday
        ,gender
        ,email
        ,user_level
        ,create_time 
        ,ds
FROM    ods_user_info_df
WHERE   ds = '${bizdate}'
AND     id IS NOT NULL
;
SELECT * FROM dim_user_info_df WHERE ds='00000000' LIMIT 10;

INSERT OVERWRITE TABLE `dim_base_province_df` PARTITION(ds)
SELECT  P.id
        ,P.name
        ,P.region_id
        ,r.region_name
        ,P.ds
FROM    ods_base_province_df P
JOIN    ods_base_region_df r
ON      P.region_id = r.id
AND     P.ds = '${bizdate}'
AND     r.ds = '${bizdate}'
;

SELECT * FROM dim_base_province_df WHERE ds='00000000' LIMIT 10;


--dws层针对明细粒度的数据进行短周期汇总。
--如最近一天某个类目、某个地区、某类人群购买商品销售总额、购买力分布。

--dws层建表语句
DROP TABLE IF EXISTS `dws_trade_detail_di` ;
-- CREATE TABLE `dws_trade_detail_di` 
-- (
--     `user_id` STRING COMMENT '用户'
--     ,`sku_id` STRING COMMENT '商品id'
--     ,`user_gender` STRING COMMENT '用户性别'
--     ,`user_age` STRING COMMENT '用户年龄'
--     ,`user_level` STRING COMMENT '用户等级'
--     ,`sku_price` DOUBLE  COMMENT '商品当日价格'
--     ,`sku_name` STRING COMMENT '商品名称'
--     ,`sku_category3_id` STRING COMMENT '商品三级品类id'
--     ,`sku_category2_id` STRING COMMENT '商品二级品类id'
--     ,`sku_category1_id` STRING COMMENT '商品一级品类id'
--     ,`sku_category3_name` STRING COMMENT '商品三级品类名称'
--     ,`sku_category2_name` STRING COMMENT '商品二级品类名称'
--     ,`sku_category1_name` STRING COMMENT '商品一级品类名称'
--     ,`spu_id`STRING COMMENT '商品的spu'
--     ,`tm_id` STRING COMMENT '品牌id'
--     ,`tm_name` STRING COMMENT '品牌名称'
--     ,`province_id` STRING COMMENT '省市id'
--     ,`province_name` STRING COMMENT '省市名称'
--     ,`region_id` STRING COMMENT '地区id'
--     ,`region_name` STRING COMMENT '地区名称'
--     ,`sku_num` BIGINT COMMENT '购买个数'
--     ,`order_count` BIGINT COMMENT '当日下单单数'
--     ,`order_amount` DOUBLE COMMENT '当日下单金额'
-- )
-- COMMENT '用户单日交易行为宽表'
-- PARTITIONED BY 
-- (
--     ds STRING
-- )
-- ;
CREATE TABLE `dws_trade_detail_di` 
(
    `user_id` STRING COMMENT '用户 id'
    ,`sku_id` STRING COMMENT '商品 Id'
    ,`user_gender` STRING COMMENT '用户性别'
    ,`user_age` STRING COMMENT '用户年龄'
    ,`user_level` STRING COMMENT '用户等级'
    ,`sku_price` DOUBLE COMMENT '商品当日价格'
    ,`sku_name` STRING COMMENT '商品名称'
    ,`sku_category3_id` STRING COMMENT '商品三级品类 id'
    ,`sku_category2_id` STRING COMMENT '商品二级品类 id'
    ,`sku_category1_id` STRING COMMENT '商品一级品类 id'
    ,`sku_category3_name` STRING COMMENT '商品三级品类名称'
    ,`sku_category2_name` STRING COMMENT '商品二级品类名称'
    ,`sku_category1_name` STRING COMMENT '商品一级品类名称'
    ,`spu_id` STRING COMMENT '商品 spu'
    ,`tm_id` STRING COMMENT '品牌 id'
    ,`tm_name` STRING COMMENT '品牌名称'
    ,`province_id` STRING COMMENT '省市 id'
    ,`province_name` STRING COMMENT '省市名称'
    ,`region_id` STRING COMMENT '地区 id'
    ,`region_name` STRING COMMENT '地区名称'
    ,`sku_num` BIGINT COMMENT '购买个数'
    ,`order_count` BIGINT COMMENT '当日下单单数'
    ,`order_amount` DOUBLE COMMENT '当日下单金额'
)
COMMENT '用户单日交易行为宽表'
PARTITIONED BY 
(
    ds STRING
)
;


--手动将数据导入dws层
-- WITH
--     tmp_trade AS ( SELECT od.user_id ,od.sku_id ,od.province_id ,sum(sku_num) sku_num ,COUNT(*) order_count ,sum(od.order_price*sku_num) order_amount FROM dwd_order_detail_di od WHERE od.ds='${bizdate}' GROUP BY od.user_id,od.sku_id,od.province_id )
-- INSERT OVERWRITE TABLE `dws_trade_detail_di` PARTITION( ds='${bizdate}' )
-- SELECT  tmp_trade.user_id
--         ,tmp_trade.sku_id
--         ,u.gender
--         ,months_between(
--             TO_CHAR(TO_DATE('${bizdate}','yyyymmdd'),'yyyy-mm-dd')
--             ,u.birthday
--         )/12 age
--         ,u.user_level
--         ,price
--         ,sku_name
--         ,category3_id
--         ,category2_id
--         ,category1_id
--         ,category3_name
--         ,category2_name
--         ,category1_name
--         ,spu_id
--         ,tm_id
--         ,tm_name
--         ,P.id
--         ,P.province_name
--         ,P.region_id
--         ,P.region_name
--         ,tmp_trade.sku_num
--         ,tmp_trade.order_count
--         ,tmp_trade.order_amount
-- FROM    tmp_trade
-- LEFT JOIN dim_user_info_df u
-- ON      u.id = tmp_trade.user_id
-- AND     u.ds = '${bizdate}' LEFT
-- JOIN    dim_sku_info_df s
-- ON      tmp_trade.sku_id = s.id
-- AND     s.ds = '${bizdate}'
-- LEFT JOIN dim_base_province_df P
-- ON      tmp_trade.province_id = P.id
-- AND     P.ds = '${bizdate}'
-- ;

WITH
    tmp_trade AS ( SELECT od.user_id,od.sku_id,od.province_id, sum(sku_num) sku_num, COUNT(*) order_count, sum(od.order_price*sku_num) order_amount FROM dwd_order_detail_di od WHERE od.ds='${bizdate}' GROUP BY od.user_id, od.sku_id, od.province_id )
INSERT OVERWRITE TABLE dws_trade_detail_di PARTITION(ds='${bizdate}')
SELECT  tmp_trade.user_id
        ,tmp_trade.sku_id
        ,u.gender
        ,months_between(
            to_char(to_date('${bizdate}','yyyymmdd'),'yy yy-mm-dd')
            ,u.birthday
        )/12 age
        ,u.user_level
        ,price
        ,sku_name
        ,category3_id
        ,category2_id
        ,category1_id
        ,category3_name
        ,category2_name
        ,category1_name
        ,spu_id
        ,tm_id
        ,tm_name
        ,pp.id
        ,pp.province_name
        ,pp.region_id
        ,pp.region_name
        ,tmp_trade.sku_num
        ,tmp_trade.order_count
        ,tmp_trade.order_amount
FROM    tmp_trade
LEFT JOIN dim_user_info_df u ON u.id = tmp_trade.user_id AND u.ds = '${bizdate}' 
LEFT JOIN dim_sku_info_df s ON tmp_trade.sku_id = s.id AND s.ds = '${bizdate}'
LEFT JOIN dim_base_province_df pp ON tmp_trade.province_id = pp.id AND pp.ds = '${bizdate}'
;

SELECT * FROM dws_trade_detail_di WHERE ds='00000000' LIMIT 10;
desc dws_trade_detail_di;
drop table if exists tmp_trade; --临时表不能drop    
SELECT * FROM dwd_order_detail_di WHERE ds='00000000' LIMIT 10;




--ADS层:针对某个维度,进行汇总。
--如针对年龄、地区、商品进行汇总统计,统计下单数、购买商品数、销售额、平均客单价四个指标。
CREATE TABLE `ads_trade_age_d` (
    `age` BIGINT COMMENT '年龄'
    ,`sku_num` BIGINT COMMENT '购买商品个数'
    ,`order_count` BIGINT COMMENT '订单个数'
    ,`order_amount` DOUBLE COMMENT '销售额'
    ,`avg_amount` DOUBLE COMMENT '平均客单价'
)
COMMENT '年龄销售统计'
PARTITIONED BY (ds STRING )
;

CREATE TABLE `ads_trade_province_d` 
(
    `province` STRING COMMENT '省份id'
    ,`province_name` STRING COMMENT '省市名称'
    ,`region_id` STRING COMMENT '地区id'
    ,`region_name` STRING COMMENT '地区名称'
    ,`sku_num` BIGINT COMMENT '购买商品个数'
    ,`order_count` BIGINT COMMENT '订单个数'
    ,`order_amount` DOUBLE COMMENT '销售额'
    ,`avg_amount` DOUBLE COMMENT '平均客单价'
)
COMMENT '地区销售统计'
PARTITIONED BY 
(
    ds STRING
)
;

CREATE TABLE `ads_trade_sku_d` 
(
    `sku_id` STRING COMMENT '商品id'
    ,`sku_name` STRING COMMENT '商品名称'
    ,`sku_num` BIGINT COMMENT '购买商品个数'
    ,`category3_id` STRING COMMENT '三级分类id'
    ,`category2_id` STRING COMMENT '二级分类id'
    ,`category1_id` STRING COMMENT '一级分类id'
    ,`category3_name` STRING COMMENT '三级分类名称'
    ,`category2_name` STRING COMMENT '二级分类名称'
    ,`category1_name` STRING COMMENT '一级分类名称'
    ,`order_count` BIGINT COMMENT '订单个数'
    ,`order_amount` DOUBLE COMMENT '销售额'
    ,`avg_amount` DOUBLE COMMENT '平均客单价'
)
COMMENT '商品销售统计'
PARTITIONED BY 
(
    ds STRING
)
;

--手动将数据导入ADS层
INSERT OVERWRITE TABLE `ads_trade_age_d` PARTITION(ds='${bizdate}')
SELECT  round(td.user_age) age
        ,sum(sku_num) sku_num
        ,sum(order_count) order_count
        ,sum(order_amount) order_amount
        ,round(avg(order_amount),2) avg_amount
FROM    dws_trade_detail_di td
WHERE   ds = '${bizdate}'
GROUP BY ROUND(td.user_age)
;

SELECT *  FROM ads_trade_age_d WHERE ds='00000000' LIMIT 10;

INSERT OVERWRITE TABLE ads_trade_province_d PARTITION(ds='${bizdate}')
SELECT  td.province_id
        ,td.province_name
        ,td.region_id
        ,td.region_name
        ,sum(sku_num) sku_num
        ,sum(order_count) order_count
        ,sum(order_amount) order_amount
        ,round(AVG(order_amount),2) avg_amount
FROM    dws_trade_detail_di td
WHERE ds='${bizdate}'
GROUP BY td.province_id
         ,td.province_name
         ,td.region_id
         ,td.region_name
;
SELECT *  FROM ads_trade_province_d WHERE ds='00000000' LIMIT 10;


INSERT OVERWRITE TABLE ads_trade_sku_d PARTITION (ds='${bizdate}')
SELECT 
    td.sku_id
    ,td.sku_name
    ,td.sku_category3_id
    ,td.sku_category2_id
    ,td.sku_category1_id
    ,td.sku_category3_name
    ,td.sku_category2_name
    ,td.sku_category1_name
    ,SUM(sku_num) sku_num
    ,SUM(order_count) order_count
    ,SUM(order_amount) order_amount
    ,ROUND(AVG(order_amount),2) avg_amount
FROM dws_trade_detail_di td 
WHERE ds='${bizdate}'
GROUP BY td.sku_id
    ,td.sku_name
    ,td.sku_category3_id
    ,td.sku_category2_id
    ,td.sku_category1_id
    ,td.sku_category3_name
    ,td.sku_category2_name
    ,td.sku_category1_name
;
SELECT *  FROM ads_trade_age_d WHERE ds='00000000' LIMIT 10; --这里有\n原因在上


--全流程调度完成后 查询一下
SELECT * FROM ads_trade_age_d WHERE ds='00000000' LIMIT 10;
SELECT * FROM ads_trade_sku_d WHERE ds='00000000' LIMIT 10; --因为上面的原因一直\n但调度上没有问题
SELECT * FROM ads_trade_province_d WHERE ds='00000000' LIMIT 10;

离线同步test

--odps sql 
--********************************************************************--
--author:i
--create time:0000-00-00 00:00:00
--********************************************************************--

--每日全量表同步测试
SELECT * FROM ods_user_info_df WHERE ds='00000000';
SELECT * FROM ods_base_category1_df WHERE ds='00000000';
SELECT * FROM ods_base_category2_df WHERE ds='00000000';
SELECT * FROM ods_base_category3_df WHERE ds='00000000';
SELECT * FROM ods_base_province_df WHERE ds='00000000';
SELECT * FROM ods_base_region_df WHERE ds='00000000';
SELECT * FROM ods_base_trademark_df WHERE ds='00000000';
SELECT * FROM ods_sku_info_df WHERE ds='00000000';
--每日增量表同步测试
-- rename ods_order_deatail_di TO ods_order_detail_di;
SELECT * FROM ods_order_deatail_di WHERE ds='00000000';
SELECT * FROM ods_order_info_di WHERE ds='00000000';


注:仅做笔记学习使用。

你可能感兴趣的:(大数据,MaxCompute,DataWorks)