Let's say I have a table called stuff with columns id and name. Let's also say that I have a subquery that selects from a different table (let's call it other_stuff) the columns id and name.
At this point in my example, the result of select * from stuff and select * from (<my subquery>) will contain the same columns (but different rows).
Is there a way I can somehow "concat" both results, so I can do something like...
select *
from stuff
concat
(<my subqueryy>)
where name == "foobar"
...so that the where condition will match all foobar values?
What you need is UNION ALL:
select t.* from (
select * from stuff
union all
select * from <my subquery>
) as t
where t.name = "foobar"
Related
simple search query
select * from table where id = id_1;
id_1,......................................id_2000
is there a way to search all these ids with one query or pl/sql code?
You could try:
SELECT * FROM table WHERE id IN (id_1, id_2, ... id_2000);
or if the IDs are strings:
SELECT * FROM table WHERE id IN ('id_'1, 'id_2', ... 'id_2000');
Say I have a table with a column called "Names" and with values "Mike", "John", "Kelly", and "Tina". Every day the values might change.
How would I structure the query so that if the table has the name "Tina", it only displays "Tina", but if it doesn't contain "Tina", it'll display everything else?
Another option to consider (BigQuery Standard SQL)
#standardSQL
SELECT * EXCEPT(flag) FROM (
SELECT *, names = 'Tina' OR COUNTIF(names = 'Tina') OVER() = 0 AS flag
FROM `project.dataset.table`
)
WHERE flag
I would expected this version is significantly cheaper than another one with implicit join
One option is union all ad not exists:
select name from mytable where name = 'Tina'
union all
select t.name from mytable t where not exists (select 1 from mytable t1 where t1.name = 'Tina')
Trying to union two tables with the same field into one master table but for some reason im getting a weird result.
select count(*)
from staging.sandoval_parcels
where parcel_id = 0;
returns 0
select count(*)
from staging.bernalillo_parcels
where parcel_id = 0;
returns 0
but when i merge the tables using
CREATE TABLE staging.master_parcels
AS
SELECT * FROM bernalillo_parcels
UNION ALL
SELECT * FROM sandoval_parcels
;
then
select count(*)
from staging.master_parcels
where parcel_id = 0;
returns 85553
both tables have the same fields and the fields are the same data type,also, no of the values for any field are missing, thus no nulls, why am i getting ids = 0 when either of the table have parcel_ids = 0?
The order of the fields matter, replace the * for the explicit name, other wise the second query field will be inserted on the first query position. But not necessarily on the same field you want.
CREATE TABLE staging.master_parcels
AS
SELECT parcel_id, field1 ... FROM bernalillo_parcels
UNION ALL
SELECT parcel_id, field1 ... FROM sandoval_parcels
;
Union will merge tables even if the column order is not the same. If all of the columns match and are in the same order, it will union distinct values and not create duplicates if the rows are the same for each table. Having the order and data type be the same is important.
There is a table "MAIN_TABLE" with columns "Table_Unique_Code" and "Table_name".
Also there are several tables with required data.
The task is to create SQL-query with parameter ("Table_Unique_Code"), which will select all data from the table determined by the "Table_Unique_code".
Something like
SELECT * FROM (*determine the name of the table by Table_Unique_code here*);
I tried
SELECT * FROM (SELECT table_name FROM MAIN_TABLE WHERE Table_Unique_Code=?)
but it doesn't work.
I work with OracleDB.
Goal is to retrieve all exact records (each field is the same) from table_a that exist in table_b; however, there are many fields (lets say 100), which I don't want to type/list out.
Is there a way to compare tables based on records? Or have it auto-recognize and join-on fields when not specified?
SELECT * FROM table_a
WHERE EXISTS (
select * from table_b
-- where table_a.field1 = table_b.field1
-- and ...
-- and table_a.field100 = table_b.field100
);
try:
select * from A
intersect
select * from B
see: http://www.postgresql.org/docs/9.1/static/queries-union.html
modified as suggested by user2989408