Snowflake insert into a table from CTE output results - snowflake-cloud-data-platform

with
ct2 (emp_name,emp_id) as (
select emp_name,emp_id
from "TEST_1"."PUBLIC"."TEST11"
)
insert into "TEST_1"."PUBLIC"."EMP1"
select emp_name,emp_id
from ct2;

I guess you are looking for the correct syntax to achieve the above.
Try this:
insert into "TEST_1"."PUBLIC"."EMP1"
with ct2 (emp_name,emp_id) as (select emp_name,emp_id from "TEST_1"."PUBLIC"."TEST11")
select emp_name,emp_id from ct2;

Related

I want to delete all the records return by below query

I want to use delete instead of select clause(snowflake).
WITH duplicates as(
select row_number()over(partition by ASCII,keyt,prodt,counter,batch_id ,batch_time,overall_level,is_f,is_s,is_c order
by batch_time DESC)as r,
* from temp_table
where batch_time BETWEEN '2021-12-01' and '2021-12-31')
select count(*) from duplicates as d where r>1 order by keyt;
from the query you that you have given it looks like you don't have column which makes up the unique key, so one option is as follows.
--- not tested
CREATE TABLE new_table LIKE temp_table ;
INSERT INTO new_table SELECT DISTINCT * FROM temp_table;
ALTER TABLE temp_table SWAP WITH new_table;

Getting a Row Count and keeping the columns with no rows are returned

If I run something like this:
select [agent_name], [agent_department], count(*) as [row_count]
from [table_name]
where [agent_name] IS NOT NULL
group by [agent_name] [agent_department];
Nothing will come back if there are no records to return (i.e. the table is empty).
If I run this
select count(*) as [row_count]
from [table_name]
where [agent_name] IS NOT NULL
I will get a row_count of 0.
Is there a way I can run the first query, and, if there are no records, have it return row_count 0?
This might not be very beautiful, but it should bring back what you want:
I start with a tiny mockup:
DECLARE #mockup TABLE(agent_name varchar(100),agent_department varchar(100));
--The query will read your SELECT within a CTE.
WITH cte AS
(
select [agent_name], [agent_department], count(*) as [row_count]
from #mockup
where [agent_name] IS NOT NULL
group by [agent_name],[agent_department]
)
SELECT agent_name,agent_department,row_count FROM cte
UNION ALL SELECT NULL,NULL,0 WHERE (SELECT COUNT(*) FROM cte)=0;
The result
agent_name agent_department row_count
NULL NULL 0
You see, that the resultset is called as is, while there is a UNION ALL SELECT query, which will deliver only in cases, where the cte has no rows.
Now we insert some data to the table
INSERT INTO #mockup VALUES('blah','blub');
WITH cte AS
(
select [agent_name], [agent_department], count(*) as [row_count]
from #mockup
where [agent_name] IS NOT NULL
group by [agent_name],[agent_department]
)
SELECT agent_name,agent_department,row_count FROM cte
UNION ALL SELECT NULL,NULL,0 WHERE (SELECT COUNT(*) FROM cte)=0;
the new result is now
agent_name agent_department row_count
blah blub 1

Joining with Temp table outside XML element

I am new to XML+SQL module and I have a code that selects a regular column and a whole bunch of XML data.
Below is my sample code:
create table #temp(cid int, val int)
insert into #temp values
(1,11),
(2,12),
(3,12)
select
t1.cid,
xml =
(
select t2.cid,t2.val
from #temp t2
join #temp t1 on t2.cid = t1.cid
for xml Path(''), type)
from #temp t1
drop table #temp
desired output is:
Rexter link: http://rextester.com/HLZS59752
Any help ??
If I understand your question.
Example
select
t1.cid,
xml = (Select t1.* for xml path('') )
from #temp t1
Returns
cid xml
1 <cid>1</cid><val>11</val>
2 <cid>2</cid><val>12</val>
3 <cid>3</cid><val>12</val> -- Last record in #temp is (3,12)
Thanks #John Cappelletti for that answer. That helped. One mroe solution I found was:
select
t1.cid,
xml =
(
select t2.cid,t2.val
from #temp t2
where t1.cid = t2.cid
for xml Path(''), type)
from #temp t1
Instead of join, I added the condition in Where clause and it worked.
Updated Rexter link: http://rextester.com/MGXDC39580

How to pass a query as a variable

DECLARE #SomeVariableTable TABLE (
Field INT
)
INSERT INTO #SomeVariableTable
SELECT st.Field
FROM SomeTable st;
SELECT *
FROM AnotherTable at
WHERE at.SomeField IN #SomeVariableTable
How can I make the above code work?
I have looked at concatenating the entire column into a VARCHAR and while this works for VARCHARs it doesn't work for INTs.
Your code is almost fine except the last SELECT. Change the last SELECT part as below.
SELECT *
FROM AnotherTable at
WHERE at.SomeField IN (select Field from #SomeVariableTable)
This is how you would do what you are trying to do .....
DECLARE #SomeVariableTable TABLE (
Field INT
)
INSERT INTO #SomeVariableTable
SELECT st.Field
FROM SomeTable st;
SELECT *
FROM AnotherTable at
WHERE at.SomeField IN (
SELECT Field
FROM #SomeVariableTable
)
But why would you do this anyway? you could have simply done something like
SELECT *
FROM AnotherTable at
WHERE at.SomeField IN (
SELECT Field
FROM SomeTable
)

How to sort date in mssqlserver

I want to get distinct dates from my dbtable named tblFormno2 in an ascending order.For that i've written the following query but its not working properly.
Column date_submit is declared as datetime
select distinct (convert(nvarchar(100),date_submit,103)) as dob from
tblFormno2 order by dob asc
Here the the output is shown as
05/07/2011
06/03/2011
06/07/2011
07/04/2011
08/01/2012
instead of
06/03/2011
07/04/2011
05/07/2011
06/07/2011
08/01/2012
How to solve this problem ???
How about
select convert(nvarchar(10), date_submit_inner, 103) as date_submit from
(
select distinct date_submit as date_submit_inner from tblFormno2
) as T
order by T.date_submit_inner asc
Your order by is not sorting by date_submit from the table. Is is sorting by the named output column of date_submit. If you specific the table name in the order by it should work. If that doesn't work, then try giving the output a different name than the table column.
select distinct (Convert(nvarchar(100),date_submit,103)) as date_submit
from tblFormno2
order by tblFormno2.date_submit asc
create table #temp
(
DT varchar(20)
)
Insert into #temp(DT)values('13/05/2011')
Insert into #temp(DT)values('03/06/2011')
Insert into #temp(DT)values('07/06/2011')
Insert into #temp(DT)values('04/07/2011')
Insert into #temp(DT)values('01/08/2011')
Select * from #temp
Below are the database records...
select (convert(varchar,Dt,107)) t into #t from #temp
select * from #t
drop table #temp
drop table #t

Resources