sql server - mssql - retrieve unique values of a column based on another column -
i have table 2 columns: columna
, columnb
, rows:
| | 1 |
| b | 1 |
| b | 2 |
| c | 1 |
| c | 1 |
| c | 1 |
| | 2 |
| b | 1 |
| | 2 |
| | 1 |
i write query return unique values columnb
, each unique value of columna
, columna
has more 1 value in columnb
i.e.
| | 1 |
| | 2 |
| b | 1 |
| b | 2 |
c 1 should omitted because there 1 distinct value columna = 'c'
there might simpler approach works:
select t.columna, t2.columnb ( select columna dbo.tablename t group t.columna having count(distinct t.columnb) > 1) t cross apply ( select distinct t2.columnb dbo.tablename t2 t.columna=t2.columna ) t2
the first subquery returns unique columna
values have multiple (different) columnb
values. 2nd subquery returns distinct columnb
values of columna
-values cross apply
.
Comments
Post a Comment