maxcompute笔记

maxcompute安装和配置

 https://help.aliyun.com/document_detail/27804.html?spm=a2c4g.11174283.3.5.566a590e0PB79r

=================================

mysql与sql的不同

SELECT TOP 2 * FROM Persons
SELECT TOP 50 PERCENT * FROM Persons
SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2

SELECT INTO 语句

SELECT INTO 语句从一个表中选取数据,然后把数据插入另一个表中。

SELECT INTO 语句常用于创建表的备份复件或者用于对记录进行存档。

可以在创建表时规定约束(通过 CREATE TABLE 语句),或者在表创建之后也可以(通过 ALTER TABLE 语句)。

我们将主要探讨以下几种约束:

  • NOT NULL
  • UNIQUE
  • PRIMARY KEY
  • FOREIGN KEY
  • CHECK
  • DEFAULT
CREATE TABLE Persons
(
Id_P int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

Insert into与Insert overwrite的区别是:Insert into会向表或表的分区中追加数据,而Insert overwrite会在向表或分区中插入数据前清空表中的原有数据。

MySQL中表复制:create table like 与 create table as select

create table like t1  会复制表数据结构和索引,一般使用这个。但是不会复制数据

 

  • MaxCompute的Insert语法与通常使用的MySQL或Oracle的Insert语法有差别,在insert overwrite|into后需要加入table关键字,而非直接使用tablename。

 

=====================重要=====================

create table sale_detail_insert like sale_detail;
alter table sale_detail_insert add partition(sale_date='2013', region='china');
insert overwrite table sale_detail_insert partition (sale_date='2013', region='china')
select shop_name, customer_id,total_price from sale_detail;


如果设置了主键自增,则insert的时候,只要指定主键的列为0,就可以




=======================================

select TO_CHAR(TO_DATE(20180101,'yyyymmdd'),'yyyymm') as time
======================================
什么情况下使用inner join?

首先,什么情况下需要使用完全限定列名,即【表.列名】
在从多张表中,查询数据的时候,最好都使用完全限定列名:比如使用了子查询,或者连结了多张表

(事实上,我们可以使用where语句做内连结,但是并不推荐)
inner join其实是交集,Join可以理解为进行【集合操作】
如果inner join 其实就是join,也就是说,必须两个表都有inner join on 字段A, 都有A才会出现结果,如果A不同的话,就不会出现结果。
如果要让字段A在表1中出现,但是表2中不出现的数据,也要出现在查询结果当中,则使用left join.
更多的Join类型请参考:https://www.cnblogs.com/logon/p/3748020.html

========================================
insert overwrite table xiamensales_retail_by_month partition(pt)
select t.userid, p.barcode,SUM(t.quantity),SUM(t.quantity*t.sellprice), CAST(TO_CHAR(TO_DATE(t.pt,'yyyymmdd'),'yyyymm') AS BIGINT) as pt 
from
( select productuid,userid,quantity,sellprice,pt from ticketitem where name is not NULL and pt>=20170101 and pt<20190101) t --商品名为必填项
inner join user_ods u on t.userid=u.id
inner join product p on t.productuid=p.uid and t.userid=p.userid --确定了productuid和userid就能确定对应商品
where u.pt=${bdp.system.bizdate} --商家信息可能会更新,取最新的分区
and industry='零售超市' and address like '%厦门市%'
group by p.barcode, t.userid, CAST(TO_CHAR(TO_DATE(t.pt,'yyyymmdd'),'yyyymm') AS BIGINT)
 
1、如果select使用了表达式 然后 as 别名,则group by里面,分组的时候,只能使用表达式,不能使用别名
2、动态插入分区,需要我们select的名字,跟表里的分区名一样
3、${bdp.system.bizdate}是当前的时间,取到日,主要是用来如果我们每天固定跑节点,这个变量可以用来取最新的分区。
 
 
===============================================
如果一个表有两个词,比如ticketitem,则写别名的时候,也要使用两个字母ti
posted @ 2019-02-18 11:24  yjy888  阅读(773)  评论(0编辑  收藏  举报