Search This Blog & Web

Thursday, April 19, 2012

Return data set using (Commutative law of addition)

I have came through a very nice example today and want to share with you all. I know how to solve the problem but happy to know it is commutative law of addition and query to get that.

a+b = b+a

Here is a table tbl has four rows of data.

a      b
524, 307
594, 307
307, 524
307, 594
for each relationship in which a likes b and b likes a but the vice versa row should be rejected
result of the query should be
a  b
524,307
594,307


I have solved this using following query and result set.

but i am not satisfied with my query as i know there must be some good query to get this result. i have received many results but query that satisfy me is as below


I have found a problem in this query when we have same a+b with different values this query omit that. like 400+431 = 831 and 524+307 = 831. Now both above mentioned queries does not show one row even both have different a and b values.

There is another solution which looks like it returns correct data but there is a condition that single row data will not return there must be pair available. look at the attached screen below mention by JOHNQ



Post a Comment