基于两个值的一个表的SQL SUM

时间:2015-07-21 12:33:18

标签: sql

如果可能,请帮助解决以下问题。我需要进行保证金分析。我可以用一张表和表格来提及成本作为营业额。通过订单号,我想连接一个订单号的成本和营业额。

通过此查询,我获得了营业额(omzet)

select  GBK.bkstnr_sub as Ordernummer,
        SUM(GBK.bdr_hfl*-1) as Omzet
from [040].dbo.gbkmut as GBK with (nolock)
where  (GBK.dagbknr = 50 or GBK.dagbknr = 40)and (GBK.reknr BETWEEN '     8000' AND '     8980') 
and GBK.bkstnr_sub in (Select ORK.ordernr from [040].dbo.orkrg as ORK with (nolock)  

where ORK.ord_soort = 'V' and ORK.status = 'A' and YEAR(ork.orddat)= '2014')

GROUP BY GBK.bkstnr_sub

通过此查询我得到了费用(kostprijs)

select  GBK.bkstnr_sub as Ordernummer,
        SUM(GBK.bdr_hfl*-1) as Omzet

from [040].dbo.gbkmut as GBK with (nolock)

where  (GBK.dagbknr = 50 or GBK.dagbknr = 40)and (GBK.reknr BETWEEN '     7000' AND '     8980') 
and GBK.bkstnr_sub in (Select ORK.ordernr from [040].dbo.orkrg as ORK with (nolock)  

where ORK.ord_soort = 'V' and ORK.status = 'A' and YEAR(ork.orddat)= '2014')

GROUP BY GBK.bkstnr_sub

我希望的结果如下: Ordernumber,营业额,成本

请指点。目前我迷路了。

3 个答案:

答案 0 :(得分:1)

这里不需要使用联接,这应该可以满足您的需求:

select  bkstnr_sub as Ordernummer,
        SUM(case when reknr BETWEEN '     8000' AND '     8980'  then GBK.bdr_hfl * -1 end) as Omzet,
        SUM(case when reknr BETWEEN '     7000' AND '     8980'  then GBK.bdr_hfl * -1 end) as kostprijs
from    [040].dbo.gbkmut
where   (dagbknr = 50 or dagbknr = 40) and 
        bkstnr_sub in (
            Select ordernr 
            from [040].dbo.orkrg
            where ord_soort = 'V' and 
                  status = 'A' and 
                  YEAR(orddat)= '2014')
GROUP BY bkstnr_sub

答案 1 :(得分:0)

可能有一种方法可以合并结果并避免子查询。我没有给它所需的思想。这是最容易理解的最简单的方法;不重写两个查询。

SELECT Turnover.OrderNummer, 
   TurnOver.omzet as TurnoverOmzet, 
   Cost.Omzet as CostOmzet
FROM 
    (SELECT GBK.bkstnr_sub as Ordernummer,
            SUM(GBK.bdr_hfl*-1) as Omzet
     FROM [040].dbo.gbkmut as GBK with (nolock)
     WHERE (GBK.dagbknr = 50 or GBK.dagbknr = 40)
       and (GBK.reknr BETWEEN '     8000' AND '     8980') 
       and GBK.bkstnr_sub in 
          (SELECT ORK.ordernr 
           FROM  [040].dbo.orkrg as ORK with (nolock)  
           WHERE ORK.ord_soort = 'V' 
             and ORK.status = 'A' 
             and YEAR(ork.orddat)= '2014')
     GROUP BY GBK.bkstnr_sub) as turnover
INNER JOIN 
    (SELECT GBK.bkstnr_sub as Ordernummer,
            SUM(GBK.bdr_hfl*-1) as Omzet
     FROM [040].dbo.gbkmut as GBK with (nolock)
     WHERE (GBK.dagbknr = 50 or GBK.dagbknr = 40)
       and (GBK.reknr BETWEEN '     7000' AND '     8980') 
       and GBK.bkstnr_sub in 
       (SELECT ORK.ordernr 
        FROM  [040].dbo.orkrg as ORK with (nolock)  
        WHERE ORK.ord_soort = 'V' 
          and ORK.status = 'A' 
          and YEAR(ork.orddat)= '2014')
     GROUP BY GBK.bkstnr_sub) as Cost
ON Turnover.orderNummer = Cost.orderNummer

答案 2 :(得分:0)

感谢您的帮助。突然间,我还有另一个人,并使用下面的代码。

select  GBK.bkstnr_sub as Ordernummer,
        SUM(GBK.bdr_hfl*-1) as Omzet,
        SUM(GBKK.bdr_hfl) as Kostprijs

from [040].dbo.gbkmut as GBK with (nolock)

 JOIN 

             (select    GBKK.bkstnr_sub,    
                    GBKK.bdr_hfl

            from [040].dbo.gbkmut as GBKK with (nolock)
            where  (GBKK.dagbknr = 50 or GBKK.dagbknr = 40)and GBKK.reknr BETWEEN '     7000' AND '     7980'
            and GBKK.bkstnr_sub in (Select ORK.ordernr from [040].dbo.orkrg as ORK with (nolock)  

            where ORK.ord_soort = 'V' and ORK.status = 'A' and YEAR(ork.orddat)= '2014')) as GBKK
on GBK.bkstnr_sub = GBKK.bkstnr_sub