New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NOT EXISTS() predicate sometimes return NULL instead of True or False [CORE6120] #6369
Comments
Modified by: @livius2summary: NOT EXISTS() predicate sometimes return NULL instead True or False => NOT EXISTS() predicate sometimes return NULL instead of True or False |
Commented by: @livius2 You can simplify query by removing WHERE clause from main query. |
Commented by: Sean Leyne (seanleyne) Karol, You need to provide a simpler example! It should not be necessary for someone to have to deal with **> 350 lines** of SQL to determine the source of the problem!!! |
Commented by: Sean Leyne (seanleyne) When a smaller example is provided, the ticket/case will be re-opened. |
Modified by: Sean Leyne (seanleyne)status: Open [ 1 ] => Resolved [ 5 ] resolution: Cannot Reproduce [ 5 ] |
Commented by: @livius2 Sean, If I knew what the problem is, I could slim down this sql. But i do not know the root of the problem. And also, you do not need to deal with whole query. Only debug 2 things. |
Commented by: Sean Leyne (seanleyne) "Reproduceable" also means "intelligible/understandable". The SQL that you have provided (IMO) is a code generated DISASTER (I know from the linked case that you are using code to generate the SQL). It re-uses the same aliases for the same/identical CTE multiple times. I don't know what you have tried in your debugging, but I would start by re-factoring the SQL as follows: IMO, if you can't re-factor you code to a simple/basic example then it is in-appropriate to expect the engine or a developer to make sense of the problem! Otherwise, you are simply <i>"throwing a wad of cold spaghetti against a wall, letting it harden and expecting someone else to do untangle it"</i>. Finally, my name is not "Shane". |
Commented by: @livius2 What i describe above, you do not need to deal with whole sql and analyze it 1. and look when and how Exists here return the value "False" 2. then look what engine do with this result when you change it to not exists when you do this and do not get the answer then you can ask me for something easier to reproduce. PS. sorry for the name (fixed now) |
Commented by: Sean Leyne (seanleyne) Edited SQL down to the minimum required to reproduce |
Modified by: Sean Leyne (seanleyne)description: In below query second column rerurn "NULL" instead of "TRUE". If you remove "NOT" then result is "FALSE". SELECT
/* jest jakakolwiek deklaracja w zadanym przedziale to znaczy, ze jest platnikiem */
/* robimy liste kwartalow w ktorych obowiazuje status dzialalnosci kontrahenta i szukamy brakow deklaracji */ FROM
/* robimy liste kwartalow w ktorych obowiazuje status dzialalnosci kontrahenta i szukamy brakow deklaracji */
/* robimy liste kwartalow w ktorych obowiazuje status dzialalnosci kontrahenta i szukamy brakow deklaracji */ => In below query second column rerurn "NULL" instead of "TRUE". If you remove "NOT" then result is "FALSE". SELECT
/* jest jakakolwiek deklaracja w zadanym przedziale to znaczy, ze jest platnikiem */
/* robimy liste kwartalow w ktorych obowiazuje status dzialalnosci kontrahenta i szukamy brakow deklaracji */ FROM |
Commented by: Sean Leyne (seanleyne) Does this SQL have the same problem? WITH
) |
Commented by: Sean Leyne (seanleyne) FYI, not all the criteria for the column is contained in the NOT EXISTS(), so the problem may not be there... What happens if you drop:
|
Commented by: @livius2 Sean, you have absolutely right here false and NULL = false then the issue doesn't exists at all. |
Modified by: Sean Leyne (seanleyne)status: Resolved [ 5 ] => Reopened [ 4 ] resolution: Cannot Reproduce [ 5 ] => |
Commented by: Sean Leyne (seanleyne) Re-opened the case to provide the correct resolution details. Karol has agreed that there is no issue to be addressed by this ticket. |
Modified by: Sean Leyne (seanleyne)status: Reopened [ 4 ] => Resolved [ 5 ] resolution: Won't Fix [ 2 ] |
Modified by: @pcisarstatus: Resolved [ 5 ] => Closed [ 6 ] |
Submitted by: @livius2
In below query second column rerurn "NULL" instead of "TRUE". If you remove "NOT" then result is "FALSE".
Run it against database from CORE6106
SELECT
http://K.ID
,
/* jest jakakolwiek deklaracja w zadanym przedziale to znaczy, ze jest platnikiem */
EXISTS
(SELECT
*
FROM
SPR S
INNER JOIN KWARTAL SKW ON S.ID_KWARTAL = http://SKW.ID
WHERE
S.ID_KONTRAHENT = http://K.ID
AND SKW.CKWARTAL BETWEEN KW.MIN_KWARTAL
AND KW.MAX_KWARTAL
)
OR
/* jesli ma wpis o statusie dzialnosci za zadany okres (z data pozyaskania nie z przyszlosci) to tez jest platnikiem */
EXISTS
(SELECT
*
FROM
STATUS_PLATNIKA SP
INNER JOIN STATUS_PLATNIKA_TYP SPT ON SP.ID_STATUS_PLATNIKA_TYP = http://SPT.ID
WHERE
SP.ID_KONTRAHENT = http://K.ID
AND SP.DATA_POZYSKANIA <= KW.MAX_ZALICZAJ_DO
AND (SPT.FLAGA_SYS IN(1) AND ((SP.DATA_OD <= KW.MAX_D_DO) AND (SP.DATA_DO IS NULL OR (SP.DATA_DO >= KW.MIN_D_OD))))
)
/* robimy liste kwartalow w ktorych obowiazuje status dzialalnosci kontrahenta i szukamy brakow deklaracji */
INNER JOIN KWARTALY KW ON
EXISTS
(SELECT
*
FROM
STATUS_PLATNIKA SP
INNER JOIN STATUS_PLATNIKA_TYP SPT ON SP.ID_STATUS_PLATNIKA_TYP = http://SPT.ID
WHERE
SP.ID_KONTRAHENT = http://K.ID
AND SP.DATA_POZYSKANIA <= KD.MAX_ZALICZAJ_DO
AND (SPT.FLAGA_SYS IN(1) AND ((SP.DATA_OD <= KW.D_DO) AND (SP.DATA_DO IS NULL OR (SP.DATA_DO >= KW.D_OD))))
)
WHERE
NOT EXISTS
(SELECT
*
FROM
SPR S
WHERE
S.ID_KONTRAHENT = http://K.ID
AND S.ID_URZADZENIE = http://U.ID
AND ((EXTRACT(YEAR FROM S.DATA_OD)*12+EXTRACT(MONTH FROM S.DATA_OD)) BETWEEN(KW.ROK*12+KW.MIESIAC_OD) AND (KW.ROK*12+KW.MIESIAC_DO) OR (EXTRACT(YEAR FROM S.DATA_DO)*12+EXTRACT(MONTH FROM S.DATA_DO)) BETWEEN(KW.ROK*12+KW.MIESIAC_OD) AND (KW.ROK*12+KW.MIESIAC_DO))
)
) AND (WITH KWARTALY AS
(SELECT
*
FROM
KWARTAL KW
WHERE
KW.CKWARTAL BETWEEN '2019.I'
AND '2019.I'), KWARTALY_DATY AS
(SELECT
MIN(KW.ZALICZAJ_OD) AS MIN_ZALICZAJ_OD
, MAX(KW.ZALICZAJ_DO) AS MAX_ZALICZAJ_DO
, MIN(KW.D_OD) AS MIN_D_OD
, MAX(KW.D_DO) AS MAX_D_DO
, MIN(KW.CKWARTAL) AS MIN_KWARTAL
, MAX(KW.CKWARTAL) AS MAX_KWARTAL
FROM
KWARTALY KW)
SELECT
DATEDIFF(DAY, KD.MIN_D_OD, KD.MAX_D_DO)
FROM
KWARTALY_DATY KD) =(WITH KWARTALY AS
(SELECT
*
FROM
KWARTAL KW
WHERE
KW.CKWARTAL BETWEEN '2019.I'
AND '2019.I'), KWARTALY_DATY AS
(SELECT
MIN(KW.ZALICZAJ_OD) AS MIN_ZALICZAJ_OD
, MAX(KW.ZALICZAJ_DO) AS MAX_ZALICZAJ_DO
, MIN(KW.D_OD) AS MIN_D_OD
, MAX(KW.D_DO) AS MAX_D_DO
, MIN(KW.CKWARTAL) AS MIN_KWARTAL
, MAX(KW.CKWARTAL) AS MAX_KWARTAL
FROM
KWARTALY KW)
SELECT
SUM(DATEDIFF(DAY, CASE WHEN SP.DATA_OD < KD.MIN_D_OD THEN KD.MIN_D_OD ELSE SP.DATA_OD END, CASE WHEN COALESCE(SP.DATA_DO, KD.MAX_D_DO) > KD.MAX_D_DO THEN KD.MAX_ZALICZAJ_DO ELSE COALESCE(SP.DATA_DO, KD.MAX_D_DO) END))
FROM
KWARTALY_DATY KD
INNER JOIN STATUS_PLATNIKA SP ON 1 = 1
INNER JOIN STATUS_PLATNIKA_TYP SPT ON SP.ID_STATUS_PLATNIKA_TYP = http://SPT.ID
WHERE
SP.ID_KONTRAHENT = http://K.ID
AND SP.DATA_POZYSKANIA <= KD.MAX_ZALICZAJ_DO
AND (SPT.FLAGA_SYS IN(2) AND SP.DATA_OD <= KD.MAX_D_DO AND (SP.DATA_DO IS NULL OR SP.DATA_DO >= KD.MIN_D_OD))))
FROM
KONTRAHENT K
The text was updated successfully, but these errors were encountered: