Select mismatch column values from database - sql-server

I want to select and fetch all mismatch value of two table having same Column Name But Id, Name and City are different. I am Using Sql Server Management Studio
Table A:
id Name City
1 John karachi
2 smith Capetown
3 liza Washington
Table B:
id Name City
7 Grey Dubai
8 Clarke Texas
9 liza Washington
OUTPUT:
7 Grey Dubai
8 Clarke Texas

This has been answered hundreds and hundreds of times. Not quite sure what you mean by "having same Column Name But Id, Name and City are different" but here are a couple of example of how you can do this.
You can use a left join where tablea is null,
select b.Name
, b.City
from tableb b
left join tablea a on a.name = b.name
and a.city = b.city
where a.name is null
You can use except.
select Name
, City
from tableb
except
select Name
, City
from tablea

You can try the below query. Assuming that the id column is never null
select tb.*
from tableB tb
left join tableA ta
on tb.Name=ta.Name and tb.City=ta.City
where ta.id is NULL
Link to demo sql fiddle: http://sqlfiddle.com/#!3/250e5/1

Related

How do I join 2 tables in SQL Server

I have two tables as follows that the id values of the tblBranch table in the tblPost table are in the Cityid, catid, Desid columns
How can I join these two tables together to see the values of cityid, catid, Desid?
tblBranch tblPost
---------- -----------
id id
Name title
cityid
catid
Desid
When you are join tables, you are bringing/combine TWO TABLES data rows
and their columns into ONE BIG TABLE base on the MATCHING JOIN COLUMNS.
Here is example of inner join:
tblDepartment
--------------
id_Dept NameDept
1 Math
2 Physic
tblTeacher
----------
id_Teacher FullName id_Dept
1 Smith Adam 1
2 John Doe 1
3 Marry Doe 2
EXAMPLE 01: No rows filter
select TC.id_Teacher, TC.FullName, TC.id_Dept, TD.NameDept from tblTeacher TC inner join tblDepartment TD on TC.id_Dept = TD.id_Dept
Result ONE BIG TABLE OF JOINING TABLES
id_Teacher FullName id_Dept NameDept
1 Smith Adam 1 Math
2 John Doe 1 Math
3 Marry Doe 2 Physic
EXAMPLE 02: With rows filter
Result ONE BIG TABLE OF JOINING TABLES with SQL where clause to limit smaller set of data rows return
select TC.id_Teacher, TC.FullName, TC.id_Dept, TD.NameDept from tblTeacher TC inner join tblDepartment TD on TC.id_Dept = TD.id_Dept
where TC.id_Dept = 2
id_Teacher FullName id_Dept NameDept
3 Marry Doe 2 Physic
You can use the above example to see WHAT COLUMNS your table should join to return result with BIG TABLE
So if I understand you correctly, you want to take CityId, CatId and DesId from tblPost, and for each of them, you want to join to the tblBranch table and show the Name for that Id - correct?
In that case, you need a query like this:
SELECT
CityId,
CityName = brCity.Name,
CatId,
CatName = brCat.Name,
DesId,
DesName = brDes.Name
FROM
tblPost p
LEFT OUTER JOIN
tblBranch brCity ON p.CityId = brCity.Id
LEFT OUTER JOIN
tblBranch brCat ON p.CatId = brCat.Id
LEFT OUTER JOIN
tblBranch brDes ON p.DesId = brDes.Id
Basically, you need to have three joins - one for each of the Id in your tblPost table.
I chose to use LEFT OUTER JOIN to avoid eliminating a row completely, if one of the Id cannot be found/matched in the tblBranch table.

Retrieving borrower name based on borrowertype id from another table in SQL Server

I have two tables, Customer and RegisteredCustomer.
Customer table has the first and last name of customers including the family members stored against each Residentid.
The RegisteredCustomer has the CustomerType as 1 being primary and 2 being FamilyMembers.
I want to select both the primary and Familymember displayed side by side.
Select FirstName, LastName
from Customer
Inner Join RegisteredCustomers on Customer.Customerid = RegisteredCustomer.Customerid
and CustomerType = 1
How can I achieve it?
Thanks
Try it like this:
Select a.Customerid,FirstName, LastName,b.CustomerType from
(Select Customerid,FirstName, LastName from Customer) as a
left join
(select Customerid,cusdescription,CustomerType from RegisteredCustomers)as b
on a.Customerid = b.Customerid where CustomerType = 1
Hope it could help.

SQL Select Count Statement

I have two tables, one for Employees and the second for records. I want to get total entries for each employee and order the results by max total entry like:
Daniel 3
David 1
tblEmployee:
EID Name
1 Daniel
2 David
tblEntry:
ID Column1 EID
1 XX 1
2 XX 1
3 XX 2
4 XX 1
try this:
select emp.EID,emp.Name,COUNT(etr.EID)
as total_entries from Employee emp join Entry etr
on emp.EID=etr.EID
group by emp.EID,emp.Name
You must use group by
select count(*) from tblEmployee ee, tblEntry e where ee.eid = e.eid group by ee.name
There are several variations on this, and you don't say what version of SQL Server you're using, but I like doing it this way:
;
using A
as (
select EID
, RecordCount = COUNT(*)
from tblEntry
group by
EID
)
select a.EID
, e.Name
, a.RecordCount
from A
join tblEmployee e
on A.EID = e.EID
order by
RecordCount desc
I like doing it this way rather than joining and then summarizing because you only have to group on the minimum number of fields. EID in tblEntry is likely to already have an index on it, while Name in tblEmployee may not.

Join where records dont match

I need to do a join on two tables, I need EVERYTHING from table A, and just the stuff from table B where the 'REF' for each row, is not already in table A's results.
Key Facts:
Table B contains the complete amount of ID's/Names however all the other information is blank. Its basically just a table with all employee's name's and id's but its a complete list.
Table A contains a limited amount of results but all the other columns have data in them.
What I need is to use Table B as a complete reference rather than just see what exists in table A so basically:
"Show me everything from table A, and add the extra people's details
found in table B where they dont already exist in table A to give me a complete result set"
select
ID,
Name,
StartDate,
EndDate,
State,
Status,
Comment,
IsComment
from
tableA
select
ID,
Name,
StartDate,
EndDate,
State,
Status,
Comment,
IsComment
from
tableB
Table A contents:
ID Name START_DATE END_DATE STATE Status Comment Is_Comment
6760 chris 2012-09-03 2012-09-09 4 Applied 0
6524 dave 2012-09-03 2012-09-09 4 Applied 0
4268 james 2012-09-03 2012-09-09 4 Applied Friday-Off 1
7851 rob 2012-09-03 2012-09-09 4 Applied 0
Table B contents
ID Name START_DATE END_DATE STATE Status Comment Is_Comment
6760 Chris
6524 dave
4268 james
7851 rob
4521 ryan
5698 julie
4596 rory
1111 mary
5621 owain
9999 jacob
After the join what I want to see:
ID Name START_DATE END_DATE STATE Status Comment Is_Comment
6760 chris 2012-09-03 2012-09-09 4 Applied 0
6524 dave 2012-09-03 2012-09-09 4 Applied 0
4268 james 2012-09-03 2012-09-09 4 Applied Friday-Off 1
7851 rob 2012-09-03 2012-09-09 4 Applied 0
4521 ryan
5698 julie
4596 rory
1111 mary
5621 owain
9999 jacob
Try this:
select
tableB.ID,
tableB.Name,
tableA.StartDate,
tableA.EndDate,
tableA.State,
tableA.Status,
tableA.Comment,
tableA.IsComment
from
tableB
LEFT JOIN tableA on tableB.ID = tableA.ID
Because then every ID and Name will be listed from tableB, and every other column is listed from tableA.
If there is no connection between the tables, then the other columns got the NULL from TableA - because of LEFT JOIN -, if there is a connection then you get the filled values from TableA also.
Here is an SQL fiddle how this simple solution works.
Please use this SQL:
declare #tableA table
(
ID int,
Name nvarchar(250),
Age int
)
declare #tableB table
(
ID int,
Name nvarchar(250),
Age int
)
Insert #tableA values (1,'a',10);
Insert #tableA values (2,'b',20);
Insert #tableB values (1,'a',null);
Insert #tableB values (2,'b',null);
Insert #tableB values (3,'c',null);
Insert #tableB values (4,'d',null);
select tblResult.*,T1.Age from
(
select ID,Name from #tableA
union
select ID,Name from #tableB) as tblResult
left join #tableA as T1 on tblResult.ID =T1.ID
Use a Left join
SELECT [columnsListYouNeed]
FROM TableB
LEFT JOIN TableA ON TableA.ID = TableB.ID

How can I add an integer from another table to the current selected table in SQL Server?

Does anyone know how can I add an integer from another table to the current selected table in SQL Server?
For example:
I have 2 tables with the following information in each table
tableA:
id username point status country
1 alvin 1 1 U.S.A
2 alvin 1 1 U.S.A
3 amy 1 0 Australia
tableB:
id username point
1 amy 1
2 alvin 1
3 ken 1
How can I sum up the total points in tableA with also add in the sum points from tableB?
I tried the following code, but seem is not working and error display:
SELECT username, (COUNT(distinct a.point) + (SELECT SUM(a.point)
FROM tableB b WHERE b.username = a.username) AS 'Points', status, country
FROM tableA
GROUP BY aco.username
And the output I expected will be:
username Points status country
alvin 3 1 U.S.A
amy 2 0 Australia
WITH Results(username,point)
AS
(
SELECT username, point FROM TableA
UNION ALL
SELECT username, point FROM TableB
)
SELECT username, sum(point) AS Points FROM Results GROUP BY username
GO
EDIT
The question has changed, so now the solution should look like this
WITH Results(username,point,status, country)
AS
(
SELECT username, point, status, country FROM TableA
UNION ALL
SELECT username, point, null, null FROM TableB
)
SELECT username, sum(point) AS Points, max(status), max(country) FROM Results GROUP BY username
GO
What is WITH ?
What is UNION ?
You don't mention why Ken doesn't appear in the output table, I assume that TableA is the 'master' list. If so I would do the following INNER JOIN which is the most simple solution.
SELECT a.username AS Username, SUM(ISNULL(a.point,0)+ISNULL(b.point,0)) as Points,
MAX(a.Status) as Status, MAX(a.Country) as Country
FROM TableA a
INNER JOIN TableB b
ON a.username=b.username
GROUP BY a.username

Resources