Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 compare multiple fields

Author  Topic 

petebob796
Starting Member

35 Posts

Posted - 2007-09-12 : 09:09:16
I have an odd table I would like to group together to only get unique records where the data is the same in 4 fields but the data could be in any order over the 4 fields example below:
Field 1 Field 2 Field 3 Field 4
10 20 30 40
20 10 40 30
50 10 30 20

so if my database was like the above the first 2 records would be grouped because they contain the same data just in a different order (not bothered which is selected). There could in the future be more fields so I would like to get it right now.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-09-12 : 09:15:00
Why don't you normalize your table ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Jenda
Starting Member

29 Posts

Posted - 2007-09-12 : 14:37:59
quote:
Originally posted by petebob796

I have an odd table I would like to group together to only get unique records where the data is the same in 4 fields but the data could be in any order over the 4 fields example below:
Field 1 Field 2 Field 3 Field 4
10 20 30 40
20 10 40 30
50 10 30 20

so if my database was like the above the first 2 records would be grouped because they contain the same data just in a different order (not bothered which is selected). There could in the future be more fields so I would like to get it right now.


That's not the best table layout ... would you mind if the table was modified in the process? What I mean is ... does the order of the values in those fields matter? If you can change the order, the it would be best to sort the values first and then you can group it easily. And it might actually make a lot of sense to always keep the values sorted ... depending on the app.

If this was OK you could do something like

UPDATE OddTable
SET Field1 = Field2, Field2 = Field1
WHERE Field1 > Field2
UPDATE OddTable
SET Field2 = Field3, Field3 = Field2
WHERE Field2 > Field3
UPDATE OddTable
SET Field3 = Field4, Field4 = Field3
WHERE Field3 > Field4
UPDATE OddTable
SET Field1 = Field2, Field2 = Field1
WHERE Field1 > Field2
UPDATE OddTable
SET Field2 = Field3, Field3 = Field2
WHERE Field2 > Field3
UPDATE OddTable
SET Field1 = Field2, Field2 = Field1
WHERE Field1 > Field2

and then just
SELECT DISTINCT Field1, Field2, Field3, Field3
FROM OddTable

In either case it might be better to tell us what are you actually trying to acomplist and we might suggest a better table structure and then easier and quicker code.

HTH, Jenda
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-12 : 15:24:41
See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=68664



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-12 : 16:24:05
Here is a SQL Server 2005 approach
-- prepare sample data
declare @tbl table (col1 int, col2 int, col3 int, col4 int)

insert @tbl
select 10, 20, 30, 40 union all
select 20, 10, 40, 30 union all
select 20, 10, 40, 30 union all
select 50, 10, 30, 20

-- show the expected output
SELECT DISTINCT p.[1] AS col1,
p.[2] AS col2,
p.[3] AS col3,
p.[4] AS col4
FROM (
SELECT d.RowID, -- Not used in outer query but vital for correct result
ROW_NUMBER() OVER (PARTITION BY d.RowID ORDER BY d.x) AS ColID,
d.x
FROM (
SELECT DISTINCT RANK() OVER (ORDER BY t.col1, t.col2, t.col3, t.col4) AS RowID,
c.x
FROM @tbl AS t
CROSS APPLY (
SELECT t.col1 AS x UNION ALL
SELECT t.col2 UNION ALL
SELECT t.col3 UNION ALL
SELECT t.col4
) AS c
) AS d
) AS y
PIVOT (
MIN(y.x) FOR y.ColID IN ([1], [2], [3], [4]) -- MAX, AVG or SUM are equally good for this solution
) AS p

E 12°55'05.25"
N 56°04'39.16"


EDIT: Colored table prefix for those not familiar with PIVOT operator
Go to Top of Page
   

- Advertisement -