From fec53d46d38a00f67292dcba828b1838683b3b6c Mon Sep 17 00:00:00 2001
From: Admin_mschuemi Similarly, many SQL syntax structures are supported. Here is a
non-exhaustive lists of things that we know will translate well: However, Oracle will throw an error when the Note that the user will need to have write privileges on
@@ -485,11 +485,11 @@ you could use Even though You should therefore always make casts explicit. In the above
example, the last statement should be replaced with either or it is preferred to use where on SQL Server we can include both database and schema names in
the value: we can set which will translate into the following on PDW: Another tuning parameter is the key to sort a table on. This can be
also be specified in a hint: translates to the following on RedShift: The hints should be formatted exactly as shown above, and directly
precede the statement where the table is created.Using SqlRender
Martijn J.
Schuemie
- 2023-08-10
+ 2023-09-20
Source: vignettes/UsingSqlRender.Rmd
UsingSqlRender.Rmd
Functions and structure
SELECT * FROM table; -- Simple selects
-
-SELECT * FROM table_1 INNER JOIN table_2 ON a = b; -- Selects with joins
-
-SELECT * FROM (SELECT * FROM table_1) tmp WHERE a = b; -- Nested queries
-
-SELECT TOP 10 * FROM table; -- Limiting to top rows
-
-SELECT * INTO new_table FROM table; -- Selecting into a new table
-
-CREATE TABLE table (field INT); -- Creating tables
-
-INSERT INTO other_table (field_1) VALUES (1); -- Inserting verbatim values
-
-INSERT INTO other_table (field_1) SELECT value FROM table; -- Inserting from SELECT
-
- DROP TABLE my_table; -- Simple drop commands
-
-DROP TABLE IF EXISTS ACHILLES_analysis; -- Drop table if it exists
-
-WITH cte AS (SELECT * FROM table) SELECT * FROM cte; -- Common table expressions
-
-SELECT ROW_NUMBER() OVER (PARTITION BY a ORDER BY b) -- OVER clauses
-AS "Row Number" FROM table;
-
- SELECT CASE WHEN a=1 THEN a ELSE 0 END AS value FROM table; -- CASE WHEN clauses
-
-SELECT * FROM a UNION SELECT * FROM b -- UNIONs
-
-SELECT * FROM a INTERSECT SELECT * FROM b -- INTERSECTIONs
-
-SELECT * FROM a EXCEPT SELECT * FROM b -- EXCEPT
-
-CAST('20220101' AS DATE) -- Cast to date
SELECT * FROM table; -- Simple selects
+
+SELECT * FROM table_1 INNER JOIN table_2 ON a = b; -- Selects with joins
+
+SELECT * FROM (SELECT * FROM table_1) tmp WHERE a = b; -- Nested queries
+
+SELECT TOP 10 * FROM table; -- Limiting to top rows
+
+SELECT * INTO new_table FROM table; -- Selecting into a new table
+
+CREATE TABLE table (field INT); -- Creating tables
+
+INSERT INTO other_table (field_1) VALUES (1); -- Inserting verbatim values
+
+INSERT INTO other_table (field_1) SELECT value FROM table; -- Inserting from SELECT
+
+DROP TABLE my_table; -- Simple drop commands
+
+DROP TABLE IF EXISTS ACHILLES_analysis; -- Drop table if it exists
+
+WITH cte AS (SELECT * FROM table) SELECT * FROM cte; -- Common table expressions
+
+SELECT ROW_NUMBER() OVER (PARTITION BY a ORDER BY b) -- OVER clauses
+ AS "Row Number" FROM table;
+
+SELECT CASE WHEN a=1 THEN a ELSE 0 END AS value FROM table; -- CASE WHEN clauses
+
+SELECT * FROM a UNION SELECT * FROM b -- UNIONs
+
+SELECT * FROM a INTERSECT SELECT * FROM b -- INTERSECTIONs
+
+SELECT * FROM a EXCEPT SELECT * FROM b -- EXCEPT
+
+CAST('20220101' AS DATE) -- Cast to date
String concatenation
@@ -419,21 +419,21 @@
Table aliases and the AS keyword
-
-- Using AS keyword
-SELECT *
-FROM my_table AS table_1
-INNER JOIN (
-SELECT * FROM other_table
- AS table_2
- ) ON table_1.person_id = table_2.person_id;
-
--- Not using AS keyword
-SELECT *
-FROM my_table table_1
-INNER JOIN (
-SELECT * FROM other_table
-
- ) table_2ON table_1.person_id = table_2.person_id;
-- Using AS keyword
+SELECT *
+FROM my_table AS table_1
+INNER JOIN (
+ SELECT * FROM other_table
+) AS table_2
+ON table_1.person_id = table_2.person_id;
+
+-- Not using AS keyword
+SELECT *
+FROM my_table table_1
+INNER JOIN (
+ SELECT * FROM other_table
+) table_2
+ON table_1.person_id = table_2.person_id;
AS
keyword
is used. In the above example, the first query will fail. It is
therefore recommended to not use the AS
keyword when
@@ -466,7 +466,7 @@ Temp tables
sql <- "SELECT * FROM #children;"
translate(sql, targetDialect = "oracle", tempEmulationSchema = "temp_schema")
## [1] "SELECT * FROM temp_schema.dx224bvxchildren ;"
+
## [1] "SELECT * FROM temp_schema.pae80i6xchildren ;"
## attr(,"sqlDialect")
## [1] "oracle"
Temp tables
+SELECT * INTO #children FROM person WHERE year_of_birth > 2000;
-SELECT * FROM #children WHERE gender = 8507;
SELECT * INTO #children FROM person WHERE year_of_birth > 2000;
+SELECT * FROM #children WHERE gender = 8507;
WITH children AS (SELECT * FROM person WHERE year_of_birth > 2000)
-SELECT * FROM children WHERE gender = 8507;
WITH children AS (SELECT * FROM person WHERE year_of_birth > 2000)
+SELECT * FROM children WHERE gender = 8507;
Implicit casts
@@ -497,12 +497,12 @@
Implicit castsOne of the few points where SQL Server is less explicit than other
dialects is that it allows implicit casts. For example, this code will
work on SQL Server:
-
CREATE TABLE #temp (txt VARCHAR);
-
-INSERT INTO #temp
-SELECT '1';
-
-SELECT * FROM #temp WHERE txt = 1;
CREATE TABLE #temp (txt VARCHAR);
+
+INSERT INTO #temp
+SELECT '1';
+
+SELECT * FROM #temp WHERE txt = 1;
txt
is a VARCHAR field and we are comparing
it with an integer, SQL Server will automatically cast one of the two to
the correct type to allow the comparison. In contrast, other dialects
@@ -510,9 +510,9 @@ Implicit casts
SELECT * FROM #temp WHERE txt = CAST(1 AS VARCHAR);
SELECT * FROM #temp WHERE CAST(txt AS INT) = 1;
Case sensitivity in string comparisons
@@ -522,9 +522,9 @@
Case sensitivity in string compa
are always case sensitive. It is therefore recommended to always assume
case-sensitive comparisons, and to explicitly make comparisons
case-insensitive when unsure about the case. For example, instead of
-
SELECT * FROM concept WHERE concep_class_id = 'Clinical Finding'
SELECT * FROM concept WHERE LOWER(concep_class_id) = 'clinical finding'
Schemas and databases
@@ -544,7 +544,7 @@
Schemas and databases@databaseSchema. For example, we could have the
parameterized SQL
-
SELECT * FROM @databaseSchema.person
databaseSchema = "cdm_data.dbo"
. On other
platforms, we can use the same code, but now only specify the schema as
@@ -557,9 +557,9 @@ Schemas and databases@database and the other called
@databaseSchema
. For example, for this parameterized
SQL:
-SELECT * FROM @databaseSchema.person;
-USE @database;
-SELECT * FROM person
database = "cdm_data"
and the other called
databaseSchema = "cdm_data.dbo"
. On platforms other than
SQL Server, the two variables will hold the same value and only on SQL
@@ -601,21 +601,21 @@ Optimization for massive
optimizations, so we’ve introduced the notion of hints. In the following
example, a hint is provided on which field should be used for the
distribution of data across nodes:
-
--HINT DISTRIBUTE_ON_KEY(person_id)
-SELECT * INTO one_table FROM other_table;
--HINT DISTRIBUTE_ON_KEY(person_id)
-IF XACT_STATE() = 1 COMMIT;
-CREATE TABLE one_table WITH (DISTRIBUTION = HASH(person_id)) AS
-SELECT * FROM other_table;
--HINT DISTRIBUTE_ON_KEY(person_id)
+IF XACT_STATE() = 1 COMMIT;
+CREATE TABLE one_table WITH (DISTRIBUTION = HASH(person_id)) AS
+SELECT * FROM other_table;
--HINT SORT_ON_KEY(INTERLEAVED:start_date)
-CREATE TABLE cdm.my_table (row_id INT, start_date);
--HINT SORT_ON_KEY(INTERLEAVED:start_date)
-CREATE TABLE cdm.my_table (row_id INT, start_date)
- INTERLEAVED SORTKEY(start_date);
--HINT SORT_ON_KEY(INTERLEAVED:start_date)
+CREATE TABLE cdm.my_table (row_id INT, start_date)
+INTERLEAVED SORTKEY(start_date);
Developing R packa
loadRenderTranslateSql()
function . For example, suppose we
have a text file called test.sql containing the following
parameterized SQL:
DEFAULT @selected_value = 1}
- {SELECT * FROM table INTO result where x = @selected_value;
Then the command
createRWrapperForSql(sqlFilename = "test.sql",
diff --git a/docs/articles/index.html b/docs/articles/index.html
index 5b09725b..fa98f156 100644
--- a/docs/articles/index.html
+++ b/docs/articles/index.html
@@ -17,7 +17,7 @@
repository>
- <id>ohdsi</id>
- <name>repo.ohdsi.org</name>
- <url>https://repo.ohdsi.org/nexus/content/repositories/releases</url>
- <repository>
- </repository>
- <id>ohdsi.snapshots</id>
- <name>repo.ohdsi.org-snapshots</name>
- <url>https://repo.ohdsi.org/nexus/content/repositories/snapshots</url>
- <releases>
- <enabled>false</enabled>
- <releases>
- </snapshots>
- <enabled>true</enabled>
- <snapshots>
- </repository> </
<repository>
+ <id>ohdsi</id>
+ <name>repo.ohdsi.org</name>
+ <url>https://repo.ohdsi.org/nexus/content/repositories/releases</url>
+</repository>
+<repository>
+ <id>ohdsi.snapshots</id>
+ <name>repo.ohdsi.org-snapshots</name>
+ <url>https://repo.ohdsi.org/nexus/content/repositories/snapshots</url>
+ <releases>
+ <enabled>false</enabled>
+ </releases>
+ <snapshots>
+ <enabled>true</enabled>
+ </snapshots>
+</repository>
2: Include the SqlRender dependency in your pom.xml
-dependency>
- <groupId>org.ohdsi.sql</groupId>
- <artifactId>SqlRender</artifactId>
- <version>1.9.2-SNAPSHOT</version>
- <dependency> </
<dependency>
+ <groupId>org.ohdsi.sql</groupId>
+ <artifactId>SqlRender</artifactId>
+ <version>1.9.2-SNAPSHOT</version>
+</dependency>
Changes:
+Adding support for ALTER TABLE ADD
for SQLite and PostgreSQL.
The render()
, translate()
, and translateSingleStatement()
functions now preserve attributes of the SQL object.
Adding support for IIF
for Synapse.
Translating double quotes to backticks for BigQuery.
Bugfixes:
+Fix translation of drvd()
for Snowflake.
Fix translation of ‘a.b.c…d’ pattern for Snowflake.
Bugfixes:
DATEADD()
for DuckDB when number to add is not an integer.getTempTablePrefix()
-#> [1] "lvtr1coh"
+#> [1] "aye03t4s"
WA`;|zu?jnc
z7>vMB@^xhmDzu6nhG&uNH44@+AtJ-^ygYf7Ry5F}%8nGpVrXjWTn02&?F&HT8S<=%
z0k9~)8oz*Q3ocL45Hd1ssIw8P?FffisPC!^LA@%8C`!O0xsN3o1UO@^Df#$lt($@4
z2|+ks?cl%*uYQBZA|m4u5`c<7n!&VsAKWLf#(ic=ps|2y6*Dx$(7ei_z@^114Vo#C
zMi@XsU1iaXKoe& A`PglGiwN=
z9GvaxCW8^qBo6vPxWByKFy3r1(S~6Z%{Imcg^s6SKZ8D-sA{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