Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- find examples where the 1st-column value exists on more than one second-column value to test this.
- Select TOP 10 [Col_1], count(1) as countRows_outer from
- (
- SELECT [Col_2]
- ,[Col_1]
- ,count(1) as countRowsInner
- FROM [OurDatabase].[dbo].[OurTable]
- WHERE
- (
- (Col_1 is not null)
- and
- (len (Col_1) > 0)
- )
- group by [Col_2] ,[Col_1] -- after studying: Inner group by *NOT* needed
- having (count(1) >= 2) -- not really needed, but limits search set, faster query results
- --order by [Col_1], [Col_2] -- , countRows desc
- )c1
- group by Col_1
- having(count(1) >= 2) -- > 1 (per answer below, may be more efficient here)
- order by countRows_outer desc
- SELECT col1
- FROM
- table
- GROUP BY col1
- HAVING COUNT(DISTINCT col2)> 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement