Hi
I 'm trying to update a header table field [ahseqn] with the maximum value of the detail field [aiseqn].
I have written the query using inner joins, but I'm struggling to convert it use 'where' statements so I can use the Update statement.
While i'm testing, I'm using the SELECT statement instead of an UPDATE statement.
I've managed to do this sort of thing in the past, but not using the HAVING clause.
the platform is DB2/400.
The 2 tables are MPAGRH(Agreement Header) The Keys are AHCONO, AHAGNB and AHSUNO
(Header company, agreement number, supplier number)
linked to MPAGRL(Agreement Line) The keys are AICONO, AIAGNB, AISUNO
(Line/Detail Company, Agreement Number, supplier number)
-- Find Agreements with the header seqn no Ahseqn, <> to the max line seqn AISEQN.
Select ahagnb, ahsuno, ahseqn, max(aiseqn)
From mpagrh t1 inner join mpagrl t2 on ahcono=aicono and ahagnb=aiagnb and ahsuno=aisuno
Group by ahagnb, ahsuno, ahseqn
Having ahseqn <> max(aiseqn)
Order by ahagnb, ahsuno, ahseqn
This is how far I have gotton to converting the query not to use Inner Joins so I can replace the Select statement with a Update statement...
Select ahagnb, ahsuno, ahseqn,
(
Select max(aiseqn)
From mpagrl t2
Where t1.ahcono=t2.aicono and t1.ahagnb=t2.aiagnb and t1.ahsuno=t2.aisuno
)
From mpagrh t1
Where exists (
Select 1
From mpagrh t3 inner join mpagrl t4 on t3.ahcono=t4.aicono and t3.ahagnb=t4.aiagnb and t3.ahsuno=t4.aisuno
Group by t3.ahcono, t3.ahagnb, t3.ahsuno, t3.ahseqn
Having t3.ahseqn <> max(t4.aiseqn) and t1.ahcono=t3.ahcono and t1.ahagnb=t3.ahagnb and t1.ahsuno=t3.ahsuno
)
Currently its repeating the key 'lots' of times.
Can someone point me in the right direction.
Thanks
Fred
I 'm trying to update a header table field [ahseqn] with the maximum value of the detail field [aiseqn].
I have written the query using inner joins, but I'm struggling to convert it use 'where' statements so I can use the Update statement.
While i'm testing, I'm using the SELECT statement instead of an UPDATE statement.
I've managed to do this sort of thing in the past, but not using the HAVING clause.
the platform is DB2/400.
The 2 tables are MPAGRH(Agreement Header) The Keys are AHCONO, AHAGNB and AHSUNO
(Header company, agreement number, supplier number)
linked to MPAGRL(Agreement Line) The keys are AICONO, AIAGNB, AISUNO
(Line/Detail Company, Agreement Number, supplier number)
-- Find Agreements with the header seqn no Ahseqn, <> to the max line seqn AISEQN.
Select ahagnb, ahsuno, ahseqn, max(aiseqn)
From mpagrh t1 inner join mpagrl t2 on ahcono=aicono and ahagnb=aiagnb and ahsuno=aisuno
Group by ahagnb, ahsuno, ahseqn
Having ahseqn <> max(aiseqn)
Order by ahagnb, ahsuno, ahseqn
This is how far I have gotton to converting the query not to use Inner Joins so I can replace the Select statement with a Update statement...
Select ahagnb, ahsuno, ahseqn,
(
Select max(aiseqn)
From mpagrl t2
Where t1.ahcono=t2.aicono and t1.ahagnb=t2.aiagnb and t1.ahsuno=t2.aisuno
)
From mpagrh t1
Where exists (
Select 1
From mpagrh t3 inner join mpagrl t4 on t3.ahcono=t4.aicono and t3.ahagnb=t4.aiagnb and t3.ahsuno=t4.aisuno
Group by t3.ahcono, t3.ahagnb, t3.ahsuno, t3.ahseqn
Having t3.ahseqn <> max(t4.aiseqn) and t1.ahcono=t3.ahcono and t1.ahagnb=t3.ahagnb and t1.ahsuno=t3.ahsuno
)
Currently its repeating the key 'lots' of times.
Can someone point me in the right direction.
Thanks
Fred