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

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

Related

SQL Multiple NOT LIKE From DB data

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
)

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.

Join of Two Tables where Data Matches in One Column

For some reason I have a hard time grasping joins and this one should be very simple with the knowledge that I have in SQL.
Anyway, I have 2 tables. We will call them TableA and TableB. One of the columns in TableA is "ID". TableB only consists of the column "ID". I want to return all rows in TableA whose ID is present in TableB.
I know this should be very simple to figure out, but my brain doesn't want to work today.
You can do this using an EXISTS:
Select A.*
From TableA A
Where Exists
(
Select *
From TableB B
Where A.Id = B.Id
)
You can also use a JOIN if you wish, but depending on your data, you may want to couple that with a SELECT DISTINCT:
Select Distinct A.*
From TableA A
Join TableB B On A.Id = B.Id
One thing to keep in mind is that the ID of TableA is not necessarily related to the ID of TableB.
this should work
SELECT B.ID
FROM TableA A
JOIN TableB B
ON (A.ID=B.ID)
WHERE A.ID=B.ID
You can also use IN operator like this:
Select *
From TableA
Where ID in
(
Select distinct ID
From TableB
)

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)

select a value where it doesn't exist in another table

I have two tables
Table A:
ID
1
2
3
4
Table B:
ID
1
2
3
I have two requests:
I want to select all rows in table A that table B doesn't have, which in this case is row 4.
I want to delete all rows that table B doesn't have.
I am using SQL Server 2000.
You could use NOT IN:
SELECT A.* FROM A WHERE ID NOT IN(SELECT ID FROM B)
However, meanwhile i prefer NOT EXISTS:
SELECT A.* FROM A WHERE NOT EXISTS(SELECT 1 FROM B WHERE B.ID=A.ID)
There are other options as well, this article explains all advantages and disadvantages very well:
Should I use NOT IN, OUTER APPLY, LEFT OUTER JOIN, EXCEPT, or NOT EXISTS?
For your first question there are at least three common methods to choose from:
NOT EXISTS
NOT IN
LEFT JOIN
The SQL looks like this:
SELECT * FROM TableA WHERE NOT EXISTS (
SELECT NULL
FROM TableB
WHERE TableB.ID = TableA.ID
)
SELECT * FROM TableA WHERE ID NOT IN (
SELECT ID FROM TableB
)
SELECT TableA.* FROM TableA
LEFT JOIN TableB
ON TableA.ID = TableB.ID
WHERE TableB.ID IS NULL
Depending on which database you are using, the performance of each can vary. For SQL Server (not nullable columns):
NOT EXISTS and NOT IN predicates are the best way to search for missing values, as long as both columns in question are NOT NULL.
select ID from A where ID not in (select ID from B);
or
select ID from A except select ID from B;
Your second question:
delete from A where ID not in (select ID from B);
SELECT ID
FROM A
WHERE NOT EXISTS( SELECT 1
FROM B
WHERE B.ID = A.ID
)
This would select 4 in your case
SELECT ID FROM TableA WHERE ID NOT IN (SELECT ID FROM TableB)
This would delete them
DELETE FROM TableA WHERE ID NOT IN (SELECT ID FROM TableB)
SELECT ID
FROM A
WHERE ID NOT IN (
SELECT ID
FROM B);
SELECT ID
FROM A a
WHERE NOT EXISTS (
SELECT 1
FROM B b
WHERE b.ID = a.ID)
SELECT a.ID
FROM A a
LEFT OUTER JOIN B b
ON a.ID = b.ID
WHERE b.ID IS NULL
DELETE
FROM A
WHERE ID NOT IN (
SELECT ID
FROM B)