How to use LIKE statement with multiple values from another field? - sql

I want to select all records where field contains values from another field. How do I do that? Here is a code that I am trying.
select field1 , field2, field3
from table1
where field1 like '%'+(select distinct field4 from table2)+'%'
Thanks in advance.

Just do your like as a join condition:
select field1 , field2, field3
from table1
join (select distinct field4 from table2) x
on field1 like '%'+field4+'%'

Using the original structure of your query, you can do:
select field1, field2, field3
from table1 t1
where exists (select 1
from table2
where t1.field1 like '%' + field4 + '%'
);
The advantage of this method is that it will not duplicate records in table1. For instance, if there are two rows in table2 with the values 'a' and 'b' respectively and one row in table1 with the value 'ab', then this method will only return the row from table1 once.

Related

Multiple select in and do a Delete after

I would like to know how to convert a select query with multiple select in ( ) into a delete query. Field1 is a primary key in all the tables.
Query:
select field1 from table1 where field1 in
(select field1 from table2 where field19<>777) and field1 in
(select field1 from table2 where field19=777) and field1 in
(select field1 from table3 where field8=0 or field8 is null)
Never mind, it’s to late ;) it will return all the field1 so I can have the total numbers of field1 but we don’t want to delete field1, so it’s not possible. This will need to be done with a Join.
Thanks we can close this post.

insert Default value For a column while inserting data from other table

I am facing an issue when I am inserting data into table1 by selecting data from table2
like
INSERT INTO table1
SELECT * FROM table2
So there is a column in both table like field1 for which value is null in table2.
But in table I have set the default value for field1
after executing ,
INSERT INTO table1
SELECT * FROM table2
I am having null value for field1 insted of getting default value of it what i set.
You Can Specify the column names and then set the value in your Select Statement
INSERT INTO table1 (field1, field2, field3, ...)
SELECT field1,
CASE WHEN field2 IS NOT NULL THEN field2
ELSE 'Your_default_value' END ,
field3, ...
FROM table2
INSERT INTO table1 (field2, field3, ...)
SELECT field2, field3, ...
FROM table2
You just skip the column for which default value oyu want to use and it will be automatically populated with default.
Interesting, One way to achieve this is use of CASE EXPRESSION, Assuming that field2 is column which you wants to put default value
INSERT INTO TableName1 (field1, field2, field3, ...)
SELECT field1, CASE WHEN field2 IS NULL THEN 'DefaultValue' ELSE field2 END, field3, ...
FROM TableName2
Try using NVL
INSERT INTO TableName1 (field1, field2, field3, ...)
SELECT field1, NVL(field2,'Your Default Value'), field3, ...
FROM TableName2
For example:
INSERT INTO DESTINATION_TABLE(A, B, C, D, E, F)
SELECT W, X, Y, 'Some custom value', CASE WHEN Z IS NULL THEN 'Some other value' ELSE Z
FROM SOURCE_TABLE
WHERE ....
Any time you want to replace a null value, use COALESCE. COALESCE is available in both Oracle and SQL Server, it doesn't evaluate arguments unless needed (NVL always evaluates the second argument), and it is simpler than a case statement:
Insert into table1(field1, field2)
select field1, coalesce(field2, field3, field4, 'novalue') from table2;
One big difference between NVL and COALESCE, if the second argument has side effects, you could come up with different results.

How to create a SELECT query FROM "TABLE1 AND TABLE2"

I have a PostgreSQL database, with only SELECT permissions. In this DB there are two tables with the same structure (the same columns).
I need to write several query in each table and join the results.
There is a way for writing a query like this one?
SELECT
field1,
field2,
field3
FROM
table1
AND
table2
WHERE
condition;
Select from 2 tables. Query = table1 OR table1 + table2 have no answer and it is not my question.
UNION ALL
SELECT field1, field2, field3
FROM table1
WHERE condition
UNION ALL
SELECT field1, field2, field3
FROM table2
WHERE condition;
Or to simplify your WHERE condition
SELECT * FROM
( SELECT field1, field2, field3
FROM table1
UNION ALL
SELECT field1, field2, field3
FROM table2
)
WHERE condition;
You can use Union:
SELECT
field1,
field2,
field3
FROM
table1
UNION
SELECT
field1,
field2,
field3
FROM
table2
SELECT * FROM
( SELECT field1, field2, field3
FROM table1
UNION ALL
SELECT field1, field2, field3
FROM table2
)
WHERE condition;

ORACLE Select and group by excluding one field

I have a very simple query (on Oracle 11g) to select 3 fields:
select field1, field2, field3, count(*) from table
where...
group by field1, field2, field3
having count(*) > 10;
Now, what I need, is exclude "field3" from the "group by" since I only need field 1 and 2 to be grouped, but I also need field3 in the output.
As far I know, all the fields in the select must be reported also in "group by", so how can I handle that?
Thanks
Lucas
select t.field1, t.field2, t.field3, tc.Count
from table t
inner join (
select field1, field2, count(*) as Count
from table
where...
group by field1, field2
having count(*) > 10
) tc on t.field1 = tc.field1 and t.field2 = tc.field2
Use the analytical version of the "count" function:
select * from (
select field1, field2, field3, count(*) over(partition by field1, field2) mycounter
from table )
--simulate the having clause
where mycounter > 10;
If you don't group by field3 anymore, there can suddenly be different field3 per group. You must decide which one to show, e.g. the maximum:
select field1, field2, max(field3), count(*) from table
where...
group by field1, field2
having count(*) > 10;
The only way I know how to handle that is to first isolate the Field1 and Field2 data and create a new table, then link it back to the original table adding in Field3.
Select Table2.Field1, Table2.Field2, Table1.Field3
From
(Select Field1, max(Field2) as Field2
From Table1) Table2
Where Table2.Field1 = Table1.Field1
And Table2.Field2 = Table1.Field2
Group By
Table2.Field1, Table2.Field2, Table1.Field3

Refer to other SQL SELECT statements aliased as tables in FROM clause

I have a very large query that follows the format below:
select ...
from ( select field1,
field2
from some_table ) table1,
( select field1,
field3
from other_table ) table2
where .....
Is is possible for me to refer to one of the tables "defined" in the from clause, lets say table1, in one of the other table definitions in the from clause?
For example:
select ....
from ( select field1,
field2
from some_table ) table1,
( select table1.field1,
field3
from other_table,
table1 ) table2
where .....
Disclaimer: What I am trying to do is not as simple as the example above. The example is simply to illustrate the idea.
WITH
table1 AS
(
SELECT field1, field2
FROM some_table
),
table2 AS
(
SELECT field1, field2
FROM other_table, table1
)
SELECT *
FROM table2
If you are using SQL 2005, you can use Common Table Expressions for doing what you are trying; Quassnoi gives us an example, in Oracle I don't know how to achieve it though