0
select * from table1
where 
(
    (
    --group1
    amount_1 > 0
    or 
    amount_2 > 0
    or 
    amount_3 > 0
    )
or
   (
    --group2
    amount_4 > 0
    or 
    amount_5 > 0
    or 
    amount_6 > 0
   )
)
or
  amount_7 > 0
;

Above is a preliminary sql statement. I need to add a feature where to use at least three amounts from both groups or more, and at least one amount of each group (group1 and group2).

Example1: If group1 amounts1-3 are less than 0 then use amount_7.

Example2: If group1 amount_1 is more than zero and group2 amount_5 and amount_6 are more than zero then use amount_1, amount_5, amount_6.

Should I use some case-structure and give 1 and 0 when amount is bigger than 0 or how?

Amounts are pre-summed sales in this example, no need to sum anymore.

Answer is something like that, still testing....

select * from table1
where 
(
    (
    --group1
   case when

    (case when amount_1 > 0 then 1 else 0 end)
    + 
    (case when amount_2 > 0 then 1 else 0 end)
    + 
    (case when amount_3 > 0 then 1 else 0 end)

    > 0 

    then 

     (
        --group1
        amount_1 > 0
        or 
        amount_2 > 0
        or 
        amount_3 > 0
        )

    else null end

    )
or
   (
    --group2
    (case when amount_4 > 0 then 1 else 0 end)
    + 
    (case when amount_5 > 0 then 1 else 0 end)
    + 
    (case when amount_6 > 0 then 1 else 0 end)

    > 0 

    then 

         (
     --group2
        amount_4 > 0
        or 
        amount_5 > 0
        or 
        amount_6 > 0
    )
    else null end
   )
)
or
  amount_7 > 0
;

or a simpler solution:

select * from table1
where 
(
    (
    --group1
    amount_1
    +
    amount_2 
    + 
    amount_3 
    >0
    )
and
   (
    --group2
    amount_4
    +
    amount_5
    +
    amount_6
    >0
   )
)
or
  amount_7 > 0
;
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
jimbiz
  • 3
  • 2
  • Note: with only `or`s involved, you don't need the`(` and `)` Maybe you intended some `and`s somewhere? – wildplasser Oct 17 '18 at 12:35
  • 3
    Please [edit] your question to provide sample data and the output you'd like to obtain from that data, along with a tag for the specific DBMS you're using. – Ken White Oct 17 '18 at 12:40
  • Can you describe your problem in a more elaborate way? – codeLover Oct 17 '18 at 12:42
  • "where to use at least three amounts" and "then use amount_7" what does "use" ***actually mean*** in the context the quoted parts appear in ? – Erwin Smout Oct 17 '18 at 12:48
  • PS "if cond-1 then cond-2 else cond-3" is not the right way to be thinking when writing a WHERE clause, which must be a simple logic predicate/clause. "(cond-1 AND cond-2) OR (not cond-1 AND cond-3)" otoh, is. But from what you wrote, I can't tell whether this is what you're thinking of. – Erwin Smout Oct 17 '18 at 12:53
  • Are you looking for something like `... where case when ( amount_1 < 0 and amount_2 < 0 and amount_3 < 0 ) and ( amount_7 > 0 ) then 1 when ... then 1 else 0 end = 1;`? (Parentheses added for clarity.) – HABO Oct 17 '18 at 13:03
  • Thanks HABO, I got some ideas, see my edited question. I continue testing. – jimbiz Oct 18 '18 at 06:26
  • Your New * Improved code has a problem. `case when ... then ( amount_1 > 0 or ... ) ...` is trying to return a [boolean](https://msdn.microsoft.com/en-us/library/ms188074.aspx#Anchor_0) data type (with values TRUE, FALSE and UNKNOWN), but you cannot get a firm grip on one: "Unlike other SQL Server data types, a Boolean data type cannot be specified as the data type of a table column or variable, and cannot be returned in a result set." You need to return a "more" supported data type, e.g `Int`, and compare the result of the `case` to a value, e.g. `= 1`. – HABO Oct 18 '18 at 13:59
  • [This](https://stackoverflow.com/questions/10256848/can-i-use-case-statement-in-a-join-condition/10260297#10260297) answer shows an example of using a `case` expression in an `on` clause. It is also applicable to a `where`. – HABO Oct 18 '18 at 13:59

0 Answers0