Returning all results in one column, but using a where statement to filter another column

StackOverflow https://stackoverflow.com/questions/21976001

  •  15-10-2022
  •  | 
  •  

문제

Using SQlite 3, and have a table that looks like the following

PKEY|TS      | A |B  |C
 1  |00:05:00|200|200|200
 2  |00:10:00|100|100|100
 3  |00:15:00|   |25 |
 4  |00:20:00|   |   |

Currently I'm using

select ts, (a+b+c) from tablename WHERE a !='null' AND b !='null' and c !='null';"

which returns

TS      | (a+b+c)
00:05:00|600
00:10:00|300

I want my results to look like the following though:

TS      | total
00:05:00|600
00:10:00|300
00:15:00|
00:20:00|

So in other words, I always want to return everything from the TS column, but I don't want to return the total unless A,B, and C have values.

I think I might need a union or a join, but I can't seem to find an example where the results from a single column are always returned.

도움이 되었습니까?

해결책

Testing for a != 'null' will never be true. you need to use a is not null.

However, to return a null total if any of the numbers are null, just do this:

select ts, a+b+c
from tablename;

Why does this work? Because in SQL if any part of an expression is null, the whole expression is null. This makes sense when you consider that null means "unknown". Logically, when part of a calculation is unknown, then the result is unknown.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top