Query Using != On LEFT JOIN Table Returns No Result - sql-server

I am querying a Microsoft SQL Server 2012.
The primary table (T1) structure contains account details:
AccountID, Name, Address
This table is dropped and recreated using external data nightly. We need to display this information but also need to exclude some of the records. Since we have no access to the external data we can't just add a column.
So we created a table (T2) to mark all the accounts we would like to exclude. It just has 2 fields:
AccountNo, Type
So we populated T2 and for every account we wanted to exclude from the display we gave the Type field a value of 'ex' (for exclude). We have no entries for the account we want to display.
When I execute the following query:
select T1.AccountID as acct, T1.Name as name, T1.Address as add
from T1
left join T2 on T1.AccountID = T2.AccountNo
WHERE T2.Type != 'ex'
The above query returns and empty set.
If I run a query to look for the value 'ex' (remove the !):
select T1.AccountID as acct, T1.Name as name, T1.Address as add
from T1
left join T2 on T1.AccountID = T2.AccountNo
WHERE T2.Type = 'ex'
The query returns the rows with that field populated with 'ex', as you expect.
I can search for NULL or NOT null with success but we need to use this extra table to do some other data manipulation in the future. In other words, we will not just be populating this field with "ex".
I'm wondering why I can't query the field in the joined table by looking for a Boolean false for a string. Is is because since the column doesn't exist in the table that is joined (T2) that it doesn't actually exist in the data set?
If that's the case how would I execute a query to return the records that do not equal a value in the joined table, whether that record exists in the joined table or not.

You can use the ISNULL solution like mentioned in the comments.
Another way you could write the query is this:
SELECT #t1.AccountID AS acct, #t1.Name AS [name], #t1.Address AS [add]
FROM #t1
LEFT JOIN #t2 ON #t1.AccountID = #t2.AccountNo
AND #t2.type = 'ex' --In case you add additional types to #t2
WHERE #t2.AccountNo IS NULL;

Related

Using group by to update multiple records in an update query

I have two tables. Table1 has company data (Company ID, Company Name ...), so single record for each company.
Table2 has information about departments in that company (Department ID, Department Name, Company ID, Company Name ... ). So, second table might have n number of records where same company id is used.
Problem is one of our trigger failed to work properly, and no one noticed till now. So, when Company Name was updated in Table1, it never reflected in Table2.
To correct this, I have to do something like the below query:
Update Table2
Set
[Company Name] = (select [Company Name]
from Table1
where Table2.Company ID = Table1.Company ID)
Group By Table2.Company ID
Basically, I am trying to update all records in Table2 to use the same name as Table1, for each record in Table1.
I am a bit confused about how to create the inner select clause.
P.S. Sorry, it might be a bit confusing. Kindly do let me know how to reword it the best.
Don't need to use group by ...
UPDATE T2
SET [Company Nane] = T1.[Company Nane]
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.[Company ID] = T2.[Company ID]
As other have mentioned: Remove GROUP BY and your query works fine.
GROUP BY is used to produce a result row in a query that is an aggregate of other rows. E.g. one record per company from your department table with the most busy department per company. You cannot update such a result record, for that record does not exist in the table. You can only update table records.
So remove GROUP BY from your query and you have it straight-forward.
Update DepartmentTable
Set [Company Name] =
(
select [Company Name]
from CompanyTable
where CompanyTable.[Company ID] = DepartmentTable.[Company ID]
);

sql server select from two tables as null or value

This shouldn't be hard but somehow I am unable to make this work. So I have two tables: Splanning_Buildings and Splanning_RoomData. The primary key of OBJECTID in the Splanning_Buildings table is represented as Objectid_bldg in the Splanning_RoomData table.
What I need is to select all rows from the Splanning_Buildings table in such a way that if its value is found inside the Splanning_RoomData table then show the value as "Existing" string; but if not found in Splanning_RoomData then show as blank.
This is SQL Server 2012.
Here is my sql:
SELECT t1.objectid, name, t2.building_n FROM splanning_buildings t1 LEFT JOIN splanning_roomdata t2 ON t1.objectid = t2.objectid_bldg WHERE t2.objectid_bldg IS NULL
SELECT t1.objectid, name, t2.building_n FROM splanning_buildings t1 LEFT JOIN splanning_roomdata t2 ON t1.objectid = t2.objectid_bldg
The first query returns all rows from the splanning-buildings table except the row which exists the splanning_RoomData table; so t2.building_n is 'null'. Makes sense.
The second query returns all rows from the buildings table but multiple t2.buildings_n rows.
None of these are the desired results! I guess I need some kind of sql Case() function?
Any idea?
Thanks!
SELECT t1.objectid, name,
CASE WHEN (EXISTS(SELECT TOP(1) 1 FROM splanning_roomdata t2 WHERE t1.objectid = t2.objectid_bldg )) THEN
'Exists'
ELSE 'Not exists' END AS [status]
FROM splanning_buildings t1
select objectid,
(select top 1 1 from room_data
where objectid_bldg=objectid)
as roomdata_exists from buildings
I simplified your table names a little bit - hope you can still make sense of it. ;-)

2 nvarchar fields are not matching though the data is same?

I want to join 2 tables using an Inner Join on 2 columns, both are of (nvarchar, null) type. The 2 columns have the same data in them, but the join condition is failing. I think it is due to the spaces contained in the column values.
I have tried LTRIM, RTRIM also
My query:
select
T1.Name1, T2.Name2
from
Table1 T1
Inner Join
Table2 on T1.Name1 = T2.Name2
I have also tried like this:
on LTRIM(RTRIM(T1.Name1)) = LTRIM(RTRIM(T2.Name2))
My data:
Table1 Table2
------ ------
Name1(Column) Name2(Column)
----- ------
Employee Data Employee Data
Customer Data Customer Data
When I check My data in 2 tables with
select T1.Name1,len(T1.Name1)as Length1,Datalength(T1.Name1)as DataLenght1 from Table1 T1
select T2.Name2,len(T2.Name2)as Length2,Datalength(T2.Name2)as DataLenght2 from Table2 T2
The result is different Length and DataLength Values for the 2 tables,They are not same for 2 tables.
I can't change the original data in the 2 tables. How can I fix this issue.
Thank You
Joins do not have special rules for equality. The equality operator always works the same way. So if a = b then the join on a = b would work. Therefore, a <> b.
Check the contents of those fields. They will not be the same although you think they are:
select convert(varbinary(max), myCol) from T
Unicode has invisible characters (that only ever seem to cause trouble).
declare #t table (name varchar(20))
insert into #t(name)values ('Employee Data'),('Customer Data')
declare #tt table (name varchar(20))
insert into #tt(name)values ('EmployeeData'),('CustomerData')
select t.name,tt.name from #t t
INNER JOIN #tt tt
ON RTRIM(LTRIM(REPLACE(t.name,' ',''))) = RTRIM(LTRIM(REPLACE(tt.name,' ','')))
I would follow the following schema
Create a new table to store all the possible names
Add needed keys and indexes
Populate with existing names
Add columns to your existing tables to store the index of the name
Create relative foreign keys
Populate the new columns with correct indexes
Create procedure to perform an insert in new tables names only in case the value is not existing
Perform the join using the new indexes

Update table based on join multiple rows SQL Server

I'm trying to update some rows in a table joined to another table. The left table is indexed by ID; the right table is joined on the same ID but has multiple rows per ID. My query looks like:
UPDATE t1
SET t1.modified = t2.created
FROM table1 t1
INNER JOIN table2 t2
ON t1.ID = t2.ID
Note that t1.modified and t2.created are both datetime.
As stated, t2 has several rows per ID, each with a different created value (so the primary key is t2, created). What I want to do is set the max value of t2.created=t1.modified. However, when joining, the t1.modified value gets updated in no particular order, so whichever row is updated last, that's the value that t1.modified gets. I tried using t1.modified=max(t2.created), but apparently I can't use aggregate functions in an update query, nor can I use an order clause (i.e. order the rows so that the last row updated will effectively be the latest value of t2.created) in the update query.
Any help you can provide me is much appreciated! Thanks!!
how about this? will this fit your need?
UPDATE t1
SET modified = isnull((SELECT max(t2.created)
FROM table2 t2
WHERE t2.ID = t1.ID), modified)
FROM table1 t1;
Use the isnull function to set modified to be itself if the value returned is null. that should take care of the null issue.
You could do it this way, although it may not be standard SQL. However it should work for SQL Server.
update t1
set t1.modified = (select max([created]) from t2 where t1.ID = t2.id)

How to get the latest inserted identity from a table?

I would like to retrieve the latest inserted or even updated identity from a table. But whenever I write the following code there is no results at all.
select *
from PersonHowEducation prh
inner join HowzeEducation he on prh.HowzeEducationId = he.HowzeEducationId
where he.HowzeEducationId = ##IDENTITY
What shall I really do ?
If this were part of a batch script, and you have only just inserted it in the same session, then you would use SCOPE_IDENTITY() to retrieve the last inserted HowzeEducationId value.
select * from PersonHowEducation prh inner join HowzeEducation he on
prh.HowzeEducationId=he.HowzeEducationId
where he.HowzeEducationId=SCOPE_IDENTITY()
However, if this is from a completely different session, one that DID NOT JUST perform the insert, then you can get the last record inserted using
select * from PersonHowEducation prh inner join HowzeEducation he on
prh.HowzeEducationId=he.HowzeEducationId
where he.HowzeEducationId=(select top(1) HowzeEducationId from HowzeEducation
order by HowzeEducationId desc)
Note: There could be rare cases where an identity insert does not create the largest id in the table, for example if someone decided a table should have 1 record of id= 1,000,000,001 and manually reseeded the identity back to 10,001.
Another alternative, which may fizz if it is possible for the record to be deleted after insertion.
select * from PersonHowEducation prh inner join HowzeEducation he on
prh.HowzeEducationId=he.HowzeEducationId
where he.HowzeEducationId=(SELECT IDENT_CURRENT('HowzeEducation'))

Resources