Sql query fetch problem

Dears,

I have a problem with fetching data from SQL DB,

I have to use the below node to fetch the below SQL query while, instated the specific date I mentioned in where clues, the result includes all date.

image

Hi @natanzi, I’m guessing your query currently returns no rows. The column “Id” cannot equal one value whilst at the same time equal a different value, which is what your query is currently stating.

Maybe instead of ANDs inside the brackets they should be ORs?

Unfortunately from the query shown it really isn’t clear what your intention actually is. You will either need to fix the query or else tell us in words what you are intending.

4 Likes

Dear Takbb,
Thanks for your reply, thew below note represented my query and it has a result, while consist of the data before 20211001 as well, which is wrong. to be specific, I have data from first row of this table (total data of table) and I look for the cause.
select
“Date_Key” as Date
,
“NSK”
,sum(“Usage_Daily”) [usage]

from “DS”.“My_table”
where “Date_Key” >= 20211001 and “Date_Key”<= 20211130 and
(“Id” = 1020 and “O_Id” = 65000)
or (“Id” = 407 and “O_Id” = 64530)
group BY "Dat

@natanzi could it be that you are simply missing some brackets?

select
“Date_Key” as Date
, “NSK”
, sum(“Usage_Daily”) AS usage

from “DS”.“My_table”
where “Date_Key” >= 20211001 and “Date_Key”<= 20211130 and
( /* <= opening bracket */
(“Id” = 1020 and “O_Id” = 65000)
or (“Id” = 407 and “O_Id” = 64530)
) /* <= closing bracket */
group BY “Date”

4 Likes

Hi @natanzi,

As suspected then for your query to return any rows it could not be the query that was shown in your original screenshot, as that definitely would return no rows. I see that you have corrected it to use “O_Id” in addition to “Id” which now makes more sense.

I’m inclined to agree with @mlauber71 that you are probably missing brackets as the precedence rules of SQL mean that AND takes precedence over OR. Whether your query is returning the correct rows depends on what you are actually asking.

To further expand on @mlauber71’s suggestion, if you have a query of the form

Where A and (B and C) or (D and E)

This will be return any rows that satisfy:

A and (B and C)

OR

(D and E)

Whereas if the condition is

Where A and ( (B and C) or (D and E) )

It will return rows satisfying

A and (B and C)

OR

A and (D and E)

Hope that helps

6 Likes

Many thanks.

BR,
Milad

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.