?
Solved

T-sql query to count  same value > 1

Posted on 2014-11-16
5
Medium Priority
?
299 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 400 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 600 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 600 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 400 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

800 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