Solved

T-sql query to count  same value > 1

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

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 12

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now