0

我有三个表,每个表都有一个Altid- 我想匹配每个表Altid并返回每个ID不匹配的表

例子:

table a    Altindx                      600650                3970034000
                                        602469                4300221000
                                        611183                5012600000
                                        612415                2002501930

table b    Datindx                      600650                3970034001
                                        602469                4300221000
                                        611183                5012600000
                                        612415                2002501931

table c    Mtindx                       600650                3970034002
                                        602469                4300221001
                                        611183                5012600000
                                        612415                2002501930

altid 排序的数据

a       600650                3970034000
b       600650                3970034001
c       600650                3970034002

a       602469                4300221000    
b       602469                4300221000
c       602469                4300221001

a       611183                5012600000
b       611183                5012600000
c       611183                5012600000

a       612415                2002501930
b       612415                2002501931
c       612415                2002501930

报告应如下所示:

600650                   3970034000
                         3970034001
                         3970034002

602469                   4300221000 
                         4300221001

612415                   2002501930
                         2002501931

611183- 不应出现在报告中,因为所有IDs 都匹配

4

3 回答 3

0
SELECT DISTINCT ID, Value FROM 
(
  SELECT A as T, ID, Value
  FROM altindex
  UNION ALL
  SELECT B as T, ID, Value 
  FROM latindex
  UNION ALL
  SELECT C as T, ID, Value 
  FROM mtindex
) sublist
WHERE
  COUNT(T) OVER (PARTITION BY ID, VALUE) > 1
于 2013-10-01T20:15:57.230 回答
0

你有两个字段,如果假设一个是id,两个是value,SQL 查询是:

SELECT * FROM(
    SELECT id, value FROM altindex
    UNION
    SELECT id, value FROM latindex
    UNION
    SELECT id, value FROM mtindex
) index
ORDER BY index.id ASC, index.value ASC
于 2013-10-01T19:19:56.673 回答
0

这会将所有表联合在一起,然后计算每个 AltID、ID 对出现的次数。结果中排除了出现 3 次的对,因为这意味着该对存在于所有三个表中。

select AltID, ID
from (
    select AltID, ID from Altindx
    union all
    select AltID, ID from Datindx
    union all
    select AltID, ID from Mtindx
) x 
group by AltID, ID
having COUNT('') < 3
order by AltID, ID
于 2013-10-01T19:42:42.877 回答