您可以根据 union 的结果使用带有 sum 和 group by 的选择
select FieldOne
, FieldTwo
, FieldThree
, sum(CountOne )
, sum(CountTwo )
, sum(CountThree )
from (
Select FieldOne
, FieldTwo
, FieldThree
, CountOne
, CountTwo
, CountThree
FROM my_view1
UNION ALL
Select FieldOne
, FieldTwo
, FieldThree
, CountOne
, CountTwo
, CountThree
FROM my_view2
) T
group by FieldOne
, FieldTwo
, FieldThree
order by FieldOne
如果你想合并不同的值,你可以使用 UNION ;如果你想合并联合选择的所有结果,你可以使用 UNION ALL 。
并对联合子选择中查询的使用进行“有点混乱”
select FieldOne
, FieldTwo
, FieldThree
, sum(CountOne )
, sum(CountTwo )
, sum(CountThree )
from (
Select FieldOne
, FieldTwo
, FieldThree
, CountOne
, CountTwo
, CountThree
FROM ( select col1 as FieldOne, col2 as FieldTwo .....
from ...
where
) TT_A
UNION ALL
Select FieldOne
, FieldTwo
, FieldThree
, CountOne
, CountTwo
, CountThree
FROM ( select col1 as FieldOne, col2 as FieldTwo .....
from ...
where
) TT_B
) T
group by FieldOne
, FieldTwo
, FieldThree
order by FieldOne