I have below table and SQL query written, this query should not return any result but its returning ID = 1 , what is wrong with the SQL query? Can anyone please help?
** Note balance data type is decimal rest are varchar
ID code balance level
1 C 150.00
1 P 40027.42 F
1 P 40027.42 F
select distinct ID from table
(
(code = 'P' and balance = 40027.42 and level = 'F') or
(code = 'C' and balance = 151.00 )
)
group by ID
having count(ID) >=2
havingclause ofcount(ID) >=2, this query is returning theidfor rows that meet thewhereclauses and have more than 1 row, which would beID = 1. Since you have two rows forID = 1that meet the clause of(code = 'P' and balance = 40027.42 and level = 'F')Also,distinctis not needed for this query.(code = 'P' and balance = 40027.42 and level = 'F')Since you have two rows that meet that critieria, it passes the having clause ofhaving count(ID) >=2. greater than or equal to 2