Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. -- find examples where the 1st-column value exists on more than one second-column value to test this.
  2.  
  3. Select TOP 10 [Col_1], count(1) as countRows_outer from
  4. (
  5. SELECT [Col_2]
  6. ,[Col_1]
  7. ,count(1) as countRowsInner
  8.  
  9. FROM [OurDatabase].[dbo].[OurTable]
  10. WHERE
  11. (
  12. (Col_1 is not null)
  13. and
  14. (len (Col_1) > 0)
  15. )
  16. group by [Col_2] ,[Col_1] -- after studying: Inner group by *NOT* needed
  17. having (count(1) >= 2) -- not really needed, but limits search set, faster query results
  18. --order by [Col_1], [Col_2] -- , countRows desc
  19. )c1
  20. group by Col_1
  21. having(count(1) >= 2) -- > 1 (per answer below, may be more efficient here)
  22. order by countRows_outer desc
  23.  
  24. SELECT col1
  25. FROM
  26. table
  27. GROUP BY col1
  28. HAVING COUNT(DISTINCT col2)> 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement