Join on all fields without listing them? - sql

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

Related

How to modify data of select query before inserting from one table to another table

I have 2 tables Temp and Final. I want to insert data from temp table to final table. There is already a complex query written for that.
INSERT INTO public.Final
(
select id, class, type, meta_id, time, zone, geom
from public.Temp where ....
)
Now I want to add further criteria on geometry where in I want to merge polygons and then remove overlapping geometries. I have 2 separate queries written for those tasks. I can't combine into one select as it is already complex.
These queries I want to apply before inserting data into final table. Is it possible that output of one select query goes to input of another select query?
INSERT INTO public.Final
(
/*step 3 final output */
select non overlapping geometries where ...
(/*step 2*/
select merged geometries
where ...
(/*step 1*/select valid geometries where ...)
)
)
If you could give me any example on how to do it, that would be great! thanks.
yes, there are multiple ways , but needs more detail to know better which way is suitable for your case :
using JOIN:
SELECT *
FROM (SELECT * FROM COMPLEXQUERY1) AS Q1
JOIN (SELECT * FROM COMPLEXQUERY2) AS Q2
ON Q1.ID = Q2.ID
JOIN (SELECT * FROM COMPLEXQUERY3) AS Q3
ON Q2.ID = Q3.ID
using IN and/or EXISTS IN:
SELECT *
FROM (SELECT * FROM COMPLEXQUERY1) AS Q1
WHERE EXISTS
(
SELECT * FROM COMPLEXQUERY2 Q2
WHERE Q1.ID = Q2.ID
AND Q2.ID2 IN
(
SELECT id from COMPLEXQUERY3
)
)

thousands of values to be searched

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');

SELECT VALUES in Teradata

I know that it's possible in other SQL flavors (T-SQL) to "select" provided data without a table. Like:
SELECT *
FROM (VALUES (1,2), (3,4)) tbl
How can I do this using Teradata?
Teradata has strange syntax for this:
select t.*
from (select * from (select 1 as a, 2 as b) x
union all
select * from (select 3 as a, 4 as b) x
) t;
I don't have access to a TD system to test, but you might be able to remove one of the nested SELECTs from the answer above:
select x.*
from (
select 1 as a, 2 as b
union all
select 3 as a, 4 as b
) x
If you need to generate some random rows, you can always do a SELECT from a system table, like sys_calendar.calendar:
SELECT 1, 2
FROM sys_calendar.calendar
SAMPLE 10;
Updated example:
SELECT TOP 1000 -- Limit to 1000 rows (you can use SAMPLE too)
ROW_NUMBER() OVER() MyNum, -- Sequential numbering
MyNum MOD 7, -- Modulo operator
RANDOM(1,1000), -- Random number between 1,1000
HASHROW(MyNum) -- Rowhash value of given column(s)
FROM sys_calendar.calendar; -- Use as table to source rows
A couple notes:
make sure you pick a system table that will always be present and have rows
if you need more rows than are available in the source table, do a UNION to get more rows
you can always easily create a one-column table and populate it to whatever number of rows you want by INSERT/SELECT into it:
CREATE DummyTable (c1 INT); -- Create table
INSERT INTO DummyTable(1); -- Seed table
INSERT INTO DummyTable SELECT * FROM DummyTable; -- Run this to duplicate rows as many times are you want
Then use this table to create whatever resultset you want, similar to the query above with sys_calendar.calendar.
I don't have a TD system to test so you might get syntax errors...but that should give you a basic idea.
I am a bit late to this thread, but recently got the same error.
I solved this by simply using
select distinct 1 as a, 2 as b from DBC.tables
union all
select distinct 3 as a, 4 as b from DBC.tables
Here, DBC.tables is a DB backend table with a few rows only. So, the query runs fast as well

SELECT * FROM (SELECT)

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.

when loading data from multiple tables into a single table in hive?

I have a hive empty table of a particular structure.
I have 10 other tables of the same structure and datatype and schema but different table names.
I loaded data of one table into the empty table using "insert into" and say i have 10 mil records.
Now I am loading the second table into this table using "insert into".
When I do a count(*), it is not showing me the entire count of records.
It is displaying only the record count of the last loaded table.
Why is that? I want all the records to be loaded.
Any help would be great!
do this,
insert into table table_name
Select * from
(
SELECT b.var1 FROM tmp_table1 b
UNION ALL
SELECT c.var1 FROM tmp_table2 c
UNION ALL
SELECT d.var1 FROM tmp_table3 d
UNION ALL
SELECT e.var1 FROM tmp_table4 e
UNION ALL
SELECT f.var1 FROM tmp_table5 f
UNION ALL
SELECT g.var1 FROM tmp_table6 g
UNION ALL
SELECT h.var1 FROM tmp_table7 h
) CombinedTable
You are having same schema for all the table so it's better to copy the files to new empty table. this is better a solution if you don't have any partitions in hive table.