i have created a query that returns users who placed order in a specific month and after how many months they have returned. the problem is when there's no user placed any order in a month it returns blank . I want to return zero instead .
I created a case statement as :
case when user_id is null then 0 else user_id end
I also tried:
case when user_id is null and delivery_month is null and returned_month is null then 0 else user_id end
if there's no row evaluated for the user, how can i replace that with 0 ?
this is how blank values looks like when I loaded data in the visualization tool:
Related
if no result found, need to show some default value for single row
and multiple column for example query
select CASE
WHEN entity_id IS NULL THEN 0 ELSE entity_id
END simplefiled
from tds2022.quarter_main
where client_id = 6365;
above query not working when no records exists
Edited to provide sample data
I am trying to create a calculated column that sums 2 other columns, which should be easy. However, some values in both of the columns are null, so I want to use a case expression to replace null values in both columns with 0s and then add up the resulting values. The other complicating factor is that the second column contains text values with commas that need to be converted to numerical before I can add them. What I am currently trying to do is:
SELECT (case when pm."PS" is null then 0 else pm."PS" end) + (case when pm."PS-PREV1" is null then 0 else replace(pm."PS-PREV1", ',', '')::numeric end) AS "Sales"
FROM pm
Sample data:
PS
PS-PREV1
20000
null
30000
20,000
null
null
null
30,000
Desired output:
output
20000
50000
0
30000
This is just returning the value of the 1st column without adding in the second column. Where am I going wrong? Am I overthinking this?
your code should work , however you can write it a little bit more clean:
SELECT COALESCE(pm."PS",0)
+ COALESCE(replace(pm."PS-PREV1", ',', '')::numeric,0) AS "Sales"
FROM pm
for example is this query possible?
CREATE TABLE products (
name text,
price numeric not null,
CHECK (IF price > 0 THEN name IS NOT NULL ELSE name IS NULL END IF)
);
UPDATE:
seems like no
here https://rextester.com/l/postgresql_online_compiler
it throws error
Error(s), warning(s):
42601: syntax error at or near "price"
looking at documentation https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-EXCLUDE it says
Currently, CHECK expressions cannot contain subqueries nor refer to variables other than columns of the current row. The system column tableoid may be referenced, but not any other system column.
but IF is not subquery, dont understand why it doesnt work
UPDATE 2:
the
CREATE TABLE products (
name text,
price numeric not null,
CHECK ((price > 0 AND name IS NOT NULL) OR (price <= 0 AND name IS NULL))
);
works, but it makes tedious to write complex queries this way
IF is not a subquery, and it is not anything else either, in SQL. So it is assumed to be a column name. Having two (assumed) column names immediately in a row is a syntax error, and is assigned to the second column name.
SQL has CASE, not IF. You need to use the language you are using, not just make up things you hope to work.
CREATE TABLE products (
name text,
price numeric not null,
CHECK (case when price > 0 THEN name IS NOT NULL ELSE name IS NULL END)
);
I'm not 100% on what you're asking, but I think you're saying:
If the price > 0 then name CANNOT be NULL
In which case this should do it:
CHECK (price > 0 AND name IS NOT NULL)
If the name can be NULL on price being 0 then use this:
CHECK ((price > 0 AND name IS NOT NULL) OR (price = 0 AND name IS NULL))
You don't need to specify IF in the CHECK condition, it should essentially contain the actual statement to be tested rather than an IF statement.
No version of a character varying field will return anything but NULL if there isn't a value... it's really frustrating
CASE WHEN COALESCE(NULLIF(e.name,''),'unassigned') IS NULL THEN 'unassigned' ELSE a.name END
was my final test and it still simply returns NULL unless the field has a value
it's character varying(255)
COALESCE(a.name,'unassigned') // won't work
NULLIF(a.name,'') // won't work
NULLIF(a.name,NULL) // won't work
COALESCE(NULLIF(a.name,''),'unassigned') // won't work
however the instant i use 0 it works..
what's up with that?
it's a character varying(255) field and it is set to default to null
as a matter of point the build of the table column is
name varying character(255) DEFAULT(NULL)
so I know it's entering NULL
and I've already done a
SELECT * FROM <tbl> WHERE name IS NULL; and of course, I return all the NULL rows with a.name... so what's the deal with this?
ok... to everyone deciding to answer me with:
COALESCE(NULLIF(e.name,''),'unassigned') IS NULL...
This method will never work on a return of "no records" and as this is a stored procedure which creates a materialized view, where I'm polling via nested queries - where it is possible for a column to have 0 (as the default id = other_id) the nested query would simply return no rows. When no row is returned, the functions of COALESCE or NULLIF would never execute. A row would have to be returned in order for those functions to act upon the row values... As I've never heard of a table with a PK auto-incremented field starting at 0 and generally starting at 1 the resultset of "no records returned" will always return a NULL value into the materialized view column.
The query I run afterward to poll rows from that materialized view will however function properly as COALESCE(etc etc) because now there is an actual NULL value in that column.
I am relatively new to Tableau and I am wondering if there is a way to calculate null values in a column.
I have a column called Email of type string and want to know how many people have not entered their email i.e. Null.
I tried to create a calculated field with
count(ISNULL([Email]))
But this gives me the total count and not the count of null.
Thanks.
You cannot count NULL since COUNT ignores NULLs.
You can do this, though:
SUM(IF ISNULL([Email]) THEN 1 ELSE 0 END)
Per your additional comment, if you wanted to count where two fields are both NULL then:
SUM(IF ISNULL([Email]) AND ISNULL([Phone]) THEN 1 ELSE 0 END)
You can continue this for any number of fields, as needed.
You can use a filter for that field. Set it to only show you Null values, and set the measure to Count instead of the default Sum.
You can create another column where all null values are set to 1 and anything else to 0, >then count that. It should be something like
>COUNT(IF "null" THEN 1 ELSE 0)
A working syntax for that would had been:
SUM(IF ([Your field] = null) THEN 1
ELSE 0
END)
You can create another column where all null values are set to 1 and anything else to 0, then count that. It should be something like
COUNT(IF "null" THEN 1 ELSE 0)