in T-SQL, how can i recover all rows that have a column that appear more than once ?

Hello,

i've a table with columns a, b, c, d

how can recover all the rows where b appear more that once.

having a result with the columns a, b, c, d and a complementary columns "d" that has the number of occurences ?

thank you in advance.

toshi
Erwin PombettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>where b appear more that once.
>and a complementary columns "d" that has the number of occurences ?
Explain for us at Barney-level what you mean here, as the above two statements seem to contradict each other.

Even better would be a data mockup both before and after.
0
Mike EghtebasDatabase and Application DeveloperCommented:
try:
;With D
As
(
Select row_number() over (partition By b Order By b) As rn,
       a
	 , b
	 , c
	 , d
From #t 
)
Select a, b, c, d, rn-1 As Repeats from d;

Open in new window


It returns:
1       b        c         d            Repeats             
1      10      100      1000      0
1      10      100      1000      1
1      10      100      1000      2
1      11      100      1000      0
1      12      100      1000      0
1      13      100      1000      0
1      14      100      1000      0

Using:
CREATE TABLE #t(
	[a] [int] ,
	[b] [int] ,
	[c] [int] ,
	[d] [int]
) 

insert into #t(a,b,c,d) values
  (1,10,100,1000)
, (1,10,100,1000)
, (1,11,100,1000)
, (1,10,100,1000)
, (1,12,100,1000)
, (1,13,100,1000)
, (1,14,100,1000);

select * from #t;

Open in new window

0
Erwin PombettAuthor Commented:
Hello eghtebas,

thanks a lot for your query.  
could you help me to understand ?

i'm trying to figure out how this is working ?

thank you in advance.

toshi
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Erwin PombettAuthor Commented:
Jim Horn,

thank you to make me discover "Barney" ;)  You were right, i wasn't clear enough.
Next time i 'll follow your tip: data mockup both before and after.

luckyly eghtebas read on my mine.

toshi
0
Mike EghtebasDatabase and Application DeveloperCommented:
Here, I have used a subquery (of type CTE) with an outer qyery. For more info on CTEs see Subqueries at a glance.

;With D                                 --<-- subquery starts here
As
(
Select row_number() over (partition By b Order By b) As rn,
       a
	 , b
	 , c
	 , d
From #t 
)
Select a, b, c, d, rn-1 As Repeats from d;     --<--  outer query query starts and ends here

Open in new window


The CTE (common table expression) fires first. It returns columns a, b, c ,d from your table (replace #t with your own table name). CTE D also includes aother key column aliased as rn. This column numbers data rows partitioned by column b and sorted by the same (you can sort it by another column). This  row_number  function groups column b rows and displays them one after another.

What else in addition to what we have you can do:

1. Change Order By b to Order By a or something else that makes more sense and it is more useful.
2. After "Select a, b, c, d, rn-1 As Repeats from d" add, for example,
 Where rn =2               to displays the 2nd repeats only, or
 Where rn<= 2             to displays 1st and its 2nd repeat, or
 Where rn =< 7            to displays 1st and its 6 repeats

Mike

This post has been modified
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Erwin PombettAuthor Commented:
on of the best answers i ever received.
i asked for explanation and i received them ;)
thank you so much.
toshi
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.