Solved

T-sql query to count  same value > 1

Posted on 2014-11-16
5
295 Views
Last Modified: 2014-11-24
select * from vNameEmp  where personal='078922251'

a query above  return me 3 rows which 2 of the rows has white space at the  end '078922251 '

But when I query to search  how many are duplicate in vNameEmp, the query below result me empty.

select company,id,count(personal) as personaloQty
from vNameEmp
--where userid=94
group by company,Id,personal   having count(*)>1

and I'm trying  with trim but did not get success.

   select rtrim(ltrim(personal_no)),company,id,count(personal) as personaloQty
from vNameEmp
--where userid=94
group by company,Id,personal   having count(*)>1

anyone can help me to build this query ?
0
Comment
Question by:motioneye
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 34

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 100 total points
ID: 40446686
try;

select * from vNameEmp  where personal='078922251' OR IsNull(personal)

I think the following should work also:

select * from vNameEmp  where IsNull(personal,'')='078922251'
0
 
LVL 13

Accepted Solution

by:
Koen Van Wielink earned 150 total points
ID: 40446706
I'm working the assumption here that ID is a unique number for each record. If that's the case, grouping by ID will always result in unique records, i.e. your count will always be 1.
If you're trying to see which personal numbers are used more than once per company, try this:
Create table #vNameEmp
(	company		nvarchar(12)
,	id			int
,	personal	nvarchar(20)
)

insert into #vNameEmp
values		('ABC',	1, '078922251')
		,	('ABC',	2, '078922251 ')
		,	('ABC',	3, '078922251')
		
select		company
		,	LTRIM(RTRIM(personal)) as 'Personal'
		,	count(LTRIM(RTRIM(personal))) as personaloQty
from	#vNameEmp 
--where userid=94
group by	company 
		,	LTRIM(RTRIM(personal))
having count(LTRIM(RTRIM(personal)))>1

drop table #vNameEmp

Open in new window


If ID is not unique, please provide some sample data from your table. But the fact you're not getting any records back is because your group by clause is resulting in a count of 1 for every returned record.
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 150 total points
ID: 40448137
You don't need to RTRIM -- trailing spaces or not will not make the values different to SQL.  That is, '1234' = '1234 ' = '1234   '.
Therefore::

SELECT vne.*
FROM vNameEmp vne
INNER JOIN (
    SELECT personal
    FROM vNameEmp
    GROUP BY personal
    HAVING COUNT(*) > 1
) AS vne_dups ON
    vne_dups.personal = vne.personal
0
 
LVL 9

Assisted Solution

by:Valliappan AN
Valliappan AN earned 100 total points
ID: 40449304
Adding to what others said, this should give a complete list of all duplicates, imo:

SELECT * FROM vNameEmp
WHERE RTRIM(LTRIM(personal)) IN (
    SELECT RTRIM(LTRIM(personal))
    FROM vNameEmp
    GROUP BY RTRIM(LTRIM(personal))
    HAVING COUNT(*)>1
)
ORDER BY personal

Also, please mention the data types of personal, if its nvarchar, a possibility of other characters similar to space,  can make issues.

HTH.
0
 

Author Closing Comment

by:motioneye
ID: 40462629
Thanks all for the help ...
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question