Reputation: 1
If possible some help with the following question. I need to make a margin analyses. This I can do with one table and in the table both Cost as Turnover are mentioned. Via the ordernumber I would like to connect the cost and turnover of one ordernumber.
Via this query I get the Turnover(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
Via this query I get the cost(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
My wished result are the following columns; Ordernumber, Turnover, cost
Please give direction. At the moment I am lost.
Upvotes: 0
Views: 71
Reputation: 1
Thank you for helping. Suddenly I had a other thougt and used the code below.
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
Upvotes: 0
Reputation: 35333
There may be a way to combine the results and avoid the sub queries. I've not given it the needed thought. This is the simplest approach that is easy to understand; without rewriting both queries.
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
Upvotes: 0
Reputation: 22001
No need to use a join here, this should give you what you want:
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
Upvotes: 1