在DB2中,我有2个正在使用WITH
进行子选择的工作查询。
它们各自独立工作,但我似乎找不到找到正确组合或结合它们的最佳方法。
第一个查询:
with c as(
select
employee,
sum(salesPrice) as priorSpecifics
from schema1.orders g
inner join dateSchema.dates d
on g.dateField = d.dateField
where d.newDate between '2018-01-01' and '2018-01-31'
and g.details = 'CategoryOne'
group by employee
), d as(
select
employee,
sum(salesPrice) as priorTotals
from schema1.orders g
inner join dateSchema.dates d
on g.dateField = d.dateField
where d.newDate between '2018-01-01' and '2018-01-31'
group by employee
)
Select ifnull(c.employee,d.employee) as employee
,c.priorSpecifics
,d.priorTotals
,cast(Round((DEC(c.priorSpecifics,12,2)/DEC(d.priorTotals,12,2)) * 100,2) as decimal(12,2)) as priorPercent
from c full join d ON (c.employee = d.employee);
返回4列
和第二个查询
with c as(
select
employee,
sum(salesPrice) as currentSpecifics
from schema1.orders g
inner join dateSchema.dates d
on g.dateField = d.dateField
where d.newDate between '2019-01-01' and '2019-01-31'
and g.details = 'CategoryOne'
group by employee
), d as(
select
employee,
sum(salesPrice) as currentTotals
from schema1.orders g
inner join dateSchema.dates d
on g.dateField = d.dateField
where d.newDate between '2019-01-01' and '2019-01-31'
group by employee
)
Select ifnull(c.employee,d.employee) as employee
,c.currentSpecifics
,d.currentTotals
,cast(Round((DEC(c.currentSpecifics,12,2)/DEC(d.currentTotals,12,2)) * 100,2) as decimal(12,2)) as currentPercent
from c full join d ON (c.employee = d.employee);
也返回4。 employee
字段是所有人之间唯一共享的东西。
如何将其组合到一个查询中以获取一个雇员列,然后跟随我的所有总和/百分比列?
答案 0 :(得分:1)
我认为您可以通过条件聚合来完成您想做的所有事情:
select employee,
sum(case when d.newDate between '2018-01-01' and '2018-01-31' and g.details = 'CategoryOne' then salesPrice end) as priorSpecifics,
sum(case when d.newDate between '2019-01-01' and '2019-01-31' and g.details = 'CategoryOne' then salesPrice end) as currentSpecifics,
sum(case when d.newDate between '2018-01-01' and '2018-01-31' then salesPrice end) as priorTotals,
sum(case when d.newDate between '2019-01-01' and '2019-01-31' then salesPrice end) as currentTotals
from schema1.orders g inner join
dateSchema.dates d
on g.dateField = d.dateField
group by employee;
我将让您填写其他计算结果。
答案 1 :(得分:1)
如果以上所有查询均有效,则您已经拥有所需的一切。
您必须从第一个组,前两个组中合并才能进行prior
查询,然后
来自第二组以进行cur
查询
最后将prior
查询加入到cur
查询中:
with
c as(
select
employee,
sum(salesPrice) as priorSpecifics
from schema1.orders g
inner join dateSchema.dates d
on g.dateField = d.dateField
where d.newDate between '2018-01-01' and '2018-01-31'
and g.details = 'CategoryOne'
group by employee
),
d as(
select
employee,
sum(salesPrice) as priorTotals
from schema1.orders g
inner join dateSchema.dates d
on g.dateField = d.dateField
where d.newDate between '2018-01-01' and '2018-01-31'
group by employee
),
prior as (
Select ifnull(c.employee,d.employee) as employee
,c.priorSpecifics
,d.priorTotals
,cast(Round((DEC(c.priorSpecifics,12,2)/DEC(d.priorTotals,12,2)) * 100,2) as decimal(12,2)) as priorPercent
from c full join d ON (c.employee = d.employee)
),
e as(
select
employee,
sum(salesPrice) as currentSpecifics
from schema1.orders g
inner join dateSchema.dates d
on g.dateField = d.dateField
where d.newDate between '2019-01-01' and '2019-01-31'
and g.details = 'CategoryOne'
group by employee
),
f as(
select
employee,
sum(salesPrice) as currentTotals
from schema1.orders g
inner join dateSchema.dates d
on g.dateField = d.dateField
where d.newDate between '2019-01-01' and '2019-01-31'
group by employee
),
cur as (
Select ifnull(e.employee,f.employee) as employee
,e.currentSpecifics
,f.currentTotals
,cast(Round((DEC(e.currentSpecifics,12,2)/DEC(f.currentTotals,12,2)) * 100,2) as decimal(12,2)) as currentPercent
from e full join f ON (e.employee = f.employee)
)
Select ifnull(p.employee, c.employee) as employee
,p.priorSpecifics
,p.priorTotals
,p.priorPercent
,c.currentSpecifics
,c.currentTotals
,c.currentPercent
from prior p full join cur c ON (c.employee = c.employee);