How do you save a join as a Snowflake table? - snowflake-cloud-data-platform

I understand how a Join is made in Snowflake but I need to temporarily save it as a table, how can it be possible make it using consola?
(For this project I can't do it with any connectors)

You are looking for the CREATE TABLE AS SELECT or CTAS pattern:
CREATE TABLE tmp_table AS
SELECT a.*, b.*
FROM (SELECT * FROM VALUES ('a'),('b')) a(string)
CROSS JOIN (SELECT * FROM VALUES (1),(2)) b(vals);
SELECT * FROM tmp_table;
gives:
STRING
VALS
a
1
a
2
b
1
b
2
and if you want it to be temporary for the session you might want to add TEMP
CREATE TEMP TABLE tmp_table AS

Related

TSQL - subquery inside Begin End

Consider the following query:
begin
;with
t1 as (
select top(10) x from tableX
),
t2 as (
select * from t1
),
t3 as (
select * from t1
)
-- --------------------------
select *
from t2
join t3 on t3.x=t2.x
end
go
I was wondering if t1 is called twice hence tableX being called twice (which means t1 acts like a table)?
or just once with its rows saved in t1 for the whole query (like a variable in a programming lang)?
Just trying to figure out how tsql engine optimises this. This is important to know because if t1 has millions of rows and is being called many times in the whole query generating the same result then there should be a better way to do it..
Just create the table:
CREATE TABLE tableX
(
x int PRIMARY KEY
);
INSERT INTO tableX
VALUES (1)
,(2)
Turn on the execution plan generation and execute the query. You will get something like this:
So, yes, the table is queried two times. If you are using complex common table expression and you are working with huge amount of data, I will advice to store the result in temporary table.
Sometimes, I am getting very bad execution plans for complex CTEs which were working nicely in the past. Also, you are allowed to define indexes on temporary tables and improve performance further.
To be honest, there is no answer... The only answer is Race your horses (Eric Lippert).
The way you write your query does not tell you, how the engine will put it in execution. This depends on many, many influences...
You tell the engine, what you want to get and the engine decides how to get this.
This may even differ between identical calls depending on statistics, currently running queries, existing cached results etc.
Just as a hint, try this:
USE master;
GO
CREATE DATABASE testDB;
GO
USE testDB;
GO
--I create a physical test table with 1.000.000 rows
CREATE TABLE testTbl(ID INT IDENTITY PRIMARY KEY, SomeValue VARCHAR(100));
WITH MioRows(Nr) AS (SELECT TOP 1000000 ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) FROM master..spt_values v1 CROSS JOIN master..spt_values v2 CROSS JOIN master..spt_values v3)
INSERT INTO testTbl(SomeValue)
SELECT CONCAT('Test',Nr)
FROM MioRows;
--Now we can start to test this
GO
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS;
GO
DECLARE #dt DATETIME2 = SYSUTCDATETIME();
--Your approach with CTEs
;with t1 as (select * from testTbl)
,t2 as (select * from t1)
,t3 as (select * from t1)
select t2.ID AS t2_ID,t2.SomeValue AS t2_SomeValue,t3.ID AS t3_ID,t3.SomeValue AS t3_SomeValue INTO target1
from t2
join t3 on t3.ID=t2.ID;
SELECT 'Final CTE',DATEDIFF(MILLISECOND,#dt,SYSUTCDATETIME());
GO
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS;
GO
DECLARE #dt DATETIME2 = SYSUTCDATETIME();
--Writing the intermediate result into a physical table
SELECT * INTO test1 FROM testTbl;
SELECT 'Write into test1',DATEDIFF(MILLISECOND,#dt,SYSUTCDATETIME());
select t2.ID AS t2_ID,t2.SomeValue AS t2_SomeValue,t3.ID AS t3_ID,t3.SomeValue AS t3_SomeValue INTO target2
from test1 t2
join test1 t3 on t3.ID=t2.ID
SELECT 'Final physical table',DATEDIFF(MILLISECOND,#dt,SYSUTCDATETIME());
GO
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS;
GO
DECLARE #dt DATETIME2 = SYSUTCDATETIME();
--Same as before, but with an primary key on the intermediate table
SELECT * INTO test2 FROM testTbl;
SELECT 'Write into test2',DATEDIFF(MILLISECOND,#dt,SYSUTCDATETIME());
ALTER TABLE test2 ADD PRIMARY KEY (ID);
SELECT 'Add PK',DATEDIFF(MILLISECOND,#dt,SYSUTCDATETIME());
select t2.ID AS t2_ID,t2.SomeValue AS t2_SomeValue,t3.ID AS t3_ID,t3.SomeValue AS t3_SomeValue INTO target3
from test2 t2
join test2 t3 on t3.ID=t2.ID
SELECT 'Final physical tabel with PK',DATEDIFF(MILLISECOND,#dt,SYSUTCDATETIME());
--Clean up (Careful with real data!!!)
GO
USE master;
GO
--DROP DATABASE testDB;
GO
On my system the
first takes 674ms, the
second 1.205ms (297 for writing into test1) and the
third 1.727ms (285 for writing into test2 and ~650ms for creating the index.
Although the query is performed twice, the engine can take advantage of cached results.
Conclusio
The engine is really smart... Don't try to be smarter...
If the table would cover a lot of columns and much more data per row the whole test might return something else...
If your CTEs (sub-queries) involve much more complex data with joins, views, functions and so on, the engine might get into troubles finding the best approach.
If performance matters, you can race your horses to test it out. One hint: I sometimes used a TABLE HINT quite successfully: FORCE ORDER. This will perform joins in the order specified in the query.
Here is a simple example to test the theories:
First, via temporary table which calls the matter only once.
declare #r1 table (id int, v uniqueidentifier);
insert into #r1
SELECT * FROM
(
select id=1, NewId() as 'v' union
select id=2, NewId()
) t
-- -----------
begin
;with
t1 as (
select * from #r1
),
t2 as (
select * from t1
),
t3 as (
select * from t1
)
-- ----------------
select * from t2
union all select * from t3
end
go
On the other hand, if we put the matter inside t1 instead of the temporary table, it gets called twice.
t1 as (
select id=1, NewId() as 'v' union
select id=2, NewId()
)
Hence, my conclusion is to use temporary table and not reply on cached results.
Also, ive implemented this on a large scale query that called the "matter" twice only and after moving it to temporary table the execution time went straight half!!

How to create a temporary table or CTE with values from two sources

Need to build a temporary table or a CTE for reporting purposes. Users will be able to select a location and courses from drop-downs.
The tables involved are the Person table that holds all employees and a Common Table Expression that will have the courses selected from drop down.
I need to be able to create a temporary table or a CTE with employee id field from the person table and a course name field from Course CTE.
For example, if courses A, B, C are selected each employee will have three records, one for each Course selected. So employee 1 will have three records in this temporary table or CTE. I'm using SQL Server 2008.
You can use INNER JOIN or UNION queries inside CTE to get values from Multiple Tables. So If you want to get the EMployeeId and CourseName for Each employee, YOu can Join the tables inside the CTE like this
;WITH CTE
AS
(
SELECT
E.EmployeeId,
C.CourseNm
FROM dbo.Employee E
INNER JOIN dbo.Course C
ON E.CourseId = C.CourseId
)
SELECT
*
FROM CTE
if you want to use Temp tables, you can try this
SELECT
E.EmployeeId,
C.CourseNm
INTO #Temp
FROM dbo.Employee E
INNER JOIN dbo.Course C
ON E.CourseId = C.CourseId
SELECT * FROM #Temp

Copy data from table and insert in different SQL Server database

I have two databases
databaseone
databasetwo
and I have a similar table in the database, name of the table is tableemployeedetails.
In my databaseone, I have 500 columns in the table tableemployeedetails.
In my databasetwo, I have 10 columns in the table tableemployeedetails.
I cannot use insert into select query because I want to insert the data into different database.
What is the best way to do this in my situation?
I just want to merge tableemployeedetails in both the databases
Try this,
insert into databasetwo..tableemployeedetails
SELECT * FROM databaseone..tableemployeedetails A
WHERE NOT EXISTS (SELECT 1 FROM databasetwo..tableemployeedetails B
WHERE A.COLUMN=B.COLUMN
)
If both databases has different records then you need two insert statements as below. If they have same then you need to prefer which database records are latest and then write an update in addition to the below insert.
insert into databasetwo..tableemployeedetails
SELECT * FROM databaseone..tableemployeedetails d1
left join databasetwo..tableemployeedetails d2 on A.PKKEY=B.PKKEY
where d2.PKKEY is null
insert into databaseone..tableemployeedetails
SELECT * FROM databasetwo..tableemployeedetails d2
left join databaseone..tableemployeedetails d1 on A.PKKEY=B.PKKEY
where d1.PKKEY is null

Copy data from one table into another new table where columns are unknown

I'm trying to copy all data from Table1 into Table2.
I don't know what and how many columns are their in table 1. I mean I want to copy even column names from table 1 to table 2.
There is option like
insert *
into #table2
from Table1
but I even can't use this because there are many select query which has already been written at past. So I have to do something like this.
insert *
into #table2
from (select * from Table1)
This is throwing an error
Incorrect syntax near )
Try This:
Select * into #table2 from (select * from table1 ) as X
---To copy along with data..
select * into newtable from oldtable
--to copy only schema..
select * into newtable from oldtable where 1=2

CROSS JOIN with one empty table sql server

I have two tables say MyFull and MyEmpty. I need to do the combination of all records from both tables. Sometimes, MyEmpty table might have no records, in this scenario I need to return all records from MyFull. This is what I have tried:
--Q1 >> returns no results
SELECT *
FROM MyFull CROSS JOIN MyEmpty
--Q2 >> returns no results
SELECT *
FROM MyFull, MyEmpty
I though of using LEFT JOIN but I have no common key to join on. This is a SQLFiddle
Try this:
SELECT *
FROM MyFull
LEFT JOIN MyEmpty ON 1=1
Though be aware that if you have multiple records in MyEmpty, it will repeat records from MyFull once for every record in MyEmpty. Effectively, the number of records in the results is MyFull * MyEmpty (unless MyEmpty has no records).
Full table :
declare #t table (Spot int,name varchar(1),pct int)
insert into #t(Spot,name,pct)values(1,'A',2),(1,'B',8),(1,'C',6),(2,'A',4),(2,'B',5),(3,'A',5),(3,'D',1),(3,'E',4)
Empty table :
declare #tt table (Spot int,name varchar(1),pct int)
select * from #t OUTER APPLY #tt

Resources