Solved

T-sql query to count  same value > 1

Posted on 2014-11-16
5
286 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:ScottPletcher
ScottPletcher 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

744 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

9 Experts available now in Live!

Get 1:1 Help Now