Union All have mismatched column count - google-bigquery

I am trying to union some table. But I am having some issues with the query as I have the following error:
Queries in UNION ALL have mismatched column count; query 1 has 10 columns, query 2 has 12 columns at [3:1]
The query used is:
SELECT * FROM `table_1`
UNION ALL
SELECT * ,null, FROM `table_2`
UNION ALL
SELECT * FROM `table_3`
UNION ALL
SELECT * FROM `table_4`
UNION ALL
SELECT * , null, FROM `table_5`
Please someone have any suggestions?

In a UNION ALL all queries need to be the same number of columns.
You need to post the schema of all tables. But according to the error message you posted, this could help for the 2 first tables:
SELECT *, null FROM `table_1` -- assuming table_1 has 10 columns, add 1 more empty to match the table_2
UNION ALL
SELECT * FROM `table_2` -- assuming table_2 has 11 columns
-- make the same to the rest of queries
-- UNION ALL
-- SELECT * FROM `table_3`
-- UNION ALL
-- SELECT * FROM `table_4`
-- UNION ALL
-- SELECT * , null, FROM `table_5`
Update:
Adding a example of union all declaring the columns:
SELECT
col1_str,
col2_str,
col3_int,
col4_int
FROM `table_1`
UNION ALL
SELECT
col2_str,
FORMAT_DATE("%Y-%m-%d", col1_date) as col1_str, -- transform a date column to string
col3_int,
null
FROM `table_2`

Related

SQL Select query optimization with indexing

A posts table contains 1 million rows. This table has a field with the name poster_id.
I have a list of followers by this poster_id.
I am trying to get a list of all activities from this followers (35 in this case but less or more is possible) in the last 48 hours.
I use this query:
SELECT post_id
, topic_id
, poster_id
, post_time
FROM posts
WHERE post_time > 1606833542
AND poster_id IN (80202, 74247, 79290, 72488,
111751, 85040, 100256, 68025,
101088, 101598, 101950, 103252,
103071, 80063, 100372, 102530, 109961,
109854, 105626, 108967, 110391, 104423,
113243, 111673, 113979, 104670, 127318,
68252, 109606, 121393, 122991, 124489,
127723, 126525)
ORDER
by post_time
LIMIT 100
Problem:
This query takes too long (0.4000 seconds) to execute.
The poster_id has an index of the post table.
How can I make this query faster?
try avoid the IN clause and use a join
SELECT
p.post_id
, p.topic_id
, p.poster_id
, p.post_time
FROM posts p
INNER JOIN (
SELECT 80202 poster_id
UNION SELECT 74247
UNION SELECT 79290
UNION SELECT 72488
UNION SELECT 111751
UNION SELECT 85040
UNION SELECT 100256
UNION SELECT 68025
UNION SELECT 101088
UNION SELECT 101598
UNION SELECT 101950
UNION SELECT 103252
UNION SELECT 103071
UNION SELECT 80063
UNION SELECT 100372
UNION SELECT 102530
UNION SELECT 109961
UNION SELECT 109854
UNION SELECT 105626
UNION SELECT 108967
UNION SELECT 110391
UNION SELECT 104423
UNION SELECT 113243
UNION SELECT 111673
UNION SELECT 113979
UNION SELECT 104670
UNION SELECT 127318
UNION SELECT 68252
UNION SELECT 109606
UNION SELECT 121393
UNION SELECT 122991
UNION SELECT 124489
UNION SELECT 127723
UNION SELECT 126525
) t ON t.poster_id = p.poster_id
AND p.post_time > 1606833542
ORDER by p.post_time LIMIT 100
could be the value in the IN clause are form some subquery in this case ypou could use the related subquery instead of the UNION .....
WHERE IN clase is the same as serverl OR condition (several internal subquery) .. instead INNER JOIN just check the values in a single operation
for better performance, instead of you actual index on poster_id, you could try using a redundant index adding all the columns you select in your index eg:
create index my_index on posts (poster_id, post_id, topic_id , post_time )
in this way all the query value are obtained using the index and the query don't need to access at the table ..

How to create table with multiple rows and columns using only SELECT clause (i.e. using SELECT without FROM clause)

I know that in SQL Server, one can use SELECT clause without FROM clause, and create a table with one row and one column
SELECT 1 AS n;
But I was just wondering, is it possible to use SELECT clause without FROM clause, to create
a table with one column and multiple rows
a table with multiple columns and one row
a table with multiple columns and multiple rows
I have tried many combinations such as
SELECT VALUES(1, 2) AS tableName(n, m);
to no success.
You can do it with CTE and using union(Use union all if you want to display duplicates)
Rextester Sample for all 3 scenarios
One Column and multiple rows
with tbl1(id) as
(select 1 union all
select 2)
select * from tbl1;
One row and multiple columns
with tbl2(id,name) as
(select 1,'A')
select * from tbl2;
Multiple columns and multiple rows
with tbl3(id,name) as
(select 1,'A' union all
select 2,'B')
select * from tbl3;
-- One column, multiple rows.
select 1 as ColumnName union all select 2; -- Without FROM;
select * from ( values ( 1 ), ( 2 ) ) as Placeholder( ColumnName ); -- With FROM.
-- Multiple columns, one row.
select 1 as TheQuestion, 42 as TheAnswer; -- Without FROM.
select * from ( values ( 1, 42 ) ) as Placeholder( TheQuestion, TheAnswer ); -- With FROM.
-- Multiple columns and multiple rows.
select 1 as TheQuestion, 42 as TheAnswer union all select 1492, 12; -- Without FROM.
select * from ( values ( 1, 2 ), ( 2, 4 ) ) as Placeholder( Column1, Column2 ); -- With FROM.
You can do all that by using UNION keyword
create table tablename as select 1 as n,3 as m union select 2 as n,3 as m
In Oracle it will be dual:
create table tablename as select 1 as n,3 as m from dual union select 2 as n,3 as m from dual
You can use UNION operator:
CREATE TABLE AS SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
The UNION operator selects only distinct values by default. To allow duplicate values you can use UNION ALL.
The column names in the result-set are usually equal to the column names in the first SELECT statement in the UNION.
try this:
--1) a table with one column and multiple rows
select * into tmptable0 from(
select 'row1col1' as v1
union all
select 'row2col1' as v1
) tmp
--2) a table with multiple columns and one row
select 'row1col1' as v1, 'row1col2' as v2 into tmptable1
--3) a table with multiple columns and multiple rows
select * into tmptable2 from(
select 'row1col1' as v1, 'row1col2' as v2
union all
select 'row2col1' as v2, 'row2col2' as v2
) tmp
One can create a view and later can query it whenever required
-- table with one column and multiple rows
create view vw1 as
(
select 'anyvalue' as col1
union all
select 'anyvalue' as col1
)
select * from vw1
-- table with multiple columns and multiple rows
create view vw2 as
(
select 'anyvalue1' as col1, 'anyvalue1' as col2
union all
select 'anyvalue2' as col1, 'anyvalue2' as col2
)
select * from vw2

Select limited rows from multiple tables

So this is fairly common knowledge to select rows from multiple tables and stack the results on top of each other:
SELECT * FROM table1
UNION
SELECT * FROM table2
UNION
...
However, if I want only a limited number of rows from each table, then how should I write it?
SELECT * FROM table1 LIMIT 2
UNION
SELECT * FROM table2 LIMIT 2
UNION
...
Clearly doesn't work.
Note that in my case, I have 51 tables, all with the same exact columns.
could be work this way
( SELECT * FROM table1 LIMIT 2 )
UNION
( SELECT * FROM table2 LIMIT 2 )
UNION
...

Oracle Query fetching table name alongwith column name

select trx_id,refernce number from
(select * from abcd_1_txt union
select * from abcd_2_txt union
select * from abcd_3_txt union
select * from abcd_4_txt)
where trx_id in (123,321,1234)
In the query all the tables are of same format, same column names and same number of columns.
After running this query, surely i will get some data.
My question --- is there any way to know from which of these tables, i am getting the output.
Try to add a column with number of query as below
select qrynum, trx_id,refernce number from
(select 1 as qrynum,* from abcd_1_txt union
select 2,* from abcd_2_txt union
select 3,* from abcd_3_txt union
select 4,* from abcd_4_txt)
where trx_id in (123,321,1234)
as Joe W said in the comment below you can also use name of the table instead of query number, short example:
select tabname, trx_id,refernce number from
(select 'abcd_1_txt' as tabname,* from abcd_1_txt union
...
where trx_id in (123,321,1234)
but both ways don't eliminate duplicates, so you can use union all instead of union. Other way to do that is to run quires separately with the condition
select * from abcd_1_txt where trx_id in (123,321,1234)
select * from abcd_2_txt where trx_id in (123,321,1234)
.
.
.

UNION ALL query: "Too Many Fields Defined"

I'm trying to get a UNION of 3 tables, each of which have 97 fields. I've tried the following:
select * from table1
union all
select * from table2
union all
select * from table3
This gives me an error message:
Too many fields defined.
I also tried explicitly selecting all the field names from the first table (ellipses added for brevity):
select [field1],[field2]...[field97] from table1
union all
select * from table2
union all
select * from table3
It works fine when I only UNION two tables like this:
select * from table1
union all
select * from table2
I shouldn't end up with more than 97 fields as a result of this query; the two-table UNION only has 97. So why am I getting Too many fields with 3 tables?
EDIT: As RichardTheKiwi notes below, Access is summing up the field count of each SELECT query in the UNION chain, which means that my 3 tables exceed the 255 field maximum. So instead, I need to write the query like this:
select * from table1
union all
select * from
(select * from table2
union all
select * from table3)
which works fine.
It appears that the number of fields being tracked (limit 255) is counted against ALL parts of the UNION ALL. So 3 x 97 = 291, which is in excess. You could probably create a query as a UNION all of 2 parts, then another query with that and the 3rd part.
I had two tables with 173 fields each (2 x 173 > 255!). So I had to resort to splitting the tables in half (keeping the primary key in both), before using the UNION statement and reassembling the resulting output tables using a JOIN.
select u1.*, u2.*
from (
select [field1_PKID],[field2],...,[field110]
from table1
union all
select [field1_PKID],[field2],...,[field110]
from table2
) as u1
inner join (
select [field1_PKID],[field111],...,[field173]
from table1
union all
select [field1_PKID],[field111],...,[field173]
from table2
) as u2
on [u1].[field1_PKID] = [u2].[field2_PKID]
Perhaps if your 3 tables have duplicate records you can go with UNION instead of UNION ALL which may reduce the number of fields to be tracked. Because UNION will always serve the business purpose which removes duplicates. In that case your query will be like following,
select * from table1
union
select * from table2
union
select * from table3;