FB-3 Update, Where In

117 views
Skip to first unread message

henry...@hotmail.com

unread,
May 14, 2025, 2:06:56 AM5/14/25
to firebird-support
Good Morning,

I want to update some of my ITEMS  table's Total_in (IN_Q) and Total_Out(OUT_Q) quantities

ITEMS Table to be updated 
ITEMNO   IN_Q   OUT_Q
AA
BB
CC
DD

Sum Quantities informations will be taken from below View
ITEMMOVEMENTS 
ITEMNO.....IN_Q....OUT_Q
AA                10         0
BB                 20        0
AA                   0        5
CC                   2        0
....

ItemNo information will be taken from below PLIST table
PLIST
ITEMNO
AA          
BB          

That is, i want to Update Just  AA and BB ItemNos in the Items Table

I wrote below SQL but I was not successful
UPDATE ITEMS IT
SET IT.IN_Q = (SELECT SUM(IM.IN_Q)
              FROM ITEMMOVEMENTS IM
              WHERE IM.ITEMNO IN (SELECT ITEMNO FROM PLIST WHERE ID = 2))
WHERE IT.ITEMNO IN (SELECT ITEMNO FROM PLIST WHERE ID = 2),

IT.OUT_Q = (SELECT SUM(IM.OUT_Q)
               FROM ITEMMOVEMENTS IM
               WHERE IM.ITEMNO IN (SELECT ITEMNO FROM PLIST WHERE RNO = 2))
WHERE IT.ITEMNO IN (SELECT ITEMNO FROM PLIST WHERE RNO = 2)

Could someone please help

Thank You

Péter Ádám

unread,
May 15, 2025, 2:58:37 AM5/15/25
to firebird-support
Hello!

" i want to Update Just  AA and BB ItemNos "

yet your query looks for an ID and for a RNO which both are 2 - what is RNO? Is it different from ID?

If PLIST contains only the ItemNos to update then why filter them with WHERE clause from PLIST?

Also is the ITEMMOVEMENTS  view based on the ITEMS table?

henry...@hotmail.com

unread,
May 15, 2025, 11:25:48 AM5/15/25
to firebird-support
Thank you so much Lape
I'm so sorry i mistakely wrote RNO it is actually ID (RNO=ID)
Items Table is like PARTCARD, there  i keep parts informations (PartNo, PartName, Incomeqty  OutGoQty, Stock..)
ItemMovements is a View and collects all Parts Movements (from Dispatch, Invoice etc..)

Svein Erling Tysvær

unread,
May 16, 2025, 5:38:58 PM5/16/25
to firebird...@googlegroups.com
Hi Henry!

One of the first things I learnt about Firebird (well, it might have been InterBase) was to never use IN (<subselect>) since it was exceedingly slow (it may be better in newer versions of Firebird). Luckily, replacing IN (<subselect>) with EXISTS(<subselect>) is trivial and I've never even wanted to use IN (<subselect>) the last 26 or 27 years and only use IN with constants.

In your case, using MERGE may be a better alternative, e.g. (completely untested)

MERGE INTO ITEMS I
USING (SELECT P.ITEMNO, SUM(IM.IN_Q) IN_Q_SUM, SUM(IM.OUT_Q) OUT_Q_SUM
       FROM PLIST P
       JOIN ITEMMOVEMENTS IM ON P.ITEMNO = IM.ITEMNO
       WHERE P.ID = 2) TMP ON I.ITEMNO = TMP.ITEMNO
WHEN MATCHED THEN UPDATE SET I.IN_Q = TMP.IN_Q_SUM, I.OUT_Q = TMP.OUT_Q_SUM

You never mentioned it, but you can also use MERGE for inserting in case there should happen to be ITEMNOs in PLIST and ITEMNOVEMENTS that does not yet exist in ITEMS.

Hopefully this can get you started,
Set

--
Support the ongoing development of Firebird! Consider donating to the Firebird Foundation and help ensure its future. Every contribution makes a difference. Learn more and donate here:
https://www.firebirdsql.org/donate
---
You received this message because you are subscribed to the Google Groups "firebird-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebird-suppo...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/firebird-support/493183de-d5b0-4f29-92f1-189a55dedddbn%40googlegroups.com.

henry...@hotmail.com

unread,
May 17, 2025, 2:14:11 AM5/17/25
to firebird-support
Thank you SO MUCH Set
Reply all
Reply to author
Forward
0 new messages