Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have a table where there are certain combinations of numbers
MyTable:
col1 col2 priority
1 2 1
1 3 2
1 4 3
2 3 2
2 4 3
2 1 1
3 6 1
3 5 3
3 1 2
so..on
And i need to exclude the combination of {(1,2) or (2,1)} and {(1,3) or (3,1)} from mytable
But I am not able to exclude the duplicate combination of the numbers.
Please suggest me a better way to exclude duplicate combinations like 
once (1,2) combination has appeared in mytable, then it should not appear again as (2,1) combination.
How can it be excluded.Please suggest.
Thanks in Advance.

What I have tried:

I have tried using ROW_NUMBER()OVER(PARTITION BY col1 ORDER BY Col1,col3 DESC)
Posted
Updated 19-Mar-17 23:18pm
Comments
CHill60 17-Mar-17 8:57am    
Are you saying that you don't want to insert data into your table if the col1 / col2 combination already exists?
pn46 20-Mar-17 1:29am    
Hi,
Yes, I don't want to insert the same combinational column data again in the table, As once the table has already with a combination, it should not insert the duplicate combination again.
Please suggest how can i avoid such duplications.?

You can add a column to your query where you multiply the values from col1 and col2, then use OVER PARTITION or DISTINCT to get only those rows which don't have the same multiplication result.
 
Share this answer
 
Comments
pn46 20-Mar-17 1:38am    
Thanks alot. It has given much better results, but what if the combination has different values and the new column gives the same result, and while using PARTITION the column will skip to next row_number. Any other way to also check the duplicate combination ?
Thanks in Advance :)
NightWizzard 20-Mar-17 8:20am    
OK, I didn't assume that the occuring values may lead to same results from differing values. In this case you have to use any other numeric trick that leads to identically results where the order of the values in col1 and col2 does no matter. You may use a calculated column that concatenates the 2 values into a varchar, beginning with the lower value, convert the resulting string back to int as the result. In this case, the value order does no matter and you have a unique value for each combination that can be used for a DISTINCT query. Example for the formula of the calculated column:
CAST(CASE WHEN (col1 < col2) THEN CAST(col1 AS varchar)) + CAST(col2 AS varchar)) ELSE CAST(col2 AS varchar)) + CAST(col1 AS varchar)) AS int
I may be oversimplifying this but why not just query for the combo before adding a new row?
SQL
declare @n1 int = 2
declare @n2 int = 1
declare @p int = 21

IF NOT EXISTS (SELECT 1 FROM MyTable 
	WHERE (col1 = @n1 AND col2 = @n2) 
	   OR (col1 = @n2 AND col2 = @n1))
BEGIN
	INSERT INTO MyTable VALUES (@n1,@n2,@p)
END
ELSE
BEGIN
	UPDATE MyTable SET [priority]=@p
		WHERE (col1 = @n1 AND col2 = @n2) 
	   OR (col1 = @n2 AND col2 = @n1)
	-- I've updated. Don't know what you want to do here
END
 
Share this answer
 
Comments
pn46 20-Mar-17 5:35am    
Thanks alot :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900