on 2024 Jul 31 6:32 PM
Dear all,
Hope you can help me with the following query:
Hi,
Try this
DECLARE @YEAR AS INT = 2024
select T2.TrnspName, T1.* from OCRD T0 right join
(SELECT *
,Alles.Januari +
Alles.Februari +
Alles.Maart +
Alles.April +
Alles.Mei +
Alles.Juni +
Alles.Juli +
Alles.Augustus +
Alles.September +
Alles.Oktober +
Alles.November +
Alles.December AS [Totaal]
fROM (
SELECT c.CardCode
,c.CardName
,(Select Cast(Isnull(Sum(x.sales), 0) As Money) From
(select (sales.DocTotal - sales.VatSum) as [sales]
from OINV sales
where month(sales.DocDate) = 1
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode
union all
select 0 - (sales.DocTotal - sales.VatSum)
from ORIN sales
where month(sales.DocDate) = 1
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode) x) AS [Januari]
,(Select Cast(Isnull(Sum(x.sales), 0) As Money) From
(select (sales.DocTotal - sales.VatSum) as [sales]
from OINV sales
where month(sales.DocDate) = 2
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode
union all
select 0 - (sales.DocTotal - sales.VatSum)
from ORIN sales
where month(sales.DocDate) = 2
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode) x) AS [Februari]
,(Select Cast(Isnull(Sum(x.sales), 0) As Money) From
(select (sales.DocTotal - sales.VatSum) as [sales]
from OINV sales
where month(sales.DocDate) = 3
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode
union all
select 0 - (sales.DocTotal - sales.VatSum)
from ORIN sales
where month(sales.DocDate) = 3
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode) x) AS [Maart]
,(Select Cast(Isnull(Sum(x.sales), 0) As Money) From
(select (sales.DocTotal - sales.VatSum) as [sales]
from OINV sales
where month(sales.DocDate) = 4
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode
union all
select 0 - (sales.DocTotal - sales.VatSum)
from ORIN sales
where month(sales.DocDate) = 4
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode) x) AS [April]
,(Select Cast(Isnull(Sum(x.sales), 0) As Money) From
(select (sales.DocTotal - sales.VatSum) as [sales]
from OINV sales
where month(sales.DocDate) = 5
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode
union all
select 0 - (sales.DocTotal - sales.VatSum)
from ORIN sales
where month(sales.DocDate) = 5
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode) x) AS [Mei]
,(Select Cast(Isnull(Sum(x.sales), 0) As Money) From
(select (sales.DocTotal - sales.VatSum) as [sales]
from OINV sales
where month(sales.DocDate) = 6
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode
union all
select 0 - (sales.DocTotal - sales.VatSum)
from ORIN sales
where month(sales.DocDate) = 6
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode) x) AS [Juni]
,(Select Cast(Isnull(Sum(x.sales), 0) As Money) From
(select (sales.DocTotal - sales.VatSum) as [sales]
from OINV sales
where month(sales.DocDate) = 7
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode
union all
select 0 - (sales.DocTotal - sales.VatSum)
from ORIN sales
where month(sales.DocDate) = 7
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode) x) AS [Juli]
,(Select Cast(Isnull(Sum(x.sales), 0) As Money) From
(select (sales.DocTotal - sales.VatSum) as [sales]
from OINV sales
where month(sales.DocDate) = 8
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode
union all
select 0 - (sales.DocTotal - sales.VatSum)
from ORIN sales
where month(sales.DocDate) = 8
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode) x) AS [Augustus]
,(Select Cast(Isnull(Sum(x.sales), 0) As Money) From
(select (sales.DocTotal - sales.VatSum) as [sales]
from OINV sales
where month(sales.DocDate) = 9
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode
union all
select 0 - (sales.DocTotal - sales.VatSum)
from ORIN sales
where month(sales.DocDate) = 9
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode) x) AS [September]
,(Select Cast(Isnull(Sum(x.sales), 0) As Money) From
(select (sales.DocTotal - sales.VatSum) as [sales]
from OINV sales
where month(sales.DocDate) = 10
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode
union all
select 0 - (sales.DocTotal - sales.VatSum)
from ORIN sales
where month(sales.DocDate) = 10
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode) x) AS [Oktober]
,(Select Cast(Isnull(Sum(x.sales), 0) As Money) From
(select (sales.DocTotal - sales.VatSum) as [sales]
from OINV sales
where month(sales.DocDate) = 11
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode
union all
select 0 - (sales.DocTotal - sales.VatSum)
from ORIN sales
where month(sales.DocDate) = 11
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode) x) AS [November]
,(Select Cast(Isnull(Sum(x.sales), 0) As Money) From
(select (sales.DocTotal - sales.VatSum) as [sales]
from OINV sales
where month(sales.DocDate) = 12
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode
union all
select 0 - (sales.DocTotal - sales.VatSum)
from ORIN sales
where month(sales.DocDate) = 12
and year(sales.DocDate) = @YEAR
and sales.CardCode = c.CardCode) x) AS [December]
FROM OCRD c
WHERE c.CardType = 'C'
AND ISNULL(c.frozenFor, 'N') = 'N') [Alles]
wHERE Alles.Januari +
Alles.Februari +
Alles.Maart +
Alles.April +
Alles.Mei +
Alles.Juni +
Alles.Juli +
Alles.Augustus +
Alles.September +
Alles.Oktober +
Alles.November +
Alles.December <> 0) T1 on T0.CardCode=T1.CardCode
left join OSHP T2 on T0.ShipType=T2.TrnspCode
Hope this helps,
Son Tran
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
66 | |
11 | |
10 | |
10 | |
9 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.