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
Solved

T-sql query to count  same value > 1

Posted on 2014-11-16
5
292 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
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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

791 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