我想使用Union将计划和实际数据合并到1个视图中。 为此我使用两个内连接来使行匹配 - 然后我使用union语句。目前我分3步完成 - 现在我想将所有这些合并为1步。
实施例
创建vPlan2
Select
a.step,
a.[planned user],
b.[actual user],
a.[invoice plan] as 'invoice'
from vPlan a
inner join vActuals b on a.step = b.step
group by
a.step,
a.[planned user],
b.[actual user],
a.[invoice plan]
创建vActuals2
Select
a.step,
b.[planned user],
a.[actual user],
a.[invoice actuals] as 'invoice'
from vActuals a
inner join vPlan b on a.step = b.step
group by
a.step,
a.[actual user],
b.[planned user],
a.[invoice actuals]
制作vplanandactuals
select
'Plan' as type,
a.step,
a.[planned user],
a.invoice,
a.[actual user]
from vPlan2 a
group by
a.step,
a.[planned user],
a.invoice,
a.[actual user]
Union
select
'Actuals',
b.step,
b.[planned user],
b.invoice,
b.[actual user]
from vActuals2 b
group by
b.step,
b.[planned user],
b.invoice,
b.[actual user]
答案 0 :(得分:2)
与@ texastonie的答案类似,但将group by
替换为select distinct
:
Select Distinct
'Plan' as type,
vPlan.step,
vPlan.[planned user],
vPlan.[invoice plan] as 'invoice',
vActuals.[actual user]
from vPlan
inner join vActuals
on vPlan.step = vActuals.step
union
Select Distinct
'Actuals' as type,
vActuals.step,
vPlan.[planned user],
vActuals.[invoice actuals] as 'invoice',
vActuals.[actual user]
from vActuals
inner join vPlan
on vActuals.step = vPlan.step
;
答案 1 :(得分:1)
我认为你在寻找的是:
Select
'Plan' as Type,
a.step,
a.[planned user],
b.[actual user],
a.[invoice plan] as 'invoice'
from vPlan a
inner join vActuals b on a.step = b.step
group by
a.step,
a.[planned user],
b.[actual user],
a.[invoice plan]
union all
Select
'Actuals' as Type,
a.step,
b.[planned user],
a.[actual user],
a.[invoice actuals] as 'invoice'
from vActuals a
inner join vPlan b on a.step = b.step
group by
a.step,
a.[actual user],
b.[planned user],
a.[invoice actuals]