I want to insert in a table multiple counts I mean count all from cars and trucks and insert the result in a row in a table.
insert into table result(A,B)
select r1,r2 from(
select count(*) from trucks where fecha='X' and name like '%X%' and name not like '%X%' as r1,
select count(*) from cars where fecha='X' and name like '%X%' and name not like '%X%' as r2
)
;
I tried that but dont work... I do not know why...
I just discover I am using a hive enviroment and subqueries are not supported
Try enclosing your two subqueries with parentheses like this:
INSERT INTO table result(A, B)
VALUES (
(
SELECT COUNT(*)
FROM trucks
WHERE fecha='X' AND name LIKE '%X%' AND name NOT LIKE '%X%'
),
(
SELECT COUNT(*)
FROM cars
WHERE fecha='X' AND name LIKE '%X%' AND name NOT LIKE '%X%'
)
);
Try this like that :-
INSERT INTO destination_table (
Field_1,
Field_2
)
SELECT Field_1,
Field_2
FROM source_table;
Related
I am trying to do a wildcard search based on the result set of a subquery in Redshift. For example, Table A has first names and Table B has names which could be Last Name, First Name or First Name, Last Name. I want to return rows from Table B based on matches to a subset of Table A. I found the Similar To operator, but that only seems to work when I can hard-code the terms I am searching for. Is there a way I can achieve something like
SELECT col1 FROM Table_A WHERE col1 SIMILAR TO '%(SELECT distinct col2 FROM Table_B)%'
in order to achieve
SELECT col1 FROM Table_A WHERE col1 LIKE '%something%' OR col1 LIKE '%something else%'
This is what I ended up implementing after a recommendation from #GMB
CREATE TABLE test2 AS (
SELECT 'a' as val
UNION ALL
SELECT 'b' as val
UNION ALL
SELECT 'c' as val
);
CREATE TABLE test3 as (
SELECT 'apple' as name
UNION ALL
SELECT 'pear' as name
UNION ALL
SELECT 'plum' as name
);
SELECT * FROM test3
WHERE EXISTS (
SELECT 1
FROM test2 WHERE test3.name LIKE ('%'||val||'%')
)
You could use exists:
select col1
from table_a a
where exists (
select 1
from table_b b
where a.col1 similar to concat('%', b.col2, '%')
)
I have a select statement that works in oracle. This is an imitation of how it looks like (the real statement is 200+ lines):
select * from (
select * from (
select id, name from my_table where name like 'D%' or name like 'Z%' order by name, id)
union all
select * from (
select id, name from my_table where name like 'K%' or name like 'T%' order by name, id)
union all
select * from (
select id, name from my_table where name like 'B%' or name like 'M%' order by name, id)
);
So, basially it's a union of 3 types of records from the same table. The outer "select * from" is automatically added and cannot be removed/changed. The ordering is important - records from the first subquery must be first, then records from the second one etc.
I need to rewrite it for sql server (preferably one common statement that works in oracle and sql server as well, but it's optional)
What I tried was:
select * from (
select * from (
select id, name, 1 as order_column from my_table where name like 'D%' or name like 'Z%') subquery
union all
select * from (
select id, name, 2 as order_column from my_table where name like 'K%' or name like 'T%') subquery
union all
select * from (
select id, name, 3 as order_column from my_table where name like 'B%' or name like 'M%') subquery
) outerquery order by order_column, name, id;
The ordering is preserved, but the order_column is included in result records and that is wrong.
So I changed it into:
select * from (
select id, name from (
select id, name, 1 as order_column from my_table where name like 'D%' or name like 'Z%') subquery
union all
select id, name from (
select id, name, 2 as order_column from my_table where name like 'K%' or name like 'T%') subquery
union all
select id, name from (
select id, name, 3 as order_column from my_table where name like 'B%' or name like 'M%') subquery
) parentquery order by order_column, name, id
But now, obviously the order_column is not visible in the outer query and it doesn't work. As I wrote before the "select * from" in first line cannot be changed - only the outer ORDER BY clause. Also the example conditions "name like 'D%'" are much more complex in reality and I cannot use them in some kind of outer order by ... case when ... (they must stay in subqueries)
I would be grateful for help.
Neither database guarantees the ordering of the result set without an order by.
Why not just do this in either database?
select id, name, 1 as order_column
from my_table
where name like 'D%' or name like 'Z%' or
name like 'K%' or name like 'T%' or
name like 'B%' or name like 'M%'
order by (case when name like 'D%' or name like 'Z%' then 1
when name like 'K%' or name like 'T%' then 2
when name like 'B%' or name like 'M%' then 3
end)
In SQL Server, you can simplify this to:
select id, name, 1 as order_column
from my_table
where name like '[DZKTBM]%'
order by (case when name like '[DZ]%' then 1
when name like '[KT]%' then 2
when name like '[BM]%' then 3
end)
I have a table like this:
CREATE TABLE mytable
(
col1 character varying(50),
mydate timestamp without time zone
);
I want to insert data to this table, but also I want to store the maximum id from my source:
insert into mytable (select myid, col1, mydate from sourcetable);
I don't have a myid column in mytable, and I can't ask later something like this: select max(myid) from sourcetable because I'm getting a snapshot and the sourcetable is a transactional table (hundreds of new records by second) so I need to get the maximum id from that snapshot
I tried something like this:
with query1 as (select myid, col1, mydate from sourcetable),
query2 as (select max(myid) id from query1)
insert into mytable (select co1, mydate from query1);
update anothertable set value=(select myid from query2) where col2='avalue';
But I get this error:
ERROR: relation "query2" does not exist
LINE 1: update anothertable set value=(select myid from query2) wher...
Is there a way to solve this?
The problem is that you have two queries after the CTEs. Only one. The CTE is connected to the queries. So, just add another CTE. Something like this:
with query1 as (
select myid, col1, mydate
from sourcetable
),
query2 as (
select max(myid) as id
from query1
),
i as (
insert into mytable -- You should really list the columns here
select co1, mydate
from query1
)
update anothertable
set value = (select myid from query2)
where col2 = 'avalue';
It is often convenient in PosgreSQL to create "tables" on the fly so to refer to them, e.g.
with
selected_ids as (
select 1 as id
)
select *
from someTable
where id = (select id from selected_ids)
Is it impossible to provide multiple values as id this way? I found this answer that suggests using values for similar problem, but I have problem with translating it to the example below.
I would like to write subqueries such as
select 1 as id
union
select 2 as id
union
select 7 as id
or
select 1 as id, 'dog' as animal
union
select 7 as id, 'cat' as animal
in more condensed way, without repeating myself.
You can use arguments in the query alias:
with selected_ids(id) as (
values (1), (3), (5)
)
select *
from someTable
where id = any (select id from selected_ids)
You can also use join instead of a subquery, example:
create table some_table (id int, str text);
insert into some_table values
(1, 'alfa'),
(2, 'beta'),
(3, 'gamma');
with selected_ids(id) as (
values (1), (2)
)
select *
from some_table
join selected_ids
using(id);
id | str
----+------
1 | alfa
2 | beta
(2 rows)
You can pass id and animal field in WITH like this
with selected_ids(id,animal) as (
values (1,'dog'), (2,'cat'), (3,'elephant'),(4,'rat')--,..,.. etc
)
select *
from someTable
where id = any (select id from selected_ids)
You should use union and IN statement like this:
with
selected_ids as (
select 1 as id
union
select 2 as id
union
select 3 as id
....
)
select *
from someTable
where id in (select id from selected_ids)
after reviewing wingedpanther's idea and looking for it, you can use his idea IF those id's are continuously like this:
with
selected_ids as (
SELECT * FROM generate_series(Start,End) --(1,10) for example
)
select *
from someTable
where id in (select id from selected_ids)
If they are not continuously , the only way you can do that is by storing those ID's in a different table(maybe you have it already and if not insert it)
And then:
select *
from someTable
where id in (select id from OtherTable)
How to use ORDER BY CHARINDEX() with UNION?
What I want is like:
select Id,Name from A where Name like '%Raspberry%'
Union
select Id,Name from B where Name like '%Raspberry%'
order by CHARINDEX('Raspberry',Name)
Common Table Expression will help you:
with cte as (
select Id,Name, CHARINDEX('Raspberry', Name) ci from #t1 where Name like '%Raspberry%'
Union
select Id,Name, CHARINDEX('Raspberry', Name) ci from #t2 where Name like '%Raspberry%'
)
select Id, Name
from cte
order by ci
SQL FIDDLE
AS you mentioned the problem is you have to select the column present in order by if you are using UNION. Try this.
select Id,Name,CHARINDEX('Raspberry',Name) as order_col
from A
where Name like '%Raspberry%'
Union
select Id,Name,CHARINDEX('Raspberry',Name) as order_col
from B
where Name like '%Raspberry%'
order by order_col