两个表的计数不同 - 使用外连接?

时间:2018-04-28 18:47:32

标签: sql postgresql join

我试图在Z_KSS和Z_KSS2上查找计数(不同的MEM_ID)。如果在Z_KSS和Z_KSS2中都出现MEM_ID,则只应计数一次。我是否使用外部加入?我是否需要指定On条件?

Select count (distinct MEM_ID), sum(Amount)
      , 'KSS' Company
      FROM    temp_09.Z_KSS
       FULL OUTER JOIN temp_09.Z_KSS2

1 个答案:

答案 0 :(得分:0)

我猜你想要union all

SELECT count(distinct MEM_ID), sum(Amount), 'KSS' Company
FROM  ((SELECT MEM_ID, Amount
        FROM temp_09.Z_KSS
       ) UNION ALL
       (SELECT DISTINCT MEM_ID, Amount
        FROM temp_09.Z_KSS2
       )
      ) t;