|
Thanks Joep,cteAmount.ntrnpd
I tried running that but it returns error.
With cteAmount (ntrnpd, amount)
as (select ntrnpd, sum(ntramt) from dktntrpf where ntrtty = 'UDD'
group by ntrnpd, ntrtty),
cteAdjust (ntrnpd, adjust))
as (select ntrnpd, sum(ntramt) from dktntrpf where ntrtty = 'ADJ'
group by ntrnpd, ntrtty)
Select npdsdt, npdedt, npdamt, npdsts, npdtno, npdabl, npdnph, npdnpd,
nphnab,
npdamt + ifnull(cteAmount.adjust, 0) -
ifnull(cteAmount.amount, 0)
from dktnphpf
left outer join cteAmount on dktnphpf.ntrnpd =
left outer join cteAdjust on dktnphpf.ntrnpd =cteAdjust.ntrnpd
where nphcoy = 1 and nphnab = 'ND0001' and nphnph = npdnph;https://urldefense.proofpoint.com/v2/url?u=http-3A__joep.beckeringh-40upcmail.nl&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=83FAoPreAUMYDPyUSsdcb9-Sth6dIjUdAXu4HmKyHUI&s=kGr0HKWVKUK1YgDK6lcWU2tv2JWDkPr6PWmB4XMzYqY&e=
SQL State: 42601
Vendor Code: -104
Message: [SQL0104] Token ) was not valid. Valid tokens: AS.
Sorry but this does not look easier :-)
From: "Joep Beckeringh via MIDRANGE-L" <midrange-l@xxxxxxxxxxxxxxxxxx>
To: midrange-l@xxxxxxxxxxxxxxxxxx
Cc: "Joep Beckeringh" <
cteAmount.ntrnpd
Date: 28/02/2021 03:46 PM
Subject: Re: SQL - Using result fields in a calculation
Sent by: "MIDRANGE-L" <midrange-l-bounces@xxxxxxxxxxxxxxxxxx>
Oh, there is something missing in the second ifnull; should be
With cteAmount (ntrnpd, amount)
as (select ntrnpd, sum(ntramt) from dktntrpf where ntrtty = 'UDD'
group by ntrnpd, ntrtty),
cteAdjust (ntrnpd, adjust))
as (select ntrnpd, sum(ntramt) from dktntrpf where ntrtty = 'ADJ'
group by ntrnpd, ntrtty)
Select npdsdt, npdedt, npdamt, npdsts, npdtno, npdabl, npdnph, npdnpd,
nphnab,
npdamt + ifnull(cteAmount.adjust, 0) -
ifnull(cteAmount.amount, 0)
from dktnphpf
left outer join cteAmount on dktnphpf.ntrnpd =
left outer join cteAdjust on dktnphpf.ntrnpd =cteAdjust.ntrnpd
where nphcoy = 1 and nphnab = 'ND0001' and nphnph = npdnph;cteAmount.ntrnpd
Op 28-2-2021 om 06:43 schreef Joep Beckeringh via MIDRANGE-L:
Don and Alan,
The CTEs should be something like 'with <virtual_table> as
<select_statement>'. So try this:
With cteAmount (ntrnpd, amount)
as (select ntrnpd, sum(ntramt) from dktntrpf where ntrtty = 'UDD'
group by ntrnpd, ntrtty),
cteAdjust (ntrnpd, adjust))
as (select ntrnpd, sum(ntramt) from dktntrpf where ntrtty = 'ADJ'
group by ntrnpd, ntrtty)
Select npdsdt, npdedt, npdamt, npdsts, npdtno, npdabl, npdnph, npdnpd,
nphnab,
npdamt + cteAmount.adjust - cteAmount.amount
from dktnphpf
join cteAmount on dktnphpf.ntrnpd = cteAmount.ntrnpd
join cteAdjust on dktnphpf.ntrnpd = cteAdjust.ntrnpd
where nphcoy = 1 and nphnab = 'ND0001' and nphnph = npdnph;
You should probably be prepared for situations where a 'UDD' or 'ADJ'
record is not available:
With cteAmount (ntrnpd, amount)
as (select ntrnpd, sum(ntramt) from dktntrpf where ntrtty = 'UDD'
group by ntrnpd, ntrtty),
cteAdjust (ntrnpd, adjust))
as (select ntrnpd, sum(ntramt) from dktntrpf where ntrtty = 'ADJ'
group by ntrnpd, ntrtty)
Select npdsdt, npdedt, npdamt, npdsts, npdtno, npdabl, npdnph, npdnpd,
nphnab,
npdamt + ifnull(cteAmount.adjust, 0) -
ifnull(cteAmount.amount)
from dktnphpf
left outer join cteAmount on dktnphpf.ntrnpd =
cteAdjust.ntrnpdleft outer join cteAdjust on dktnphpf.ntrnpd =
npdnpd,where nphcoy = 1 and nphnab = 'ND0001' and nphnph = npdnph;'UDD';
Joep Beckeringh
Op 28-2-2021 om 05:36 schreef Don Brown via MIDRANGE-L:
Thank Alan but same error;
See my testing;
select sum(ntramt) from dktntrpf where ntrnpd = 3913 and ntrtty =
'ADJ';<=== This works and gives the result
select sum(ntramt) from dktntrpf where ntrnpd = 3913 and ntrtty =
<=== This works and gives the result
Select npdsdt, npdedt, npdamt, npdsts, npdtno, npdabl, npdnph,
npdnpd,andnphnab
-- Cast(npdamt + Adjust - amount As Decimal(11,2))
<=== I
commented the cast out and this works returning one row.
from dktnphpf, dktnpdpf
where nphcoy = 1 and nphnab = 'ND0001' and nphnph = npdnph;
With Amount as (select sum(ntramt) from dktntrpf where ntrnpd = 3913
ntrtty = 'UDD'), -- <=== But this fails with the error below.
Adjust as (select sum(ntramt) from dktntrpf where ntrnpd = 3913 and
ntrtty
= 'UDD')
Select npdsdt, npdedt, npdamt, npdsts, npdtno, npdabl, npdnph,
npdnpd,andnphnab,
Cast(npdamt + adjust - amount As Decimal(11,2))
from dktnphpf, dktnpdpf
where nphcoy = 1 and nphnab = 'ND0001' and nphnph = npdnph;
Error:
SQL State: 42703
Vendor Code: -206
Message: [SQL0206] Column or global variable ADJUST not found.
Which is the same error I had originally ?
From: "Alan Campin" <alan0307d@xxxxxxxxx>
To: "Midrange Systems Technical Discussion"
<midrange-l@xxxxxxxxxxxxxxxxxx>
Date: 28/02/2021 02:23 PM
Subject: Re: SQL - Using result fields in a calculation
Sent by: "MIDRANGE-L" <midrange-l-bounces@xxxxxxxxxxxxxxxxxx>
With Amount as (select sum(ntramt) from dktntrpf where ntrnpd = 3913
ntrtty =
'UDD'),
Adjust as (select sum(ntramt) from dktntrpf where ntrnpd = 3913 and
ntrtty
=
'UDD')
Select npdsdt, npdedt, npdamt, npdsts, npdtno, npdabl, npdnph,
ofnphnab,
Cast(npdamt + adjust - amount As Decimal(11,2))
from dktnphpf, dktnpdpf
where nphcoy = 1 and nphnab = 'ND0001' and nphnph = npdnph;
The cast is not really needed but I like to define the type and size
Iany
variable I define.
I would recommend for testing that you test in pieces.
First
With Amount as (...)
Select * from amount;
If that works add the next piece and see that works. If both work add
the
last piece,
That way you see if there is some other error in the code
Also I note you don't have a join clause. Deliberate?
On Sat, Feb 27, 2021, 6:50 PM Don Brown via MIDRANGE-L <
midrange-l@xxxxxxxxxxxxxxxxxx> wrote:
Thank you for your reply Alan.npdnpd,
Would you be able to share an example as i have not used CTE's
Thanks
From: "Alan Campin" <alan0307d@xxxxxxxxx>
To: "Midrange Systems Technical Discussion"
<midrange-l@xxxxxxxxxxxxxxxxxx>
Date: 28/02/2021 12:47 PM
Subject: Re: SQL - Using result fields in a calculation
Sent by: "MIDRANGE-L" <midrange-l-bounces@xxxxxxxxxxxxxxxxxx>
Why not use CTE's. Much simpler code.
On Sat, Feb 27, 2021, 6:33 PM Don Brown via MIDRANGE-L <
midrange-l@xxxxxxxxxxxxxxxxxx> wrote:
Hi,
I have the following SQL
select npdsdt, npdedt, npdamt, npdsts, npdtno, npdabl, npdnph,
ntrttynphnab,
(select sum(ntramt) from dktntrpf where ntrnpd = 3913 and
=ntrtty
'UDD') as amount,
(select sum(ntramt) from dktntrpf where ntrnpd = 3913 and
=
'ADJ') as adjust,
npdamt + adjust - amount as avail
from dktnphpf, dktnpdpf
where nphcoy = 1 and nphnab = 'ND0001' and nphnph = npdnph;
I am running it in iACS
It fails indicating the variable "adjust" is not found.
I am obviously not using the correct terms when searching google as
https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.midrange.com_mailman_listinfo_midrange-2Dl&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=Ujx-rkrGSO55h_jypQFfIiHaHA5RwcjZpDDGKfQVwCc&s=20L2tP8BR3TTjEzg0c10Ad7jQh76XEcpmWmubrBNofM&e=email,takencan't find an example of how to use the results or a sub select.
Any assistance would be appreciated.
Thanks
Don
--
This email has been scanned for computer viruses. Although MSD has
reasonable precautions to ensure no viruses are present in this
mailingMSDuse
cannot accept responsibility for any loss or damage arising from the
of
this email or attachments..
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L)
list
To post a message email: MIDRANGE-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:
https://urldefense.proofpoint.com/v2/url?u=https-3A__archive.midrange.com_midrange-2Dl&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=Ujx-rkrGSO55h_jypQFfIiHaHA5RwcjZpDDGKfQVwCc&s=BzwQ8Z6zZDW1xi6KgosG7vR8JYWfOC8x9M1FgI--nMQ&e=
or email: MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at
https://urldefense.proofpoint.com/v2/url?u=http-3A__midrange.com&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=Ujx-rkrGSO55h_jypQFfIiHaHA5RwcjZpDDGKfQVwCc&s=Mqos72GpQZwTS914dlmKpQuemp8aUx6AAI_uo8tCAgI&e=
.related
Please contact support@xxxxxxxxxxxxxxxxxxxx for any subscription
questions.
Help support
https://urldefense.proofpoint.com/v2/url?u=http-3A__amazon.com&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=Ujx-rkrGSO55h_jypQFfIiHaHA5RwcjZpDDGKfQVwCc&s=xQQhRc_hC0zHQcwu9-Yf7G-N9YQK00p02ySMwIttDWQ&e=
by shopping at
https://urldefense.proofpoint.com/v2/url?u=https-3A__amazon.midrange.com&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=Ujx-rkrGSO55h_jypQFfIiHaHA5RwcjZpDDGKfQVwCc&s=WG23P3IU834Gq8DyPUYVwnt-1cduIhrj4kSbr1oYO88&e=
with our affiliate
link:
mailing
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L)
https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.midrange.com_mailman_listinfo_midrange-2Dl&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=Ujx-rkrGSO55h_jypQFfIiHaHA5RwcjZpDDGKfQVwCc&s=20L2tP8BR3TTjEzg0c10Ad7jQh76XEcpmWmubrBNofM&e=list
To post a message email: MIDRANGE-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:
https://urldefense.proofpoint.com/v2/url?u=https-3A__archive.midrange.com_midrange-2Dl&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=Ujx-rkrGSO55h_jypQFfIiHaHA5RwcjZpDDGKfQVwCc&s=BzwQ8Z6zZDW1xi6KgosG7vR8JYWfOC8x9M1FgI--nMQ&e=
or email: MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at
https://urldefense.proofpoint.com/v2/url?u=http-3A__midrange.com&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=Ujx-rkrGSO55h_jypQFfIiHaHA5RwcjZpDDGKfQVwCc&s=Mqos72GpQZwTS914dlmKpQuemp8aUx6AAI_uo8tCAgI&e=
.
Please contact support@xxxxxxxxxxxxxxxxxxxx for any subscription
related
questions.
Help support
https://urldefense.proofpoint.com/v2/url?u=http-3A__amazon.com&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=Ujx-rkrGSO55h_jypQFfIiHaHA5RwcjZpDDGKfQVwCc&s=xQQhRc_hC0zHQcwu9-Yf7G-N9YQK00p02ySMwIttDWQ&e=
by shopping at
https://urldefense.proofpoint.com/v2/url?u=https-3A__amazon.midrange.com&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=Ujx-rkrGSO55h_jypQFfIiHaHA5RwcjZpDDGKfQVwCc&s=WG23P3IU834Gq8DyPUYVwnt-1cduIhrj4kSbr1oYO88&e=
with our affiliate link:
email,
--
This email has been scanned for computer viruses. Although MSD has
taken
reasonable precautions to ensure no viruses are present in this
mailingMSD
cannot accept responsibility for any loss or damage arising from theof
use
this email or attachments..
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L)
https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.midrange.com_mailman_listinfo_midrange-2Dl&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=eXIxBbr3VqJD3wCezEK_IfTQDOF3eRXaDnDWsBJLHnI&s=KvPxmIbyDLIvOeqwhQyme0J7algFXmWnw_-FLwin_Uw&e=list
To post a message email: MIDRANGE-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:
https://urldefense.proofpoint.com/v2/url?u=https-3A__archive.midrange.com_midrange-2Dl&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=eXIxBbr3VqJD3wCezEK_IfTQDOF3eRXaDnDWsBJLHnI&s=jHqnFTbGo8l_F3OmT1dUdRCehaBRsJtpVhqpl8DCdQc&e=
or email: MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at
https://urldefense.proofpoint.com/v2/url?u=http-3A__midrange.com&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=eXIxBbr3VqJD3wCezEK_IfTQDOF3eRXaDnDWsBJLHnI&s=GtT9cT344QFoTUiQEn1NHW5Pfea29C16ibaly0NopVE&e=
.
Please contact support@xxxxxxxxxxxxxxxxxxxx for any subscription
related
questions.
Help support
https://urldefense.proofpoint.com/v2/url?u=http-3A__amazon.com&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=eXIxBbr3VqJD3wCezEK_IfTQDOF3eRXaDnDWsBJLHnI&s=52Fc7QNlH2uEPsdBipqbXohpmL2BKImcyve9zShef54&e=
by shopping at
https://urldefense.proofpoint.com/v2/url?u=https-3A__amazon.midrange.com&d=DwICAg&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=Kys-lxRCMpPr7up01Pp1FRjOe49ne6imWwi1b-ue8yQ&m=eXIxBbr3VqJD3wCezEK_IfTQDOF3eRXaDnDWsBJLHnI&s=U5GboQ7eE7fQbfH6RJdz3v-e8GdsCTUS6wXY6KPzr5w&e=
with our affiliate
link:
--
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.