Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.
The following SQL statement gets me the record I'm interested in. I want to delete that record. How do I properly combine this SELECT statement with the DELETE statement? Does this become the WHERE clause after DELETE FROM MYLIB/MYFILE, or a subselect, or?
SELECT MIN( DATE('20' || trim(CHAR(YY)) || '-' || trim(CHAR(MM)) || '-' || trim(CHAR(DD)))) from MYLIB/MYFILE where PERIOD = '22'
Thanks.
It was WHERE EXISTS that I needed to tie them together.
This would work as well:
DELETE FROM MYLIB/MYFILE WHERE PERIOD = '22' AND DateField = ( SELECT MIN( DATE('20' || trim(CHAR(YY)) || '-' || trim(CHAR(MM)) || '-' || trim(CHAR(DD)))) FROM MYLIB/MYFILE WHERE PERIOD = '22' )
SteveCCNJ, what is DateField? I don't have a specific date in my program, I just want to delete the oldest record in the file (based on YY, MM, DD) that has PERIOD = 22.
Ah - Well then you would have to do your date construct in the place of DateField.
DELETE
FROM
MYLIB/MYFILE
WHERE
PERIOD =
'22'
AND (DATE('20' ||
trim(CHAR(YY)) || '-' ||
trim(CHAR(MM)) || '-' ||
trim(CHAR(DD)))) = (
SELECT
MIN
(
DATE
(
'20'
||
trim(
CHAR
(YY)) ||
'-'
||
trim(
CHAR
(MM)) ||
'-'
||
trim(
CHAR
(DD))))
FROM
MYLIB/MYFILE
WHERE
PERIOD =
'22'
)
Dale,
You may want to avoid your Cut/Paste option that seems to be inserting garbage into your Comments. Is it Rich Text or from RDi or what?
I fixed your first post for you.
I also noticed that the Syntax Highlighter is no longer rendering correctly. This seems like it was spontaneous, so it may be an external JS issue.
Sorry Bob, code still looks like RPG 6. Very modular I must say!
Bob: Is there another Dale? I had started a reply in this thread, but never actually posted it. But Steve's post is a mess - could be the rendering problem to which you refer.
Viking: I the date unique in your table? Is it really three separate fields, and how are they declared?
The individual date fields YY, MM, and DD are all numeric (2,0). The dates should be unique and a date shouldn't appear in more than one period. PERIOD is (for some reason) 2A.
Here's what I've finally ended up with:
Exec sql delete from MYFILE a where PERIOD = :inPeriod AND digits(YY) || digits(MM) || digits(DD) = (select min(digits(YY) || digits(MM) || digits(DD)) from MYFILE b where b.PERIOD = a.PERIOD);
Wow - let's try again, pulling the code right out of a text editor.
DELETE FROM MYLIB/MYFILE
WHERE PERIOD = '22'
AND DATE('20' ||
trim(CHAR(YY)) || '-' ||
trim(CHAR(MM)) || '-' ||
trim(CHAR(DD))) =
(SELECT MIN( DATE('20' ||
trim(CHAR(YY)) || '-' ||
trim(CHAR(MM)) || '-' ||
trim(CHAR(DD))))
FROM MYLIB/MYFILE
WHERE PERIOD = '22'
)
FYI: After initially posting this question, I have learned that DIGITS is better to use for this than TRIM(CHAR( because DIGITS doesn't remove leading zeros like CHAR does.