I have a requirement where I need to write a few lines of case statement to generate a new column, counting the first and the second column to return the result.
In this example I want to check if the second column has more null, then return 'semi valid'
ref no first column second column result (new column)
1234 yes yes
1234 null null
1234 yes null
In this example I want to check if there's no null in both, then return 'valid'
ref no first column second column result (new column)
1234 yes yes
1234 yes yes
1234 yes yes
In this example I want to check if the second column has more null, then return 'higher valid'
ref no first column second column result (new column)
1234 yes yes
1234 yes yes
1234 yes null
In this example I want to check if the first column has more null, then return 'not valid'
ref no first column second column result (new column)
1234 null yes
1234 null yes
1234 null yes
Related
In my Postgres database, I'm checking user answers for correctness by checking if two IDs, "user_answered_id" and "expected_answer_id", are equivalent. If the user doesn't provide a "user_answered_id", then we still mark their answer as incorrect.
In Postgres, the following queries
select case when 1 != null then TRUE else FALSE end as test;
select case when 1 = null then TRUE else FALSE end as test;
both result in FALSE. This is true for any number check (e.g., when 2 != null, when 3 != null, ..., etc.
Why doesn't CASE WHEN show TRUE for 1 != null?
Must I put in the check "or is null"? E.g.,
CASE WHEN
user_answered_id != expected_answer_id
OR user_answered_id IS NULL
THEN TRUE
ELSE FALSE
END as user_incorrect_tally
What you are looking for is: IS DISTINCT FROM
select 2 is distinct from null;
?column?
----------
t
select 2 is distinct from 1;
?column?
----------
t
From the docs:
datatype IS DISTINCT FROM datatype → boolean
Not equal, treating null as a comparable value.
1 IS DISTINCT FROM NULL → t (rather than NULL)
NULL IS DISTINCT FROM NULL → f (rather than NULL)
SQL uses three-valued logic: true, false, and null. Null is not false. Null can be thought of as "no value".
Operations on null almost always yield null. So 1 != null is null. 1 = null is null. null = null is null. 5 < null is null. Etc.
To check for null, use is null and is not null.
Back to your query. is not distinct from and is distinct from are like = and != which treat null as a comparable value. So null is distinct from 1 will be true.
select
user_answered_id is distinct from expected_answer_id as user_incorrect
If you need to convert a null into a different value such as 0 or an empty string, use coalesce.
select
coalesce(user_answered_text, 'No Answer')
Your column is named "tally", but a tally means a count. If you intend to count a user's true and false answers use count with a filter.
select
count(user_answered_id) filter (
where user_answered_id = expected_answer_id
) as user_correct_tally,
-- count ignores null, this will only be the questions they tried to answer
count(user_answered_id) as user_answered_tally,
count(user_answered_id) filter (
where user_answered_is is distinct from expected_answer_id
) as user_incorrect_tally
Yes, You should check NULL value with is null, And last query you wrote is correct.
I suggest you to read below documents:
https://www.postgresql.org/docs/current/functions-comparison.html
"b" ~ meta[update b:1b from ([]a:1 2) where a=3][`b;`t]
But how does q knows what type should it be in the column b without any matched rows (where a=3) in update expression?
It infers the type from your statement, it doesn't matter if there was a match or not. It takes the null of whatever type you've specified for the column.
Same thing if there was a match:
q)update b:.z.D from ([]a:1 2) where a=2
a b
------------
1
2 2021.05.14
It fills the matches with your value, but the non-matches get the corresponding null for that datatype. Boolean is a grey area as there technically isn't a null but 0b is the assumed null Boolean
Can anyone tell me which command is used for concatenate three columns data into one column in PostgreSQL database?
e.g. If the columns are
begin | Month | Year
12 | 1 | 1988
13 | 3 |
14 | | 2000
| 5 | 2012
output:
Result
12-1-1988
13-3-null
14-null-2000
null-5-2012
Actually, I have concatenated two columns but it is displaying only those values in the result
which is not null in all columns but i want to display that value also which is not null in single
column.
If you simply used a standard concatenation function like concat() or the || operator, you'd get a complete null string when any element is null.
You could use the function concat_ws() which ignores a null value. But you are expecting them to be shown.
So you need to cast the real null value into a non-null text 'null'. This could be done using the COALESCE() function, which takes several arguments and returns the first non-null. But here the problem occurs, that the 'null' string is of another type (text) than the columns (int). So you have to equalize the types, e.g. by casting the int values into text before. So, finally your query could look like this:
Click: demo:db<>fiddle
SELECT
concat_ws('-',
COALESCE(begin::text, 'null'),
COALESCE(month::text, 'null'),
COALESCE(year::text, 'null')
)
FROM mytable
I'm trying to insert values in a tabla with values inside, like this table:
If you look at the column "202001" and row 22 at the end, it is null, so I want to insert manually any value.
It is null because that value it's not inserted to the database yet.
The expression I have is Sum([SH_historico_1.MONTO]/1000000) (Mounts..)
Any ideas? maybe with that SUM and then concadenate
Please try:
Alt(Sum([SH_historico_1.MONTO]/1000000),0)
where 0 is as you say "any value".
ALt function return first numeric value. So when your function will not return any number, another number is used.
It is possible to create table where for example:
CREATE TABLE IF NOT EXISTS router (
id BIGSERIAL PRIMARY KEY
, name_1 BOOLEAN NOT NULL DEFAULT FALSE
, name_2 BOOLEAN NOT NULL DEFAULT FALSE
, name_3 BOOLEAN NOT NULL DEFAULT FALSE
, name_4 BOOLEAN NOT NULL DEFAULT FALSE
, name_5 BOOLEAN NOT NULL DEFAULT FALSE
);
And with this table i would use something like lock to prevent of put more than 1 true value bettwen name_1 - name_5. For example if true value exist in name_1 is impossible to put something in name_2 - name_5. If exists in name_3 is impposible to put name_1,2,4 and 5
You can add a check constraint:
check (name_1 and not name_2 and not ...
or name_2 and not name_1 and not ...
or ...)
(There probably are shorter ways to express the boolean expression, but that's the gist of it.)