Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my table

EmpID      EmpName          CompanyName                            CompanyID
123         Josep        Kramer Levin Naftalis & Frankel LLP            468
123         Josep        Thompson Hine LLP                              567
801         Simon        Ogletree Deakins International LLP             222
801         Simon        Ogletree, Deakins, Nash PC                     916
602         alen         Baker Co Ltd                                   732
602         alen         Baker Mcken  Ltd                               242


Condition is : Output will return, if the first word of the company name is not more than once, Ex:Baker and Ogletree these words are more then once so it is not include as Output

My Output like this
EmpID     EmpName           Company Name                            CompanyID
123        Josep       Kramer Levin Naftalis & Frankel LLP            468
123        Josep       Thompson Hine LLP                              567
Posted
Updated 21-Oct-15 4:18am
v2
Comments
Duncan Edwards Jones 21-Oct-15 12:31pm    
I would suggest you have a data quality issue if these are in fact the same company?
Member 12076430 21-Oct-15 13:20pm    
no
phil.o 21-Oct-15 14:11pm    
Wow, what a laconic precision ^^
[no name] 21-Oct-15 16:13pm    
But short ...and... I Need at least for this answer no Google translate :lol:

Interestingly using an aggregate function and over clause -
a count can be performed on
SQL
LEFT(CompanyName, CHARINDEX(' ', REPLACE(CompanyName, ',', ''))-1)
--gets the first word of the company name as used in solution 1 (thanks Maciej Los)

Like so:
SQL
with Employee as (
--set up dummy data
	select 
		123 EmpID, 
		'Josep' EmpName, 
		'Kramer Levin Naftalis & Frankel LLP' CompanyName, 
		468 CompanyID
	union all select 123, 'Josep', 'Thompson Hine LLP', 567
	union all select 801, 'Simon', 'Ogletree Deakins International LLP', 222
	union all select 801, 'Simon', 'Ogletree, Deakins, Nash PC', 916
	union all select 602, 'alen', 'Baker Co Ltd', 732
	union all select 602, 'alen', 'Baker Mcken  Ltd', 242
)
select * from (
	select 
		*,
		count(*) over(partition by LEFT(CompanyName, CHARINDEX(' ', REPLACE(CompanyName, ',', ''))-1)) CompanyFirstWordCnt
	from Employee
) EmployeeCompanyFirstWordCnt
where CompanyFirstWordCnt = 1  --filter when count is 1
;
 
Share this answer
 
Comments
Maciej Los 23-Oct-15 9:29am    
You're very welcome.
Have a 5!
jaket-cp 23-Oct-15 9:58am    
thanks :)
As Duncan Edwards Jones[^] mentioned in the comment to the question, you have to repair data in a CompanyName field.

At this moment, my best guess is:
SQL
DECLARE @tmp TABLE(EmpID INT, EmpName VARCHAR(50), CompanyName VARCHAR(155), CompanyID INT)

INSERT INTO @tmp(EmpID, EmpName, CompanyName, CompanyID)
VALUES(123, 'Josep', 'Kramer Levin Naftalis & Frankel LLP', 468),
(123, 'Josep', 'Thompson Hine LLP', 567),
(801, 'Simon', 'Ogletree Deakins International LLP', 222),
(801, 'Simon', 'Ogletree, Deakins, Nash PC', 916),
(602, 'alen', 'Baker Co Ltd', 732),
(602, 'alen', 'Baker Mcken  Ltd', 242)


SELECT EmpID, LEFT(CompanyName, CHARINDEX(' ', REPLACE(CompanyName, ',', ''))-1) AS ShortComanyName
FROM @tmp
GROUP BY EmpID, LEFT(CompanyName, CHARINDEX(' ', REPLACE(CompanyName, ',', ''))-1)
HAVING COUNT(EmpID)=1


Above query returns:
EmpID	ShortComanyName
123		Kramer
123		Thompson
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900