Re: [firebird-support] Query and problem with nulls
Hallo, Il 04/01/2018 21.57, setysvar setys...@gmail.com [firebird-support] ha scritto: I expect the following query to give you the result you want: ... Yes. I think I understand your query and I'll treasure it for the next time. Thanks so much. -- Luigi Siciliano --
Re: [firebird-support] Query and problem with nulls
>LEFT JOIN DOC_TESTA DT ON D.ID = DT.DEPOSITO_ID, DOC_CORPO DC Ouch, Luigi, I don't think I've ever seen anyone using an implicit join after an explicit left join like this before! I have no clue how Firebird handles this (and I'm uncertain if I would want to know it or not). Generally, you should always use explicit JOINs, implicit JOINs are historical remnants that you could have eliminated when InterBase (the ancestor of Firebird) started supporting the explicit JOINs of SQL-92 (don't know when InterBase started doing that, but I would assume it was between 1992 and 1999). Since you don't seem to have any problems with treating parameters as if they were variables, I expect the following query to give you the result you want: SELECT DT.DEPOSITO_ID, SUM(IIF(DT.DATA < :DaData, DC.CARICO - DC.SCARICO, 0)) RIPORTO, SUM(IIF(DT.DATA BETWEEN :DaData AND :AData, DC.CARICO, 0)) CARICO, SUM(IIF(DT.DATA BETWEEN :DaData AND :AData, DC.SCARICO, 0)) SCARICO, SUM(IIF(DT.DATA <= :AData, DC.CARICO - DC.SCARICO, 0)) ESISTENZA, SUM(IIF(DT.DATA BETWEEN :DaData AND :AData, DC.IMPEGNATI, 0)) IMPEGNATI, SUM(IIF(DT.DATA BETWEEN :DaData AND :AData, DC.ORDINATI, 0)) ORDINATI, FROM DOC_CORPO DC JOIN DOC_TESTA DT ON DC.DOC_TESTA_ID = DT.ID WHERE DC.ARTICOLO_ID = :ID GROUP BY DT.DEPOSITO_ID You have said that you want rows returned if there are older data, but not between :DaData and :AData in DOC_TESTA, you haven't specified whether you want any rows returned if there are only newer data than :AData in DOC_TESTA. My query will return a row (albeit with lots of 0.0...) even if you specify ancient values, e.g. 01/01/1935 in :DaData and 31/12/1935 in :AData (as long as there are any rows with the correct ARTICOLO_ID in the table). HTH, Set
Re: [firebird-support] Query and problem with nulls
not, I not solved ;( The query seem to works because return rows but the rows returned are wrongs. -- Luigi Siciliano --
Re: [firebird-support] Query and problem with nulls
Hallo, Il 04/01/2018 13.27, Rustam rusta...@ukr.net [firebird-support] ha scritto: Try use explicit JOIN here: ... FROM DOC_CORPO DC, DOC_TESTA DT Not works because in DT there is not rows between 01/01/2018 and 31/12/2018. But I solved: I have another table in database that contains foreign keys for dt.deposito_id. I modified the query like this, and now works ok: SELECT DEPOSITO_ID, RIPORTO, CARICO, SCARICO, RIPORTO + ESISTENZA AS ESISTENZA, IMPEGNATI, ORDINATI FROM ( SELECT D.ID AS DEPOSITO_ID, (SELECT SUM(DC2.CARICO - DC2.SCARICO) FROM DOC_CORPO DC2, DOC_TESTA DT2 WHERE DC2.DOC_TESTA_ID = DT2.ID AND DC2.ARTICOLO_ID = :ID AND DT2.DATA < :DaData ) AS RIPORTO, SUM(DC.CARICO) as Carico, SUM(DC.SCARICO) as Scarico, SUM(DC.CARICO - DC.SCARICO) as ESISTENZA, SUM(DC.IMPEGNATI) as Impegnati, SUM(DC.ORDINATI) as Ordinati FROM DEPOSITI D LEFT JOIN DOC_TESTA DT ON D.ID = DT.DEPOSITO_ID, DOC_CORPO DC WHERE DC.ARTICOLO_ID = :ID AND DT.DATA >= :DaData AND DT.DATA <= :AData GROUP BY D.ID ) Thanks. -- Luigi Siciliano --
Re: [firebird-support] Query and problem with nulls
04.01.2018 17:13, Luigi Siciliano luigi...@tiscalinet.it [firebird-support] wrote: > if I select between 01/01/2018 and 31/12/2018 I not have results! this > because there are no rows in DT between 01/01/2018 and 31/12/2018. That's right. No rows - no result. If you want this result, add into year 2018 a record with DEPOSITO_ID = 1. -- WBR, SD. ++ Visit http://www.firebirdsql.org and click the Documentation item on the main (top) menu. Try FAQ and other links from the left-side menu there. Also search the knowledgebases at http://www.ibphoenix.com/resources/documents/ ++ Yahoo Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/firebird-support/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/firebird-support/join (Yahoo! ID required) <*> To change settings via email: firebird-support-dig...@yahoogroups.com firebird-support-fullfeatu...@yahoogroups.com <*> To unsubscribe from this group, send an email to: firebird-support-unsubscr...@yahoogroups.com <*> Your use of Yahoo Groups is subject to: https://info.yahoo.com/legal/us/yahoo/utos/terms/
Re: [firebird-support] Query and problem with nulls
I expecting the result because in table Doc_testa DT I have rows between 01/01/2017 and 31/12/2017 and not have rows between 01/01/2018 and 31/12/2018. -- Luigi Siciliano -- ++ Visit http://www.firebirdsql.org and click the Documentation item on the main (top) menu. Try FAQ and other links from the left-side menu there. Also search the knowledgebases at http://www.ibphoenix.com/resources/documents/ ++ Yahoo Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/firebird-support/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/firebird-support/join (Yahoo! ID required) <*> To change settings via email: firebird-support-dig...@yahoogroups.com firebird-support-fullfeatu...@yahoogroups.com <*> To unsubscribe from this group, send an email to: firebird-support-unsubscr...@yahoogroups.com <*> Your use of Yahoo Groups is subject to: https://info.yahoo.com/legal/us/yahoo/utos/terms/
Re: [firebird-support] Query and problem with nulls
Hallo, Il 04/01/2018 16.58, Dimitry Sibiryakov s...@ibphoenix.com [firebird-support] ha scritto: > 04.01.2018 16:55, Luigi Siciliano luigi...@tiscalinet.it [firebird-support] > wrote: >> As you see: >> - if I select between 01/01/2018 and 31/12/2018 the are not rows in DT >> - if I select between 01/01/2017 and 31/12/2017 works fine because the >> are at least 1 row in DT > So, where is the problem? I can't see "all nulls". > The problem is: if I select between 01/01/2018 and 31/12/2018 I not have results! this because there are no rows in DT between 01/01/2018 and 31/12/2018. I, instead, expecting this result: DEPOSITO_ID RIPORTO CARICO SCAR ICO ESISTENZA IMPEGNATI ORDINATI === === === === === === === 1 1. 0.000 0.0 000 2.000 0. 0. Thanks. -- Luigi Siciliano -- ++ Visit http://www.firebirdsql.org and click the Documentation item on the main (top) menu. Try FAQ and other links from the left-side menu there. Also search the knowledgebases at http://www.ibphoenix.com/resources/documents/ ++ Yahoo Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/firebird-support/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/firebird-support/join (Yahoo! ID required) <*> To change settings via email: firebird-support-dig...@yahoogroups.com firebird-support-fullfeatu...@yahoogroups.com <*> To unsubscribe from this group, send an email to: firebird-support-unsubscr...@yahoogroups.com <*> Your use of Yahoo Groups is subject to: https://info.yahoo.com/legal/us/yahoo/utos/terms/
Re: [firebird-support] Query and problem with nulls
04.01.2018 16:55, Luigi Siciliano luigi...@tiscalinet.it [firebird-support] wrote: > As you see: > - if I select between 01/01/2018 and 31/12/2018 the are not rows in DT > - if I select between 01/01/2017 and 31/12/2017 works fine because the > are at least 1 row in DT So, where is the problem? I can't see "all nulls". -- WBR, SD. ++ Visit http://www.firebirdsql.org and click the Documentation item on the main (top) menu. Try FAQ and other links from the left-side menu there. Also search the knowledgebases at http://www.ibphoenix.com/resources/documents/ ++ Yahoo Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/firebird-support/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/firebird-support/join (Yahoo! ID required) <*> To change settings via email: firebird-support-dig...@yahoogroups.com firebird-support-fullfeatu...@yahoogroups.com <*> To unsubscribe from this group, send an email to: firebird-support-unsubscr...@yahoogroups.com <*> Your use of Yahoo Groups is subject to: https://info.yahoo.com/legal/us/yahoo/utos/terms/
Re: [firebird-support] Query and problem with nulls
Hallo, Il 04/01/2018 12.39, Dimitry Sibiryakov s...@ibphoenix.com [firebird-support] ha scritto: > Are you sure that your query really returns all nulls and this is not a > visual bug in > the tool you run this query in? Show log from isql, please. the log is: SQL> SELECT CON> DEPOSITO_ID, CON> RIPORTO, CON> CARICO, CON> SCARICO, CON> RIPORTO + ESISTENZA AS ESISTENZA, CON> IMPEGNATI, CON> ORDINATI CON> FROM CON> ( CON> SELECT CON> DT.DEPOSITO_ID, CON> (SELECT CON> SUM(DC2.CARICO - DC2.SCARICO) CON> FROM CON> DOC_CORPO DC2, CON> DOC_TESTA DT2 CON> WHERE CON> DC2.DOC_TESTA_ID = DT2.ID CON> AND DC2.ARTICOLO_ID = 'CRSSV750' CON> AND DT2.DATA < '01/01/2018' CON> ) AS RIPORTO, CON> SUM(DC.CARICO) as Carico, CON> SUM(DC.SCARICO) as Scarico, CON> SUM(DC.CARICO - DC.SCARICO) as ESISTENZA, CON> SUM(DC.IMPEGNATI) as Impegnati, CON> SUM(DC.ORDINATI) as Ordinati CON> FROM CON> DOC_CORPO DC, DOC_TESTA DT CON> WHERE CON> DC.DOC_TESTA_ID = DT.ID CON> AND DC.ARTICOLO_ID = 'CRSSV750' CON> AND DT.DATA >= '01/01/2018' CON> AND DT.DATA <= '12/31/2018' CON> GROUP BY CON> DT.DEPOSITO_ID CON> ); SQL> SELECT CON> DEPOSITO_ID, CON> RIPORTO, CON> CARICO, CON> SCARICO, CON> RIPORTO + ESISTENZA AS ESISTENZA, CON> IMPEGNATI, CON> ORDINATI CON> FROM CON> ( CON> SELECT CON> DT.DEPOSITO_ID, CON> (SELECT CON> SUM(DC2.CARICO - DC2.SCARICO) CON> FROM CON> DOC_CORPO DC2, CON> DOC_TESTA DT2 CON> WHERE CON> DC2.DOC_TESTA_ID = DT2.ID CON> AND DC2.ARTICOLO_ID = 'CRSSV750' CON> AND DT2.DATA < '01/01/2017' CON> ) AS RIPORTO, CON> SUM(DC.CARICO) as Carico, CON> SUM(DC.SCARICO) as Scarico, CON> SUM(DC.CARICO - DC.SCARICO) as ESISTENZA, CON> SUM(DC.IMPEGNATI) as Impegnati, CON> SUM(DC.ORDINATI) as Ordinati CON> FROM CON> DOC_CORPO DC, DOC_TESTA DT CON> WHERE CON> DC.DOC_TESTA_ID = DT.ID CON> AND DC.ARTICOLO_ID = 'CRSSV750' CON> AND DT.DATA >= '01/01/2017' CON> AND DT.DATA <= '12/31/2017' CON> GROUP BY CON> DT.DEPOSITO_ID CON> ); DEPOSITO_ID RIPORTO CARICO SCAR ICO ESISTENZA IMPEGNATI ORDINATI === === === === === === === 1 0. 1.000 0.0 000 1.000 0. 0. As you see: - if I select between 01/01/2018 and 31/12/2018 the are not rows in DT - if I select between 01/01/2017 and 31/12/2017 works fine because the are at least 1 row in DT Any suggestion? Thanks. -- Luigi Siciliano -- ++ Visit http://www.firebirdsql.org and click the Documentation item on the main (top) menu. Try FAQ and other links from the left-side menu there. Also search the knowledgebases at http://www.ibphoenix.com/resources/documents/ ++ Yahoo Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/firebird-support/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/firebird-support/join (Yahoo! ID required) <*> To change settings via email: firebird-support-dig...@yahoogroups.com firebird-support-fullfeatu...@yahoogroups.com <*> To unsubscribe from this group, send an email to: firebird-support-unsubscr...@yahoogroups.com <*> Your use of Yahoo Groups is subject to: https://info.yahoo.com/legal/us/yahoo/utos/terms/
Re: [firebird-support] Query and problem with nulls
I would suggest replacing your query with something like: with tmp(DaData) as (select cast(:DaData as date) from rdb$database) SELECT DT.DEPOSITO_ID, SUM(iif(DT.DATA < t.DaData, DC.CARICO - DC.SCARICO, 0)) AS RIPORTO, SUM(iif(DT.DATA >= t.DaData, DC.CARICO, 0)) as Carico, SUM(iif(DT.DATA >= t.DaData, DC.SCARICO, 0)) as Scarico, SUM(iif(DT.DATA >= t.DaData, DC.CARICO - DC.SCARICO, 0)) as ESISTENZA, SUM(iif(DT.DATA >= t.DaData, DC.IMPEGNATI, 0)) as Impegnati, SUM(iif(DT.DATA >= t.DaData, DC.ORDINATI, 0)) as Ordinati FROM DOC_CORPO DC JOIN DOC_TESTA DT ON DC.DOC_TESTA_ID = DT.ID CROSS JOIN tmp T WHERE DC.ARTICOLO_ID = :ID AND DT.DATA <= :AData GROUP BY DT.DEPOSITO_ID The reason for the CTE is because parameters are not variables and I have experienced cases when I've tried to refer to the same parameter twice only to discover that IBO treated it as two separate parameters (I don't know what components you use, but the CTE eliminates any ambiguity). In addition to this, I updated your query from implicit to explicit JOIN and simplified a bit (well, at least in my opinion). The way I would expect my query to differ from yours, is that mine will return rows with values in RIPORTO, but only 0's for the other summed fields. I've no clue whether or not this is what you want, please tell us more specifically what you're looking for if it is something different. HTH, Set 2018-01-04 13:32 GMT+01:00 Mark Rotteveel m...@lawinegevaar.nl [firebird-support]: > Without relevant DDL and sample data we can't really help you. > > Mark > > On 4-1-2018 12:36, Luigi Siciliano luigi...@tiscalinet.it > [firebird-support] wrote: > > Hallo, > > > > The following query works fine but returns *all nulls* if no rows > > between :DaData and :AData > > > > SELECT > > > > DEPOSITO_ID, > > RIPORTO, > > CARICO, > > SCARICO, > > RIPORTO + ESISTENZA AS ESISTENZA, > > IMPEGNATI, > > ORDINATI > > FROM > > ( > >SELECT > > DT.DEPOSITO_ID, > > (SELECT > > SUM(DC2.CARICO - DC2.SCARICO) > > FROM > > DOC_CORPO DC2, > > DOC_TESTA DT2 > > WHERE > > DC2.DOC_TESTA_ID = DT2.ID > > AND DC2.ARTICOLO_ID = :ID > > AND DT2.DATA < :DaData > > ) AS RIPORTO, > > SUM(DC.CARICO) as Carico, > > SUM(DC.SCARICO) as Scarico, > > SUM(DC.CARICO - DC.SCARICO) as ESISTENZA, > > SUM(DC.IMPEGNATI) as Impegnati, > > SUM(DC.ORDINATI) as Ordinati > >FROM > > DOC_CORPO DC, DOC_TESTA DT > >WHERE > > DC.DOC_TESTA_ID = DT.ID > > AND DC.ARTICOLO_ID = :ID > > AND DT.DATA >= :DaData > > AND DT.DATA <= :AData > >GROUP BY > > DT.DEPOSITO_ID > > ) > > > > How I can modify it? > > > > Thanks. > > > > > -- > Mark Rotteveel > > > > Posted by: Mark Rotteveel > > > ++ > > Visit http://www.firebirdsql.org and click the Documentation item > on the main (top) menu. Try FAQ and other links from the left-side menu > there. > > Also search the knowledgebases at http://www.ibphoenix.com/ > resources/documents/ > > ++ > > > Yahoo Groups Links > > > >
Re: [firebird-support] Query and problem with nulls
Without relevant DDL and sample data we can't really help you. Mark On 4-1-2018 12:36, Luigi Siciliano luigi...@tiscalinet.it [firebird-support] wrote: > Hallo, > > The following query works fine but returns *all nulls* if no rows > between :DaData and :AData > > SELECT > > DEPOSITO_ID, > RIPORTO, > CARICO, > SCARICO, > RIPORTO + ESISTENZA AS ESISTENZA, > IMPEGNATI, > ORDINATI > FROM > ( > SELECT > DT.DEPOSITO_ID, > (SELECT > SUM(DC2.CARICO - DC2.SCARICO) > FROM > DOC_CORPO DC2, > DOC_TESTA DT2 > WHERE > DC2.DOC_TESTA_ID = DT2.ID > AND DC2.ARTICOLO_ID = :ID > AND DT2.DATA < :DaData > ) AS RIPORTO, > SUM(DC.CARICO) as Carico, > SUM(DC.SCARICO) as Scarico, > SUM(DC.CARICO - DC.SCARICO) as ESISTENZA, > SUM(DC.IMPEGNATI) as Impegnati, > SUM(DC.ORDINATI) as Ordinati > FROM > DOC_CORPO DC, DOC_TESTA DT > WHERE > DC.DOC_TESTA_ID = DT.ID > AND DC.ARTICOLO_ID = :ID > AND DT.DATA >= :DaData > AND DT.DATA <= :AData > GROUP BY > DT.DEPOSITO_ID > ) > > How I can modify it? > > Thanks. > -- Mark Rotteveel
[firebird-support] Query and problem with nulls
Try use explicit JOIN here: FROM DOC_CORPO DC, DOC_TESTA DT
Re: [firebird-support] Query and problem with nulls
04.01.2018 12:36, Luigi Siciliano luigi...@tiscalinet.it [firebird-support] wrote: > The following query works fine but returns*all nulls* if no rows > between :DaData and :AData Are you sure that your query really returns all nulls and this is not a visual bug in the tool you run this query in? Show log from isql, please. -- WBR, SD. ++ Visit http://www.firebirdsql.org and click the Documentation item on the main (top) menu. Try FAQ and other links from the left-side menu there. Also search the knowledgebases at http://www.ibphoenix.com/resources/documents/ ++ Yahoo Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/firebird-support/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/firebird-support/join (Yahoo! ID required) <*> To change settings via email: firebird-support-dig...@yahoogroups.com firebird-support-fullfeatu...@yahoogroups.com <*> To unsubscribe from this group, send an email to: firebird-support-unsubscr...@yahoogroups.com <*> Your use of Yahoo Groups is subject to: https://info.yahoo.com/legal/us/yahoo/utos/terms/
[firebird-support] Query and problem with nulls
Hallo, The following query works fine but returns *all nulls* if no rows between :DaData and :AData SELECT DEPOSITO_ID, RIPORTO, CARICO, SCARICO, RIPORTO + ESISTENZA AS ESISTENZA, IMPEGNATI, ORDINATI FROM ( SELECT DT.DEPOSITO_ID, (SELECT SUM(DC2.CARICO - DC2.SCARICO) FROM DOC_CORPO DC2, DOC_TESTA DT2 WHERE DC2.DOC_TESTA_ID = DT2.ID AND DC2.ARTICOLO_ID = :ID AND DT2.DATA < :DaData ) AS RIPORTO, SUM(DC.CARICO) as Carico, SUM(DC.SCARICO) as Scarico, SUM(DC.CARICO - DC.SCARICO) as ESISTENZA, SUM(DC.IMPEGNATI) as Impegnati, SUM(DC.ORDINATI) as Ordinati FROM DOC_CORPO DC, DOC_TESTA DT WHERE DC.DOC_TESTA_ID = DT.ID AND DC.ARTICOLO_ID = :ID AND DT.DATA >= :DaData AND DT.DATA <= :AData GROUP BY DT.DEPOSITO_ID ) How I can modify it? Thanks. -- Luigi Siciliano -- ++ Visit http://www.firebirdsql.org and click the Documentation item on the main (top) menu. Try FAQ and other links from the left-side menu there. Also search the knowledgebases at http://www.ibphoenix.com/resources/documents/ ++ Yahoo Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/firebird-support/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/firebird-support/join (Yahoo! ID required) <*> To change settings via email: firebird-support-dig...@yahoogroups.com firebird-support-fullfeatu...@yahoogroups.com <*> To unsubscribe from this group, send an email to: firebird-support-unsubscr...@yahoogroups.com <*> Your use of Yahoo Groups is subject to: https://info.yahoo.com/legal/us/yahoo/utos/terms/