-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigquery.sql
52 lines (41 loc) · 1.58 KB
/
bigquery.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
-- load ndvi data
CREATE OR REPLACE EXTERNAL TABLE `data-enginerring-zoomcamp.farm_dataset.ndvi`
OPTIONS (
format = 'CSV',
uris = ['gs://sammy_project_bucket2024/ndvi/*.csv']);
-- load ndvi data
CREATE OR REPLACE EXTERNAL TABLE `data-enginerring-zoomcamp.farm_dataset.ndmi`
OPTIONS (
format = 'CSV',
uris = ['gs://sammy_project_bucket2024/ndmi/*.csv']);
-- load
CREATE OR REPLACE EXTERNAL TABLE `data-enginerring-zoomcamp.farm_dataset.farm_boundary2` OPTIONS (
format="NEWLINE_DELIMITED_JSON",
json_extension = 'GEOJSON',
uris = ['gs://sammy_project_bucket2024/farmland_features.json']
);
CREATE OR REPLACE TABLE `data-enginerring-zoomcamp.farm_dataset.farm_info` AS
-- Create or replace the table `farm_info` in the dataset `data-enginerring-zoomcamp.farm_dataset`
SELECT
farm.id,
farm.statename,
farm.lganame,
farm.wardname,
farm.urban,
farm.landuse,
ROUND(veg_indices.ndvi, 2) AS farm_ndvi, -- Select and round the `ndvi` values to two decimal places
ROUND(veg_indices.ndmi, 2) AS farm_ndmi, -- Select and round the `ndmi` values to two decimal places
veg_indices.processed_date,
farm.geometry
FROM
`data-enginerring-zoomcamp.farm_dataset.farm_boundary2` as farm
LEFT JOIN
(SELECT DISTINCT ndvi.id, ndvi.ndvi, ndmi.ndmi, ndvi.processed_date
FROM
farm_dataset.ndvi AS ndvi -- Alias the `ndvi` table as `ndvi`
INNER JOIN
farm_dataset.ndmi AS ndmi -- Alias the `ndmi` table as `ndmi`
ON
ndvi.id = ndmi.id ) as veg_indices
ON
farm.id = veg_indices.id