Postgres single query for function and temp table it generates - sql

I wonder if in Postgres there is any way to replace below 2-queries call by single query:
SELECT my_func(8,CURRENT_DATE);
SELECT * FROM tmp_table_generated_by_my_func
my_func is generating temp table want then use for other queries
I tried below but doesn't work (compiler seems doesn't recognise tmp table)
WITH
x AS(
SELECT * FROM my_func(8,CURRENT_DATE)
),
y AS(
SELECT * FROM tmp_table_generated_by_my_func
)
SELECT * FROM y

Related

Save a Select/Except Union into a Temp Table

This code does precisely what I want: finds the difference between two tables, including nulls, and returns them. Thanks to: sql query to return differences between two tables
(
SELECT * FROM table1
EXCEPT
SELECT * FROM table2
)
UNION ALL
(
SELECT * FROM table2
EXCEPT
SELECT * FROM table1
)
I am having trouble getting this to turn into a temporary table (or even a regular table) to store its results for later use. Is there a way that I can tack on INSERT INTO here or generate a temp table from this beautiful query?
Select from your existing query as a sub-query INTO the temp table of your choice.
SELECT *
INTO #temp1
FROM (
(
SELECT * FROM #table1
EXCEPT
SELECT * FROM #table2
)
UNION ALL
(
SELECT * FROM #table2
EXCEPT
SELECT * FROM #table1
)
) X

Is there a way to return a table given a input in sqlite?

I want to return the nth_value of a table given the value of an existing table column. But using that nested select affects the query's legibility: (ignore what demo is, just imagine that is a table with a sequential integer column ID)
WITH R (a,b) AS
(
VALUES(0, (SELECT first_value(ID) over() FROM demo))
UNION ALL
SELECT a+1 as a, (SELECT nth_value(ID, a+1) over() FROM demo) FROM R WHERE a<10
)
SELECT * FROM R
This would be better if I could make some kind of function that maps a given input to a nth_value of demo. Something that could make this could be like:
WITH R (a,b) AS
(
VALUES(0, get_demo_nth_value(0) )
UNION ALL
SELECT a+1 as a, get_demo_nth_value(a+1) FROM R WHERE a<10
)
SELECT * FROM R
We cannot create user-defined functions in SQLite, but is there a workaround? Something like a function that doesn't need to be defined as a procedure or other thing to map a value to a table.

How to add union data in table?

Thanks in advance !!
I want to get below data in separate table with column how can we achieved this.
From my reading of your question, you would like the results of that SELECT statement put into a new table?
Firstly, I'm assuming your original SQL works as a SELECT statement - e.g., all those tables have the same structure. Note that you can simplify the unions, but I haven't done so here, to keep the key part of the answer (saving the data) as the main focus.
To save the data into another table, you can either create a table first and make that into an insert, or just use 'SELECT INTO' within the main SELECT.
If you are happy with the columns being automatically created, the 'SELECT INTO' version will create columns (e.g., you do not need to specify the columns in a CREATE TABLE statement). However, when you run the SELECT INTO, it does create the table. Therefore if you want to insert further values, you need to specify the column list (or have matching column lists).
SELECT INTO version
select *
INTO #Temp -- Added This row
from
( select * from #OneyearExpiry
union all
select * from #OtherYearExpiry
) A
except
select * from
( select * from #ONEYRCON
union all
select * from #OTHERYRCON
) B
INSERT INTO version
CREATE TABLE #Temp (<your fields here to match the SELECT statement>)
INSERT INTO #Temp
select * from
( select * from #OneyearExpiry
union all
select * from #OtherYearExpiry
) A
except
select * from
( select * from #ONEYRCON
union all
select * from #OTHERYRCON
) B
Set operators are evaluated from top to bottom so there only needs to be 1 subquery. Something like this
select ab.* into #Temp
from (select * from #OneyearExpiry
union all
select * from #OtherYearExpiry
except
select * from #ONEYRCON
except
select * from #OTHERYRCON) ab;

Is there a SQL function to expand table?

I vaguely remember there being a function that does this, but I think I may be going crazy.
Say I have a datatable, call it table1. It has three columns: column1, column2, column3. The query
SELECT * FROM table1
returns all rows/columns from table1. Isn't there some type of EXPAND function that allows me to duplicate that result? For example, if I want to duplicate everything from the SELECT * FROM table1 query three times, I can do something like EXPAND(3) ?
In BigQuery, I would recommend a CROSS JOIN:
SELECT t1.*
FROM table1 CROSS JOIN
(SELECT 1 as n UNION ALL SELECT 2 UNION ALL SELECT 3) n;
This can get cumbersome for lots of copies, but you can simplify this by generating the numbers:
SELECT t1.*
FROM table1 CROSS JOIN
UNNEST(GENERATE_ARRAY(1, 3)) n
This creates an array with three elements and unnests it into rows.
In both these cases, you can include n in the SELECT to distinguish the copies.
Below is for BigQuery Standard SQL
I think below is close enough to what "got you crazy" o)
#standardSQL
SELECT copy.*
FROM `project.dataset.tabel1` t, UNNEST(FN.EXPAND(t, 3)) copy
To be able to do so, you can leverage recently announced support for persistent standard SQL UDFs, namely - you need to create FN.EXPAND() function as in below example (note: you need to have FN dataset in your project - or use existing dataset in which case you should use YOUR_DATASET.EXPAND() reference
#standardSQL
CREATE FUNCTION FN.EXPAND(s ANY TYPE, dups INT64) AS (
ARRAY (
SELECT s FROM UNNEST(GENERATE_ARRAY(1, dups))
)
);
Finally, if you don't want to create persistent UDF - you can use temp UDF as in below example
#standardSQL
CREATE TEMP FUNCTION EXPAND(s ANY TYPE, dups INT64) AS ( ARRAY(
SELECT s FROM UNNEST(GENERATE_ARRAY(1, dups))
));
SELECT copy.*
FROM `project.dataset.tabel1` t, UNNEST(EXPAND(t, 3)) copy
if you want a cartesian product (all the combination on a row ) you could use
SELECT a.*, b.*, c.*
FROM table1 a
CROSS JOIN table1 b
CROSS JOIN table1 c
if you want the same rows repeated you can use UNION ALL
SELECT *
FROM table1
UNION ALL
SELECT *
FROM table1
UNION ALL
SELECT *
FROM table1
Use union all
Select * from table1
Union all
Select * from table1
Union all
Select * from table1
Union all
Select * from table1
For reuse purposes can embed this code in a procedure like
Create Procedure
expandTable(tablename
varchar2(50))
As
Select * from table1
Union all
Select * from table1
Union all
Select * from table1
Union all
Select * from table1
End
/

SQL: Insert into temp table on nested common table expression

I want to insert into temp table inside the nested CTE as on below code. I can select from first x inside second expression but cannot insert into temp table.
WITH x AS
(
SELECT * FROM MyTable
),
y AS
(
SELECT * INTO #temp FROM x
)
SELECT * FROM y
I have to do this using nested CTE as there is other logic to implement. I know i can insert into temp table outside of expression. Is there a way to achieve this? Please help.
SQL Server is quite explicit. A query cannot both return a result set and save the results to a query. You can easily do:
WITH x AS (
SELECT * FROM MyTable
)
SELECT x.*
INTO #temp
FROM x;
And then:
SELECT t.*
FROM #temp t;
The first query saves the result set into a temporary table. The second returns the values from the query.