0

在我的表中,我有一些与另一个匹配的记录:

644432 738987
738987 644432
..
854313 871860
854313 874411
871860 854313
871860 874411
874411 854313
874411 871860

例如644432匹配738987738987匹配644432(显然)。对我来说,它们必须是相同的,我必须得到一个且只有一个(644432 或 738987 等等)。

另一个例子854313871860which 匹配874411(这就是我有 6 条记录的原因)。

我必须在决赛中只获得两条记录,我该怎么做?

对不起我的英语,如果我的问题不清楚,请告诉我。

例如,有一个代码来填充表格,例如:

DECLARE @DataTable TABLE (ColA  INT, ColB  INT)
insert into @DataTable  values 
(644432,    738987),
(738987,    644432),
(854313,    871860),
(854313,    874411),
(871860,    854313),
(871860,    874411),
(874411,    854313),
(874411,    871860)
select * from @DataTable
4

5 回答 5

1

假设这是一个名为 DataTable 的表,它有两列 ColA 和 ColB,那么你可以这样做:

select distinct Smallest,Largest from
(
  select case when ColA > ColB then ColB else ColA end as Smallest,
  case when ColA > ColB then ColA else colB end as Largest
  from DataTable
) minmax

这使用内部选择重新排列值,以便最小值始终位于第一列,最大值位于第二列。然后外部选择只是拉出不同的值集。

于 2013-03-20T11:57:00.443 回答
0

试试这个

select col1,col2 from (Select col1+col2 as indicator, col1, col2 from table1) 按指标分组

在此处参考 sqlfiddle

注意:如果两个不同行的总和相同,这将不起作用。

于 2013-03-20T12:14:16.290 回答
0
select n1,n2 from(select a.col1 col1,a.col2 col2,rownum rn from tbl a, tbl b 
where a.col1||a.col2=(b.col2||b.col1)) where mod(rn,2)<>0
union
select a.col1 col1,a.col2 col2 from tbl a left outer join tbl b on 
a.col1||a.col2=(b.col2||b.col1) where b.col1 is null
于 2013-03-20T12:40:26.537 回答
0

用于查找连接集的递归查询。对于每个链,编号最小的项目被报告为“组长”。

该查询首先对对的成员进行排序,然后找到连接组件的链。如果集群有多个起点,此方法将不起作用。(但它确实避免了循环)

此语法适用于 Postgresql,对于 microsoft,您应该省略RECURSIVE关键字,对于 Oracle,您应该使用CONNECT BY, PRIOR. YMMV。

DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;

CREATE TABLE pairs (ONE INTEGER NOT NULL, two INTEGER NOT NULL
        , PRIMARY KEY (one, two)
        );
INSERT INTO pairs(one, two) values
(644432,738987) ,(738987,644432)
,(854313,871860) ,(854313,874411) ,(871860,854313) ,(871860,874411) ,(874411,854313) ,(874411,871860)
        ;

WITH RECURSIVE rope AS (
        WITH opair AS (
                SELECT LEAST(one, two) AS one
                , GREATEST(one, two) AS two
                FROM pairs
                )
        SELECT o.one AS top
        , o.one AS one
        , o.two AS two
        FROM opair o
        WHERE NOT EXISTS ( SELECT * FROM opair x WHERE x.two=o.one)
        UNION ALL
        SELECT k.one AS top
        , p.one AS one
        , p.two AS two
        FROM opair p
        JOIN rope k ON k.two = p.one
        )
SELECT DISTINCT top
        , COUNT(*) AS N_members
FROM rope
GROUP BY top
ORDER BY top
        ;

结果:

CREATE TABLE
INSERT 0 8
  top   | n_members 
--------+-----------
 644432 |         2
 854313 |         8
(2 rows)
于 2013-03-20T13:05:12.373 回答
0

好的,下面的示例将跟随一级深度的链接。这可以通过存储过程或从代码创建查询来大量清理,这将更容易添加额外级别的链接跟踪。

-- Set up an example table
create table DataTable
(
    A int,
    B int
)
GO

insert into DataTable values(644432,738987)
insert into DataTable values(738987,644432)
insert into DataTable values(854313,871860)
insert into DataTable values(854313,874411)
insert into DataTable values(871860,854313)
insert into DataTable values(871860,874411)
insert into DataTable values(874411,854313)
insert into DataTable values(874411,871860)
GO

-- Strip out initial duplicates
select distinct A,B into Pass1
from
(
  select case when A > B then B else A end as A,
  case when A > B then A else B end as B
  from DataTable
) minmax

-- Create a copy that we will update with links between values
select * into Pass2 from Pass1 order by A

update Pass2 set B=x.NewB from
(
  select L.A as OldA,L.B as OldB, R.B as NewB
  from Pass1 L
  inner join Pass1 R on L.B = R.A
) x
where Pass2.A=x.OldA and Pass2.B=x.OldB

update Pass2 set A=x.NewA from
(
  select L.B as OldA, R.B as OldB, L.A as NewA
  from Pass1 L
  inner join Pass1 R on L.B = R.A
) x
where Pass2.A=x.OldA and Pass2.B=x.OldB

-- Dedupe any newly created duplicates
select distinct A,B
from
(
  select case when A > B then B else A end as A,
  case when A > B then A else B end as B
  from Pass2
) minmax
于 2013-03-20T13:48:28.947 回答