I'm needing rows from the Employee table that have null values in OwnerID field and GMID field.
SELECT b.FirstName + space(1) + b.LastName AS OwnerName,
c.FirstName + space(1) + c.LastName AS GeneralManager
FROM Store a
JOIN Employee b ON a.OwnerID = b.EmployeeID
JOIN Employee c ON a.GMID = c.EmployeeID
The query is working...but the store table has null values in the OwnerID and the GMID fields. What changes do I need to bring back the null rows also?
Update
This is the corrected query.
SELECT b.FirstName + space(1) + b.LastName AS OwnerName,
c.FirstName + space(1) + c.LastName AS GeneralManager
FROM Store a
LEFT JOIN Employee b ON a.OwnerID = b.EmployeeID
LEFT JOIN Employee c ON a.GMID = c.EmployeeID
https://learn.microsoft.com/en-us/sql/t-sql/queries/from-transact-sql?view=sql-server-2017
Use left join instead of join.
The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match.
Type of joins: See more here
Related
I have written this SQL query that returns the users with first and last name if same name is found in two different databases database1 and database2, if the user's status is 'employee' in one of the tables in database1.
SELECT distinct
FirstName, LastName
FROM
database1.dbo.test1 a
JOIN
database1.dbo.test2 b ON b.id = a.id
JOIN
database1.dbo.test3 c ON a.id = c.id
JOIN
database2.dbo.test d ON a.firstname + ' ' + a.lastname = d.firstname + ' '+ d.lastname
WHERE
c.status = 'employee'
Now, I need to compare this first and last names I got using above query with the first and last name in database "database2" and if match is found I need to insert in column "isemployee" as yes. Can you please suggest how can I apply the condition to compare the names I got using above Select query and the names in database2 and insert in column "isemployee" of database2 if name match is found or condition is true.
You can use the code below...
UPDATE d SET isemployee = 'YES!'
FROM
database1.dbo.test1 a
join database1.dbo.test2 b on b.id = a.id
join database1.dbo.test3 c on a.id = c.id
join database2.dbo.test d
on a.firstname + ' ' + a.lastname = d.firstname + ' '+ d.lastname
where
c.status = 'employee'
All you need to do is to use your own query to update the column that you want, because the comparison already had been made
You could something like this using Exists
UPDATE d2
SET d2.isemployee = 1
FROM database2.dbo.test d2
WHERE EXISTS ( SELECT *
FROM database1.dbo.test1 a
JOIN database1.dbo.test2 b ON b.id = a.id
JOIN database1.dbo.test3 c ON a.id = c.id
WHERE c.status = 'employee'
AND a.firstname = d2.firstname
AND a.lastname = d2.lastname )
I have 2 tables; table A and B. My data looks as follows below. I'm trying to do 2 things:
How many times the Last Name and Zip in table A match the Last Name and Zip in table B?
If Last Name and Zip in Table A match Last Name and Zip in table B, how often do their phone numbers match?
Table A
Last Name..........Zip.....Phone
Tester..............00000....555555555
Tester..............00000....111111111
Test................11111.....99999999
Table B
Last Name..........Zip.....Phone
Tester..............00000....555555555
Tester..............00000....111111111
Test................11111.....99999999
This should give you the first answer
SELECT COUNT(A.Last Name) as namezipmatch
FROM A
INNER JOIN B ON A.LAST_NAME = B.LAST_NAME
WHERE
A.ZIP = B.ZIP
This should give you the second answer
SELECT COUNT(A.Last Name) as nameziphonematch
FROM A
INNER JOIN B ON A.LAST_NAME = B.LAST_NAME
WHERE
A.ZIP = B.ZIP AND A.PHONE = B.PHONE
Perhaps... Can't recall if access lets you count distinct.
SELECT count(Distinct B.Last_name + B.Zip) as CntLastZip,
count(Distinct C.Last_Name + C.Zip + C.Phone) as CntLastZipPhone
FROM TableA
LEFT JOIN tableB
ON A.Last_name = B.Last_Name
AND A.Zip = B.ZIP
LEFT JOIN tableC
ON A.Last_Name = C.Last-Name
AND A.Zip=C.Zip
AND A.Phone = C.Phone
I have two tables one is Friends table and other table is user profile table (all user related information e.g. firstname, lastname etc) both has relation among them
Friend table (It has two entries for every user for e.g. the first two rows)
Now i want to display names of users from above table which will look like below
so in the output i want to remove duplicates which is not working for me
my query
select distinct u.FirstName + ' ' + u.LastName As UserName,
(select distinct firstname + ' ' + lastname from UserProfiles where id = uw.friendid) as FriendName
from UserFriends as uw left join userprofiles as u
on u.id = uw.userid
You need to join UserProfiles twice on UserFriends since there are two columns are dependent on it.
SELECT a.ID,
f.FirstName + ' ' + f.LastName FriendName,
u.FirstName + ' ' + u.LastName UserName
FROM UserFriends a
INNER JOIN UserProfiles f
ON a.FriendID = f.ID
INNER JOIN UserProfiles u
ON a.UserID = u.ID
INNER JOIN UserFriends dup
ON a.FriendID = dup.UserID
AND dup.FriendID = a.UserID
AND a.ID > dup.ID
SQLFiddle Demo
I would suggest that you have to use CTE to eliminate the duplicates from the beggining(that can be done by self joining the table). I have managed to reproduce your scenario, so if you adjust the query a bit, you will be able to obtain the desired result
with cte as
(select
t1.id as a_id
,t1.friendID as a_friendID
,t1.userID as a_userID
,t2.id as b_id
,t2.friendID as b_friendID
,t2.userID as b_userID
from #temp t1
left join #user t2 on t1.id+1=t2.id
),
cte2 as
select
a_id
,a_userID
,a_friendID
from cte t1
where a_friendID = (select b_friendID from cte t2 where t2.b_id= t1.b_id-1)
)
select firstname+ ' '+lastname as FriendName
,firstname+ ' '+lastname as UserName
from cte2 uw
left join UserProfiles u on uw.a_userID=u.ID and uw.a_friendID=u.id
I've got the following SQL Statement:
select * from Leaves inner join LeaveDetails on Leaves.LeaveId= LeaveDetails.LeaveId
inner join Employee on Leaves.EmployeeCode = Employee.EmployeeCode
inner join LeaveType on Leaves.LeaveTypeId= LeaveType.LeaveTypeId
inner join LeaveStatus on Leaves.StatusId = LeaveStatus.StatusId
inner join Employee_organizationaldetails on Employee_organizationaldetails.EmployeeCode=Employee.EmployeeCode
where Leaves.LeaveId = 7295
Employee_organizationdetails contains another column called reporting officer which is a foreign key to the same Employee table. Now I need to get the name of the Employee.
How can I write the above query so that I can get the name of the reporting officer as another column without fetching executing the query
select (FirstName + ' ' + LastName) as name from Employee where EmployeeCode = ReportingTo
Here ReportingTo is the employee code. I need to join them vertically. Something similar to Union operator
You want to join back to another "copy" of the Employee table:
select *, (ro.FirstName + ' ' + LastName) as ReportingName
from Leaves inner join LeaveDetails on Leaves.LeaveId= LeaveDetails.LeaveId
inner join Employee on Leaves.EmployeeCode = Employee.EmployeeCode
inner join LeaveType on Leaves.LeaveTypeId= LeaveType.LeaveTypeId
inner join LeaveStatus on Leaves.StatusId = LeaveStatus.StatusId
inner join Employee_organizationaldetails on Employee_organizationaldetails.EmployeeCode=Employee.EmployeeCode left outer join
Employee ro
on ro.EmployeeCode = ReportingTo
where Leaves.LeaveId = 7295;
You probably don't want the * -- I assume it is just a shorthand for the question. It is better to list columns explicitly, especially because there are duplicate column names.
Possible this be helpful for you -
SELECT
*
, ReportingName = ro.FirstName + ' ' + LastName
FROM (
SELECT *
FROM dbo.Leaves l
WHERE l.LeaveId = 7295
) l
JOIN dbo.LeaveDetails ld ON l.LeaveId = ld.LeaveId
JOIN dbo.Employee e ON l.EmployeeCode = e.EmployeeCode
JOIN dbo.LeaveType lt ON l.LeaveTypeId = lt.LeaveTypeId
JOIN dbo.LeaveStatus ls ON l.StatusId = ls.StatusId
JOIN dbo.Employee_organizationaldetails e2 ON e2.EmployeeCode = e.EmployeeCode
LEFT JOIN dbo.Employee ro ON ro.EmployeeCode = ReportingTo
I have to add a contacts name to an assignments table by querying the contacts table.
Contact table:
ID First_name Last_Name
-----------------------------------
1234 John Jones
9876 Mary Smith
Assignment table
ContactID Name
-----------------
1234
9876
Using this query I get
Subquery returned more than 1 value.
Query:
update A
set Name = (select distinct first_name + ' ' + last_name from contacts c join Assignments A on c.id = A.contact_id where A.contact_id = c.id)
from Assignments A
join contacts c on c.id = A.contact_id
where c.id = A.contact_id
What am I missing?
JOIN them directly, like so:
UPDATE a
SET a.Name = c.first_name + ' ' + c.last_name
FROM Assignments a
INNER JOIN Contacts c ON c.id = A.contact_id
Try
Update A
Set A.name = c.First_name + ' ' + c.Last_name
From Assignment A JOIN Contacts C ON A.ContactID = C.ID
No need for a subselect
UPDATE A
SET Name = c.first_name + ' ' + c.last_name
FROM Assignments A
JOIN contacts c
ON c.id = A.contact_id
try this
UPDATE a
SET a.Name = c.first_name + ' ' + c.last_name
FROM Assignments a
INNER JOIN Contacts c ON c.id = A.contact_id