0

Have a table, and need to output only certain rows. Here is the example:

Table:

ID  Pacct   Sacct
----------------------
10  12  12
9   13  12
12  14  12
4   15  16
5   16  15
6   17  18
7   19  18

Need to select rows where Pacct <> Sacct whenever there also exists rows where Pacct = Sacct

So, for the table above, the output I am looking for will be:

ID  PAcct   Sacct
--------------------------
9   13  12
12  14  12

Any ideas on how to accomplish? Thanks a lot!

4

5 回答 5

1

似乎您想要成对的pacctsacct它们在哪里不同,在哪里sacct不在paccts两者相同的列表中:

select *
from t
where pacct <> s.acct and
      s.acct in (select t2.pacct from t t2 and t2.sacct = t2.pacct)
于 2013-05-29T17:54:41.207 回答
0

认为您正在寻找类似的东西。但我不确定。

select *
from Table1 
inner join 
  (select D, Pacct, Sacct
   from Table1
   where Pacct = Sacct ) p_eq_s
on Table1.Pacct = p_eq_s.Sacct
or Table1.Pacct = p_eq_s.Pacct
or Table1.Sacct = p_eq_s.Sacct
or Table1.Sacct = p_eq_s.Pacct
where Table1.Pacct <> Table1.Sacct
于 2013-05-29T17:54:04.650 回答
0
SELECT t1.ID, t1.PAcct, t1.Sacct
FROM MyTable t1
INNER JOIN MyTable t2
ON t2.ID <> t1.ID AND t2.PAcct = t1.PAcct
WHERE t1.PAcct <> t1.Sacct
AND t2.PAcct = t2.Sacct

请注意,这依赖于 PAcct 和 Sacct 不可为空(或有关更改空相等比较的默认 SQL Server 设置)。如果它们可以为空,则WHERE t1.PAcct <> t1.Sacct必须更改子句以解决此问题。

于 2013-05-29T17:54:16.833 回答
0

尝试这个

select * from table x1
where Pacct <> Sacct 
and exists (select 1 from table x2 
             where x2.Pacct = x2.Sacct and 
                  (x1.Pacct = x2.Pacct or 
                   x1.Sacct = x2.Sacct))        

SQL 演示

于 2013-05-29T17:56:54.173 回答
-1

你应该尝试类似:

SELECT * FROM dump WHERE (PAcct - Sacct = 0);
于 2013-05-29T18:05:11.237 回答