|
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 = cteAmount.ntrnpd
left outer join cteAdjust on dktnphpf.ntrnpd = cteAdjust.ntrnpd
where nphcoy = 1 and nphnab = 'ND0001' and nphnph = npdnph;
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 = 'UDD';
<=== This works and gives the result
select sum(ntramt) from dktntrpf where ntrnpd = 3913 and ntrtty = 'ADJ';
<=== This works and gives the result
Select npdsdt, npdedt, npdamt, npdsts, npdtno, npdabl, npdnph, npdnpd,
nphnab
-- 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 and
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,
nphnab,
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 and
ntrtty =
'UDD'),
Adjust as (select sum(ntramt) from dktntrpf where ntrnpd = 3913 and ntrtty
=
'UDD')
Select npdsdt, npdedt, npdamt, npdsts, npdtno, npdabl, npdnph, npdnpd,
nphnab,
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 of
any
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
=taken
'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 I
can'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
usereasonable precautions to ensure no viruses are present in this email,MSD
cannot accept responsibility for any loss or damage arising from the
ofhttps://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=
this email or attachments..list
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing
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
.related
Please contact support@xxxxxxxxxxxxxxxxxxxx for any subscription
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=questions.
Help support
by shopping athttps://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=
with our affiliatehttps://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=
link:
--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=
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing
list
To post a message email: MIDRANGE-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:
or email: MIDRANGE-L-request@xxxxxxxxxxxxxxxxxxhttps://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=
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
by shopping athttps://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=
with our affiliate link: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=
MSD
--
This email has been scanned for computer viruses. Although MSD has taken
reasonable precautions to ensure no viruses are present in this email,
cannot accept responsibility for any loss or damage arising from the useof
this email or attachments..list
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing
To post a message email: MIDRANGE-L@xxxxxxxxxxxxxxxxxxhttps://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=
To subscribe, unsubscribe, or change list options,
visit:
or email: MIDRANGE-L-request@xxxxxxxxxxxxxxxxxxhttps://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=
Before posting, please take a moment to review the archives
at
.
Please contact support@xxxxxxxxxxxxxxxxxxxx for any subscription relatedhttps://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=
questions.
Help support
by shopping at
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=
with our affiliate
link: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=
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.