一个AltID的多个ID

时间:2013-10-01 19:04:44

标签: sql

我有三个表格,每个表格都有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 - 不应出现在报告中,因为所有ID匹配

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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