views:

928

answers:

4

Hi,

I've got an MS access database and I would need to create an SQL query that allows me to select all the not distinct entries in one column while still keeping all the values.

In this case more than ever an example is worth thousands of words:

Table:

A B C
1 x q
2 y w
3 y e
4 z r
5 z t
6 z y

SQL magic

Result:

B C
y w
y e
z r
z t
z y

Basically it removes all unique values of column B but keeps the multiple rows of the data kept. I can "group by b" and then "count>1" to get the not distinct but the result will only list one row of B not the 2 or more that I need.

Any help?

Thanks.

+1  A: 
select 
  * 
from 
  my_table t1, 
  my_table t2
where 
  t1.B = t2.B
and
  t1.C != t2.C

-- apparently you need to use <> instead of != in Access
-- Thanks, Dave!

Something like that?

This is close, but you need to use <> instead of != in Access.
Dave DuPlantis
Um, why would you offer an answer about Jet SQL without testing the SQL in Access or some form of Jet? Why post an answer that you don't know will work (as you'd have found out if you'd spent the 3 seconds it takes to paste the SQL with != into the Access QBE grid)?
David-W-Fenton
Because I don't use Access and I don't have an installation available to me? The point of my posting was that you can use the same table twice in normal SQL (probably doesn't work in Access either, for all I know).
+1  A: 

join the unique values of B you determined with group by b and count > 1 back to the original table to retrieve the C values from the table.

Paul Morgan
+5  A: 

Another way of returning the results you want would be this:

select *
from
    my_table
where 
    B in 
    (select B from my_table group by B having count(*) > 1)
Dave DuPlantis
+5  A: 
Select B, C
From Table
Where B In
    (Select B From Table
     Group By B
     Having COunt(*) > 1)
Charles Bretana