-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_sql_merge.sql
92 lines (84 loc) · 1.78 KB
/
06_sql_merge.sql
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
set hive.exec.dynamic.partition.mode=nonstrict;
set hive.optimize.sort.dynamic.partition=true;
CREATE DATABASE IF NOT EXISTS hwxdemo;
USE hwxdemo;
drop table if exists flights_new purge;
create table flights_new (
DateOfFlight date,
UniqueCarrier string,
FlightCount int
)
CLUSTERED BY (DateOfFlight) INTO 6 BUCKETS
STORED AS ORC
TBLPROPERTIES("transactional"="true")
;
INSERT INTO TABLE flights_new
SELECT * FROM flights_agg;
SET hive.merge.cardinality.check=false;
explain
MERGE INTO flights_new AS T USING flights_agg AS S
ON T.DateOfFlight = S.DateOfFlight AND S.UniqueCarrier = T.UniqueCarrier
WHEN MATCHED
AND S.UniqueCarrier = 'AA' AND S.Year>=2004
THEN UPDATE
SET UniqueCarrier = 'UA';
MERGE INTO flights_new AS T USING flights_csv AS S
ON T.DateOfFlight = S.DateOfFlight AND T.DepTime = S.DepTime AND T.Origin = S.Origin
AND T.Dest=S.Dest AND S.FlightNum = T.FlightNum
WHEN NOT MATCHED
AND S.UniqueCarrier = 'AA' AND Year>=2004
THEN INSERT VALUES
(s.DateOfFlight,
s.DepTime,
s.CRSDepTime,
s.ArrTime,
s.CRSArrTime,
'UA',
s.FlightNum,
s.TailNum,
s.ActualElapsedTime,
s.CRSElapsedTime,
s.AirTime,
s.ArrDelay,
s.DepDelay,
s.Origin,
s.Dest,
s.Distance,
s.TaxiIn,
s.TaxiOut,
s.Cancelled,
s.CancellationCode,
s.Diverted,
s.CarrierDelay,
s.WeatherDelay,
s.NASDelay,
s.SecurityDelay,
s.LateAircraftDelay)
WHEN NOT MATCHED
THEN INSERT VALUES
(s.DateOfFlight,
s.DepTime,
s.CRSDepTime,
s.ArrTime,
s.CRSArrTime,
s.UniqueCarrier,
s.FlightNum,
s.TailNum,
s.ActualElapsedTime,
s.CRSElapsedTime,
s.AirTime,
s.ArrDelay,
s.DepDelay,
s.Origin,
s.Dest,
s.Distance,
s.TaxiIn,
s.TaxiOut,
s.Cancelled,
s.CancellationCode,
s.Diverted,
s.CarrierDelay,
s.WeatherDelay,
s.NASDelay,
s.SecurityDelay,
s.LateAircraftDelay);