i have a one select statement
select a,b,c,d,e,f from test where sample='one'
another select statement
select number,date,c,g,h,i test where sample='two'
output query should be
select number,date,c,d,e,f,g,h,i
If join on what column it should be
how to achieve this, using union or joins?
Related
Lets assume I have something like the following simplified query for big query:
WITH test AS (
SELECT 1 AS fieldA, 2 AS fieldB
)
SELECT fieldA, fieldB
FROM test
UNION ALL
SELECT fieldB, fieldA
FROM test;
Will Big Query run the test CTE twice or only once and then share the data between both parts of the union?
I searched before posting this and I know that a CTE query only lives for one SQL statement. But here - there is only one statement which uses same CTE twice and I could not find something similar.
Of course CTE query is more complex in real life scenario and might contain a ROW NUMBER window function and also JOINS.
Thanks a lot.
The documentation is clear for the tables in the with clause (CTE).
BigQuery only materializes the results of recursive CTEs, but does not
materialize the results of non-recursive CTEs inside the WITH clause.
If a non-recursive CTE is referenced in multiple places in a query,
then the CTE is executed once for each reference.
This can be tested by adding a column with rand(). For each usage it will have its own value.
WITH RECURSIVE test AS (
SELECT "normal" AS fieldA, 2 AS fieldB, RAND() AS R),
test_recursive AS
(SELECT "recursive" , 9, RAND() AS R
UNION ALL SELECT * FROM test_recursive
WHERE FALSE )
SELECT * FROM test
UNION ALL SELECT * FROM test
UNION ALL SELECT * FROM test
UNION ALL SELECT * FROM test
UNION ALL SELECT * FROM test_recursive
UNION ALL SELECT * FROM test_recursive
UNION ALL SELECT * FROM test_recursive
order by 1
All real recursive CTEs have the same value for the random one. Therefore, the CTE was only caluculated once.
This query also shows that with two extra lines every CTE can by materialized.
Currently
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
outputs only distinct. What if I want to output all results?
Use UNION ALL in place of UNION, it does not remove duplicate rows and will display all results, like you are looking for.
I would like to use a with structure with multiple sql select queries. Ex:
;with temptable as ( ... )
select id from temptable
select name from temptable
However after the first select query is done, SQL Server 2008 doesnt allow me to do it and it pushes me to write the same with structure again above the second query. Ex:
;with temptable as ( ... )
select id from temptable
;with temptable as ( ... )
select name from temptable
I tried using comma after the first select query but didnt work. How can I use multiple select queries under one with structure in SQL Server 2008.
Common table expression works only for one statement.
Specifies a temporary named result set, known as a common table
expression (CTE). This is derived from a simple query and defined
within the execution scope of a single SELECT, INSERT, UPDATE, or
DELETE statement.
select id from temptable;
select name from temptable;
are two statements, so you cannot use it in second query.
The alternative is to use temp table:
SELECT .... INTO #temptable FROM ...; -- your query from CTE
SELECT id FROM #temptable;
SELECT name FROM #temptable;
CTE only exists for the "duration of the query" you can't use the same CTE in two different SELECT statements as each is a separate query.
you need to store the result set of CTE into temp table to and fire multiple select statements on temp table.
;with temptable as ( ... )
select id
into #tempResultSet
from temptable
select name from tempResultSet
I would like to pull all of the columns from my second select statement and put it to the right of all of my columns from my first statement.
I have tried Union and the join commands with no luck.
When I use these they just have what I wanted from my first select statement.
Here is basic code I have.
Select * from MTG_TREND where LINEID='A2' end;
Select * from MTG_TREND where LINEID='B2'
All of the other columns are the same.
Select t1.*,t2.* from MTG_TREND t1
left join (
Select * from MTG_TREND where LINEID='B2'
) as t2
on t1.primarykey=t2.primarykey
where t1.LINEID='A2'
NOTE:
- Make sure you are matching the same number of columns in both SELECT Statement.
- Another thing to remember is you have to have the same matched column (datatype).
- If there are some columns that are not present is the table then just defined the column with null so that you can get it in output from another select statement.
Select col1, col2, col3,...... from MTG_TREND where LINEID='A2'
UNION ALL
Select col1, col2, col3,...... from MTG_TREND where LINEID='B2'
I have a temporary table in my procedure; I tried to insert data from a select statement like this:
INSERT INTO #temptable
SELECT fee, expense, total FROM invoice
UNION
SELECT vat, holdingtax, total FROM uplifts
...but in my temptable, only the first select statement gets populated into the table, while the next select statement does not insert the data.
I assume the UNION is removing duplicated data.
I just made a test with union ALL:
insert into #temptable
select top 1 name from sys.tables
union all
select top 1 name from sys.tables a
and I got (2 row(s) affected)
try to replace UNION by UNION ALL
The syntax looks ok,
Its possible that there is duplication in the 2nd table.
You could test this using a Union All as opposed to Union.
I would just run the query without the insert to see if you return the results you expect.