0

In DB Browser for SQLite, if I have two columns, results1 and results2, each of which can either have "pass" or "fail" (or blank( in the column, how can I create a third column called results_final which returns "pass" if both results1 and results2 had "pass" in the same row, or returns "fail" if it was any other combo? So essentially trying to use a CASE WHEN but with conditions in two different columns. Is this possible? In Excel, I could just do a =IF(AND(pass,pass), "pass", "fail") sort of formula but what can I do here?

GMB
  • 216,147
  • 25
  • 84
  • 135
bz_2020
  • 79
  • 1
  • 14

1 Answers1

0

You can use a case expression like so:

select 
    t.*, 
    case when result1 = 'pass' and result2 = 'pass' 
        then 'pass' 
        else 'fail' 
    end result3
from mytable t

It is unclear whether you actually want to create a new column to hold that value; I wouldn't actually recommend storing that information. You can use a view, or a generated virtual column for that:

alter table add column column3 text as (
    case when result1 = 'pass' and result2 = 'pass' 
        then 'pass' 
        else 'fail' 
    end) virtual
GMB
  • 216,147
  • 25
  • 84
  • 135
  • I am not sure if this works here because the notation is slightly different to "usual" SQL. I think after CASE an expression follows (in DB Browser for SQLite) so not sure how to write it? https://www.sqlitetutorial.net/sqlite-case/ – bz_2020 Apr 27 '20 at 21:56
  • @bz_2020: there are two ways to phrase a `CASE` expression. The short version, which you are showing, and the long version, that I used. This is standard SQL that should work just fine in SQLite. – GMB Apr 27 '20 at 22:02
  • Thanks! I thought the issue was in the CASE but I had a misplaced comma :) – bz_2020 Apr 27 '20 at 22:04