delphi 3

I have two implement below logic,

case function of
1 : Showmessage ('1');
2: Showmessage('2');
3: showmessage('3');

function in above statement shall return 1 or 2 or 3 querying a table on below conditions:

The table has two columns eg A and B, which contain either 0 or 1 values.
First condition shall be querying the table where column B has all 0 values. For this condition the function shall return 1.
Second condition:  Column B has all 1 values and column A has 0 values. For this condition the function shall return 2.
Third condition: Column B has all 1 values and Column A has 1 values, for this condition the function shall returen 3.

Need help with code snippet !
Who is Participating?
jimyXConnect With a Mentor Commented:
Here is a query that detects your three conditions and returns either 1, 2 or 3, and in case of other order it returns negative:
function CaseResultZeroOne():integer;
  with AdoQry do
      Sql.Add('select (case Sum(b) when 0 then 1 else');
      Sql.Add('case when (Sum(b) = Count(b)) and (Sum(a) = 0) then 2 else');
      Sql.Add('case when (Sum(a) = Count(a)) and (Sum(b) = Count(b)) then 3 else -2 end end end) AS CaseResult');
      Sql.Add('from DBTable;');
      if IsEmpty then
        Result := -1
        Result := FieldByName('CaseResult').AsInteger;


case CaseResultZeroOne() of
  1 : Showmessage ('1');
  2: Showmessage('2');
  3: showmessage('3');

Open in new window

Assuming your table name is "DBTable".
What happens if your conditions are not met?
For instance, if column A is not all ones but column B is all ones?
Geert GOracle dbaCommented:
this is an abstract sample of a problem
as jimyX pointed out, the else condition is missing
or the equation of your abstract problem is not complete
AbhiJeetAuthor Commented:
any other nice and simple work around?
It involves DB and records, no other work around.
The only way to do that is to read the records from the database so that's the best that it could be.

Probably you can explain, what did you expect the solution to look like.
Geert GOracle dbaCommented:
from a quote: "nulls are evil"
from the text of your question: either 0 or 1 values

what do you understand as 0 values
> the column has all values as zero (or 0)
> the column has all nulls (or 0 values)

your question contains ambiguities and because of that it's unclear what you want
> you'll have to explain in more detail what you want
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.