It is important to understand the naming convetion: [sorting]-[tableName].le.sql
As we already have a model named 001-test2.le.sql
we now create a file named 002-test3.le.sql
. 002 is for sorting in your file structure only. The table name that will be created by this model is table3.
touch workflows/zzzexampleproject/models/002-test3.le.sql
Add this to the file
{{ block "CONFIG" .}}
{{ setTimestampColumn "date2"}}
{{end}}
{{ block "SELECT" .}}
SELECT
"dimension1",
"kpi1",
"date" AS "date2",
'step2'::TEXT AS "step"
FROM
{{ .Project }}.{{ ref "test2" }}
WHERE
"date" BETWEEN '{{ .StartDate }}' and '{{ .EndDate }}'
{{end}}
As the model test2 this one does three steps as well:
Based on the the SELECT statement, lesql analyzes which columns have to be created. You could and in many cases you should cast the data type, as shown for the step column.
CREATE TABLE IF NOT EXISTS "zzzexampleproject"."test3" (
"dimension1" VARCHAR,
"kpi1" NUMERIC,
"date2" DATE,
"step" TEXT
);
The test3 table does not have a date column, but a date2 column. Therefore we have to tell lesql to use the date2 column to identify which timeperiod to delete. This is done in the CONFIG block of the model file: {{ setTimestampColumn "date2"}}
DELETE FROM "zzzexampleproject"."test3" WHERE "date2" BETWEEN '2019-10-24' AND '2019-11-23';
blub