SQL Multiple NOT LIKE From DB data - sql

Having the following 2 tables:
tableA
id
Name
tableB
id
filter
filter is something in the following format: aaa;bbb;ccc;...
and what I need to do is:
Select Name
FROM tableA A INNER JOIN
tableB B
ON A.id=B.id
WHERE A.Name NOT LIKE aaa AND A.Name NOT LIKE bbb AND A.Name NOT LIKE ccc ...
How can I get dynamically the query for doing the NOT LIKE ANDs from the respectively filter row in tableB?

How can I get dynamically the query for doing the NOT LIKE ANDs from the respectively filter row in tableB?
You can just use the filter with NOT LIKE:
Select Name
FROM tableA A INNER JOIN
tableB B
ON A.id = B.id
WHERE A.Name NOT LIKE b.filter;
If you want names where no filter matches, then you can use:
select a.name
from tableA a
where not exists (select 1
from tableB b
where A.Name like b.filter
);

Select Name
FROM tableA A INNER JOIN
tableB B
ON A.id=B.id
WHERE A.Name LIKE '%^[aaa,bbb,ccc,...]%'

Your table design isn't very good. Ideally you would store each filter in TableB in a separate row.
Be that as it may, you can split the filters with STRING_SPLIT and then filter on that:
SELECT Name
FROM tableA A INNER JOIN
tableB B
ON A.id=B.id
WHERE NOT EXISTS (SELECT 1
FROM STRING_SPLIT(B.filter, ';') s
WHERE A.Name LIKE s.value
)

Related

SQL how to insert and left join

name table a
1.budy
2.rendy
3.rona
4.sandi
5.susi
table b
1.budy
2.rendy
3.rona
how I can show on table b sandi and susi from table a with sql?
To test: http://rextester.com/HDZ43705
This will do what you're looking for:
select * from A
except
select * from B
Try It:-
INSERT INTO tableb
SELECT tablea.id,tablea.name,tableb.id,tableb.name
FROM tablea
LEFT JOIN tableb
ON tablea.name = tableb.name;
You can check the existence of data in TableA using LEFT JOIN or NOT EXISTS which is not exist in TableB and then insert in TableB
INSERT INTO tableb(name)
SELECT A.name
FROM tableA A
LEFT JOIN tableB B ON B.name = A.name
WHERE B.name IS NULL
OR
SELECT A.name
FROM tableA A
WHERE NOT EXISTS(SELECT 1 FROM tableB B WHERE B.name = A.name)
Use this
INSERT INTO tableb
SELECT tablea.id,tablea.name FROM dbo.tablea
LEFT JOIN dbo.tableb
ON tablea.id=tableb.id
WHERE tableb.id is null
Might not be the most optimized solution, but guess it will work:
SELECT name
FROM TableB
UNION
SELECT name
FROM TableA
Will return a DISTINCT version of the data in both tables, if that was what you were looking for (a joined view of the tables).
If you only want to show the names in A that are not in B, then something like this:
SELECT name FROM TableA WHERE name NOT IN (SELECT name FROM TableB)
I think this is essentially the same soution that DEEPAK suggested, only his was more compact and modern.

How do i join two tables in such a way that all the rows are present in the output

I have two table say TableA and TableB with below structure
TableA
cust_name
cust_id
cust_age
TableB
id
cust_id
balance
I need to join these two tables all retrieve the below columns
tableA.cust_name,tableA.cust_age,tableB.balance
but if I use the below query
select a.cust_name,a.cust_age,sum(b.balance) from tableA a,tableB b
where a.cust_id=b.cust_id and b.id = (select max(id) from tableB where cust_id=b.cust_id)
I only get those rows which are present in both the tables, but i need all the rows where there is a customer in tableA and not in tableB the b.balance should come either null or 0.
I would say Left join
SELECT a.cust_name, a.cust_age, sum(nvl(b.balance,0))
FROM tableA a
LEFT JOIN tableB b
ON a.cust_id=b.cust_id
GROUP BY a.cust_name, a.cust_age
Also use NVL as you may get some nulls in b.balance.
Alternatively if you are on an old Oracle version you have to use a different join:
SELECT a.cust_name, a.cust_age, sum(nvl(b.balance,0))
FROM tableA a, tableB b
where a.cust_id=b.cust_id (+)
GROUP BY a.cust_name, a.cust_age

SQL Query Duplicating records

I've got two tables.
Let's call them table_A and table_B.
Table_B contains the ForeignKey of table_A.
Table_A
ID Name
1 A
2 B
3 C
Table_B
ID table_a_fk
1 2
2 3
Now I want to get all the names out of table_a IF table_b does not contain the ID of the record in table_a.
I've tried it with this query:
SELECT a.name
FROM table_a a, table_b b
WHERE a.id != b.table_a_fk
With this Query I'm getting the right result I just get this result like 5times and I don't know why.
Hope someone can explain me that.
Your query creates a cartesian product between your two tables A and B. It is the cartesian product that generates those duplicate values. Instead, you want to use an anti-join, which is most commonly written in SQL using NOT EXISTS
SELECT a.name
FROM table_a a
WHERE NOT EXISTS (
SELECT *
FROM table_b b
WHERE a.id = b.table_a_fk
)
Another way to express an anti-join with NOT IN (only if table_b.table_a_fk is NOT NULL):
SELECT a.name
FROM table_a a
WHERE a.id NOT IN (
SELECT b.table_a_fk
FROM table_b b
)
Another, less common way to express an anti-join:
SELECT a.name
FROM table_a a
LEFT OUTER JOIN table_b b ON a.id = b.table_a_fk
WHERE b.id IS NULL
use distinct
SELECT distinct a.name
FROM table_a a, table_b b
WHERE a.id != b.table_a_fk
or better is...
Select distinct name
from tableA a
Where not exists (Select * from tableB
Where table_a_fk = a.id)

How to filter table with conditions stored as parameters in another table?

I want to use parameters stored in tableA to filter tableB.
Here is my tableA with parameters:
I want to filter tableB with more or less such a query:
WITH A AS
(SELECT
[FilterType]
,[MaxID]
FROM TableA
WHERE [FilterType]=1
)
SELECT * FROM TableB B
WHERE B.ID>A.MaxID
I want to get all the records from TableB where B.ID is larger than MaxID chosen from TableA for a FilterType 1. How to do it? Speaking more generally, how to get a parameter from table and use this parameter for query?
Not a lot of detail here but something like this?
select b.Columns
from TableB b
join TableA a on a.MaxID <= b.ID
where a.FilterType = 1
SELECT B.*
FROM TableB AS B
JOIN TableA AS A ON B.ID > A.MaxID
WHERE A.FilterType = 1
WITH A AS
(SELECT
[FilterType]
,[MaxID]
FROM TableA
WHERE [FilterType]=1
)
SELECT B.* FROM TableB B,A
WHERE B.ID>A.MaxID

How do I write SELECT FROM myTable WHERE id IN (SELECT...) in Linq?

How do you rewrite this in Linq?
SELECT Id, Name FROM TableA WHERE TableA.Id IN (SELECT xx from TableB INNER JOIN Table C....)
So in plain english, I want to select Id and Name from TableA where TableA's Id is in a result set from a second query.
from a in TableA
where (from b in TableB
join c in TableC on b.id equals c.id
where .. select b.id)
.Contains(a.Id)
select new { a.Id, a.Name }
LINQ supports IN in the form of contains. Think "collection.Contains(id)" instead of "id IN (collection)".
from a in TableA
where (
from b in TableB
join c in TableC
on b.id equals c.id
select b.id
).Contains(TableA.Id)
select new { a.Id, a.Name }
See also this blog post.
There is no out of box support for IN in LINQ. You need to join 2 queries.