SQL how to insert and left join - sql

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.

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
)

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

Sorting data from one table based on another table

Here are my tables:
create table tableA (id int, type varchar(5))
insert into tableA (ID,TYPE)
values
(101,'A'),
(101,'C'),
(101,'D'),
(102,'A'),
(102,'B'),
(103,'A'),
(103,'C')
create table tableB (id int, type varchar(5), isActive bit)
insert into tableB (id, type, isActive)
Values
(101,'A',1),
(101,'B',0),
(101,'C',0),
(101,'D',1),
(102,'A',1),
(102,'B',0),
(102,'C',1),
(102,'D',1),
(103,'A',1),
(103,'B',1),
(103,'C',1),
(103,'D',1)
Now, I want to do two things here:
1) Find the rows that are present in tableA but isActive = 0 in tableB. (done)
select A.* from tableA A
join tableB B
on A.id = B.id and A.type = B.type
where B.isactive = 0
2) Find the rows that are missing in tableA but isActive = 1 in tableB.
For example ID 103, type B is active in tableB but is missing in tableA.
I don't care about existance of type D in tableA because I am only checking the last entry in tableA which is C.
Also, ABCD is in order for all IDs, they can be active or inactive.
Thanks for your help!
My effort: (not working)
select A.* from tableA A
where exists (
select B.* from tableA A
join tableB B
on a.id = b.id
where b.isActive = 1
order by b.id,b.type
)
SQLFiddle
I think you are looking for something like the following:
select B.* from tableB B
left join tableA A
on B.id = A.id and B.type = A.type
where B.isActive = 1
and A.id is null
order by B.id, B.type
By using the left join, it means that rows in tableB that have no rows to join with in tableA will have all A.* columns null. This then allows you to add the where clause to check where the tableA records are null thus determining what is contained within tableB that is active and not in tableA

Sql Query for inserting data from 2 tables into one based on name

Hello I currently 2 tables of data from different sources,
I need to combine all of them into one main table.
Database layout:
Table A
-Name
-Ranking
-Score
Table B
-Name
-Ranking
-Score
Table New
-Name
-Ranking A
-Score A
-Ranking B
-Score B
I want to take the data from table A and B and insert it into table New based on the name.
Not sure how to do this in sql, any help appreciated
Assuming every record in TableA has a corresponding record in TableB:
insert into TableNew
(Name, RankingA, ScoreA, RankingB, ScoreB)
select a.Name, a.Ranking, a.Score, b.Ranking, b.Score
from TableA a
inner join TableB b
on a.Name = b.Name
If that assumption is invalid, then:
insert into TableNew
(Name, RankingA, ScoreA, RankingB, ScoreB)
select a.Name, a.Ranking, a.Score, b.Ranking, b.Score
from TableA a
left join TableB b
on a.Name = b.Name
union all
select b.Name, a.Ranking, a.Score, b.Ranking, b.Score
from TableB b
left join TableA a
on b.Name = a.Name
where a.Name is null
Try this(should work in Oracle, SQL Server, MySQL):
To Create the third table(if it doesn't exist):
CREATE TABLE [TableNew] AS
SELECT a.Name
,a.Ranking RankingA
,a.Score ScoreA
,b.Ranking RankingB
,b.Score ScoreB
FROM TableA a, TableB b
WHERE a.Name = b.Name
To Insert into third table(if it exists):
INSERT INTO [TableNew]
SELECT a.Name
,a.Ranking RankingA
,a.Score ScoreA
,b.Ranking RankingB
,b.Score ScoreB
FROM TableA a, TableB b
WHERE a.Name = b.Name

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.