samedi 25 avril 2015

SQL Compare rows


Okay here is the situation: I the following data in a table.

PAIR_NO NO          NO2

3       5678EFGH    1234ABCD
4       1111BBBB    0000AAAA
1       1234ABCD    5678EFGH
2       0000AAAA    1111BBBB

The constraints are if no = no2 in another row skip that row. So in this sample data the only rows that would be selected should be pair no 3 and 4.

I have tried to merge and inner join with self but I just keep getting all 4 rows back.

I have tried to insert into a table where not exists but again I get 4 rows inserted.

   SELECT a.* from PAIRS a
   inner join PAIRS b on a.no=b.no2 and a.no2=b.no;

I was thinking maybe selecting distinct number from column 1 and then check those in column 2 but I think that would yield the same four rows.

I may be over thinking this problem and maybe some here can look at this and see where the solution is hiding.

I am currently testing this on MySQL but it should run on SQLServer 2008. I have searched but all the questions didn't seem to match my data set issue.


Aucun commentaire:

Enregistrer un commentaire