ORACLE SQL:组合2行2个单独的选择查询

时间:2018-05-04 11:06:38

标签: sql oracle aggregate-functions union-all

您好我是SQL新手,我遇到了问题。我有3个工作表,收入和成本。 job.unid = cost.job_unid和revenue.job_unid

我想找到每项工作的总收入和成本,然后在一行中查找每项工作的利润:

标题看起来像这样: 工作号,总收入,总成本,P& L(Rev-Cost)

我尝试制作一个查询,但在互联网上搜索解决方案后,似乎我应该单独查询收入和成本,然后使用Union All进行组合。

现在我这样做:

  select m,
rlc,
rvatlc,
rtotlc,
clc,
cvatlc,
ctotlc
from(

(select decode(j.consolno,null,j.shpno,j.consolno) m,

sum(nvl(r.amtlc,0)) rlc,
sum(nvl(r.vatamtlc,0)) rvatlc,
sum(nvl(r.amtlc,0))+sum(nvl(r.vatamtlc,0)) rtotlc,
0 clc,
0 cvatlc,
0 ctotlc


from 
job j
full join revenue r on j.unid=r.job_unid
where j.voidby is  null and decode(j.consolno,null,j.shpno,j.consolno)='SHSEM00131'
group by decode(j.consolno,null,j.shpno,j.consolno)
)







union all




(select decode(j.consolno,null,j.shpno,j.consolno) m,


0 rlc,
0 rvatlc,
0 rtotlc,
sum(nvl(c.amtlc,0)) clc,
sum(nvl(c.vatamtlc,0)) cvatlc,
sum(nvl(c.amtlc,0))+sum(nvl(c.vatamtlc,0)) ctotlc


from 
job j, cost c 
where j.voidby is  null and j.unid=c.job_unid and decode(j.consolno,null,j.shpno,j.consolno)='SHSEM00131'
group by decode(j.consolno,null,j.shpno,j.consolno)
))

结果是: 我不确定你是否能看到,但它是2行: 1是收入,1是成本 我想结合这两行

query result jpg

如果有人能帮助我,我会非常感激!先谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个。

public class Circle extends Div
{
@Override
public IRenderer getRenderer()
{
    return new CircleRenderer(this);
}

private class CircleRenderer extends DivRenderer
{

    public CircleRenderer(final Circle circle)
    {
        super(circle);
        setPadding(PADDING);
    }


    @Override
    public void draw(final DrawContext drawContext)
    {
        final PdfCanvas canvas = drawContext.getCanvas();


        final Rectangle area = this.occupiedArea.getBBox();
        final float x = area.getX();
        final float y = area.getY();

        canvas.circle(x, y, 8);

        canvas.fillStroke();            

        super.draw(drawContext);
    }
}
}