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');
Related
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"
I am writing a HIVE query to pull about 2,000 unique keys from a table.
I keep getting this error - java.lang.StackOverflowError
My query is basic but looks like this:
SELECT * FROM table WHERE (Id = 1 or Id = 2 or Id = 3 Id = 4)
my WHERE clause goes all the way up to 2000 unique id's and I receive the error above. Does anyone know of a more efficient way to do this or get this query to work?
Thanks!
You may use the SPLIT and EXPLODE to convert the comma separated string to rows and then use IN or EXISTS.
using IN
SELECT * FROM yourtable t WHERE
t.ID IN
(
SELECT
explode(split('1,2,3,4,5,6,1998,1999,2000',',')) as id
) ;
Using EXISTS
SELECT * FROM yourtable t WHERE
EXISTS
(
SELECT 1 FROM (
SELECT
explode(split('1,2,3,4,5,6,1998,1999,2000',',')) as id
) s
WHERE s.id = t.id
);
Make use of the Between clause instead of specifying all unique ids:
SELECT ID FROM table WHERE ID BETWEEN 1 AND 2000 GROUP BY ID;
i you can create a table for these IDs and after use the condition of exist in the new table to get only your specific IDs
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