Click here to Skip to main content
15,884,075 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have SQL table like this,
COMPANY    | CATEGORY | Session
Wills Corp |   7      |  1
Wills Corp |   7      |  1
Wills Corp |   7      |  2
Stan Co    |   2      |  1
Wills Corp |   5      |  1
Stan Co    |   2      |  1



This section added for clarity by PIEBALDconsult.

Select Distinct COMPANY,CATEGORY,Session from Table

COMPANY    CATEGORY Session
Stan Co    2        1
Wills Corp 5        1
Wills Corp 7        1
Wills Corp 7        2




And there are Many Duplicate Entries in Category and Session COMPANY..I want to find all that have duplicates in either 'Category' or 'Session' COMPANY..i want Output Like this..
COMPANY    | CATEGORY | Session   
Wills Corp |   7      |  1
Wills Corp |   7      |  2
Wills Corp |   5      |  1

I tried the Following Query and it return exact Company name,but i want Category and Session also...
SQL
Select COMPANY,COUNT(*) from (Select Distinct COMPANY,CATEGORY,Session from Table) Tbl group by COMPANY having COUNT(*) > 1
Posted
Updated 1-Nov-14 7:01am
v3
Comments
PIEBALDconsult 1-Nov-14 11:42am    
I'm having trouble trying to match your example data to your description.
Stan Co has duplicates, but you don't want them?
Can you maybe find a way to indicate which source rows match to which destination rows?
MichealRay 1-Nov-14 12:00pm    
I want to find duplicate in either Category and Session only...thats y StanCo not came..bcoz Stan Co have same Category and session...
PIEBALDconsult 1-Nov-14 12:21pm    
That really doesn't describe it any better.

How about this:

SQL
WITH cte0 AS
(
	SELECT COMPANY , CATEGORY , Session FROM Dupes GROUP BY COMPANY , CATEGORY , Session 
)
, cte1 AS
(
	SELECT COMPANY FROM cte0 GROUP BY COMPANY HAVING COUNT(*) > 1
)
SELECT A.*
FROM cte0 A
INNER JOIN cte1 B
ON A.COMPANY=B.COMPANY


The problem is that you are having trouble describing what you mean by "there are Many Duplicate Entries". You are not actually looking for duplicates in the source table, you are looking for duplicate company values once you have a list of unique records from the source. That is, duplicates in the result of your statement Select Distinct COMPANY,CATEGORY,Session from Table

COMPANY    CATEGORY Session
Stan Co    2        1
Wills Corp 5        1
Wills Corp 7        1
Wills Corp 7        2


I used a Common Table Expression (cte0) to hold that result.
http://technet.microsoft.com/en-us/library/ms190766(v=SQL.105).aspx[^]
(You could use a temporary table if you like.)

The output you specified is any row from that result for which a company name appears more than once.

In cte1 I selected the duplicated Company name(s).

And finally I JOINed the two results to get only those rows from cte0 that have a name that appears more than once.
 
Share this answer
 
v4
Comments
MichealRay 1-Nov-14 12:30pm    
Thank u PIEBALDconsult..This is almost what i need...5 Star..
PIEBALDconsult 1-Nov-14 12:41pm    
Almost? What more does it need? I hope, not ordering.
Manas Bhardwaj 2-Nov-14 6:35am    
CTE rocks +5!
Try below query:
SQL
SELECT Company, Category, Session, COUNT(*) AS Occurence
FROM TableName
GROUP BY Company, Category, Session
HAVING COUNT(*)>=1

It shows the count of each record.

Second way:
SQL
SELECT *
FROM (
    SELECT Company, Category, Session, ROW_NUMBER() OVER(PARTITION BY Company, Category ORDER BY Session) AS RowNo
    FROM TableName
) AS T
WHERE T.RowNo=1

It shows only first occurence of record ;)

If you would like to delete duplicates, you need to change this part: WHERE condition to RowNo>1

SqlFiddle[^]
 
Share this answer
 
v3
Comments
MichealRay 1-Nov-14 12:08pm    
I tried this Query Already,but it will not return exact output..
Maciej Los 1-Nov-14 12:10pm    
No, you did not. There is few differences...
[no name] 1-Nov-14 12:18pm    
The first query seems to be so what to be ok. 5 for the query.
Maciej Los 1-Nov-14 12:22pm    
Thank you, Bruno ;)
Manas Bhardwaj 2-Nov-14 6:35am    
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900