-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadme
69 lines (49 loc) · 2.48 KB
/
Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Basic:
SELECT FROM / WHERE / LIKE / * % / IN / OR / AND / NOT/ LIMIT
BETWEEN AND
DATA JOIN:
JOIN ON /LEFT JOIN ON
FULL JOIN
Self JOINs
UNION /UNION ALL UNION will drop the duplicate rows but UNION can append all the rows UNION ALL is used more often
Operator Example Result
+ date '2001-09-28' + integer '7' date '2001-10-05'
+ date '2001-09-28' + interval '1 hour' timestamp '2001-09-28 01:00:00'
+ date '2001-09-28' + time '03:00' timestamp '2001-09-28 03:00:00'
+ interval '1 day' + interval '1 hour' interval '1 day 01:00:00'
+ timestamp '2001-09-28 01:00' + interval '23 hours' timestamp '2001-09-29 00:00:00'
+ time '01:00' + interval '3 hours' time '04:00:00'
DATA AGGREGATION:
NULL / ORDER BY / MAX / MIN / SUM / AVG / GROUP BY / HAVING / COUNT/ DISTINCT
DATE_TRUNC
DATE_PART
CASE WHEN THEN ELSE END AS
SUBQUERY:
记得可以用1,2等数字来代替
SELECT order_id, total_spending FROM xxx ORDER BY 1
WITH table AS
DATA CLEANING:
LEFT,RIGHT,SUBTRING(text,begin,length),LENGTH,STRPOS(text,arg) / POSITION(arg IN text)
CONCAT 与 ||相同
CAST 与 :: 相同,定义数据类型
COALESCE() 补全空白值
WINDOW FUNCTION:
OVER,ALIASE,LAG/LEAD,NTILE,RANK(),DENSE_RANK(),ROW_NUMBER(),PARTITION BY
LIMIT cannot speed up when there is an aggregation.
%%%%%%%%%%read the columns info%%%%%%%%%%%%%%
-- Pull column_name & data_type from the columns table
SELECT
column_name,
data_type
FROM information_schema.columns
-- Filter for the table 'country_stats'
WHERE table_name = 'table';
%%%% String functions %%%%%%%%%%%%%%%%%
The LOWER(fieldName) function changes the case of all characters in fieldName to lower case.
The INITCAP(fieldName) function changes the case of all characters in fieldName to proper case.
The LEFT(fieldName,N) function returns the left N characters of the string fieldName.
The SUBSTRING(fieldName from S for N) returns N characters starting from position S of the string fieldName. Note that both from S and for N are optional.
REPLACE(fieldName, 'searchFor', 'replaceWith')
TRIM([characters FROM ]string)
"characters FROM " Optional, Specific characters to remove.
"string" Required, The string to remove spaces or characters from