I would like to create a view that selects ALL columns in a table, then creates/adds an additional "columnX" based on the value of another column in that same table.
For example if [column1] IS NULL, I would like [columnX] = 0 (zero)
and if [column1] IS NOT NULL, I would like [columnX] = [column1]
I think this should be simple, but I cannot seem to find the solution.
Can anyone please help?
Thanks!
case when col1 is null then 0 else col1 end as COLUMNX
Related
hey guys I've some data in event_name with a community_code = 1 .
now I need to add rows to the same data to the table with community_code = 2. instead of adding each row, is it possible to add all the rows with different community_code. please help me out thank you.
You can insert by querying the table and replacing values as needed. For example:
insert into community_comms (id, community_id, event_name)
select id, 2, event_name
from community_comms
where community_id = 1
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.
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.
original query looks like this :
UPDATE reponse_question_finale t1, reponse_question_finale t2 SET
t1.nb_question_repondu = (9-(ISNULL(t1.valeur_question_4)+ISNULL(t1.valeur_question_6)+ISNULL(t1.valeur_question_7)+ISNULL(t1.valeur_question_9))) WHERE t1.APPLICATION = t2.APPLICATION;
I know you cannot update 2 tables in a single query so i tried this :
UPDATE reponse_question_finale t1
SET nb_question_repondu = (9-(COALESCE(t1.valeur_question_4,'')::int+COALESCE(t1.valeur_question_6,'')::int+COALESCE(t1.valeur_question_7)::int+COALESCE(t1.valeur_question_9,'')::int))
WHERE t1.APPLICATION = t1.APPLICATION;
But this query gaves me an error : invalid input syntax for integer: ""
I saw that the Postgres equivalent to MySQL is COALESCE() so i think i'm on the good way here.
I also know you cannot add varchar to varchar so i tried to cast it to integer to do that. I'm not sure if i casted it correctly with parenthesis at the good place and regarding to error maybe i cannot cast to int with coalesce.
Last thing, i can certainly do a co-related sub-select to update my two tables but i'm a little lost at this point.
The output must be an integer matching the number of questions answered to a backup survey.
Any thoughts?
Thanks.
coalesce() returns the first non-null value from the list supplied. So, if the column value is null the expression COALESCE(t1.valeur_question_4,'') returns an empty string and that's why you get the error.
But it seems you want something completely different: you want check if the column is null (or empty) and then subtract a value if it is to count the number of non-null columns.
To return 1 if a value is not null or 0 if it isn't you can use:
(nullif(valeur_question_4, '') is null)::int
nullif returns null if the first value equals the second. The IS NULL condition returns a boolean (something that MySQL doesn't have) and that can be cast to an integer (where false will be cast to 0 and true to 1)
So the whole expression should be:
nb_question_repondu = 9 - (
(nullif(t1.valeur_question_4,'') is null)::int
+ (nullif(t1.valeur_question_6,'') is null)::int
+ (nullif(t1.valeur_question_7,'') is null)::int
+ (nullif(t1.valeur_question_9,'') is null)::int
)
Another option is to unpivot the columns and do a select on them in a sub-select:
update reponse_question_finale
set nb_question_repondu = (select count(*)
from (
values
(valeur_question_4),
(valeur_question_6),
(valeur_question_7),
(valeur_question_9)
) as t(q)
where nullif(trim(q),'') is not null);
Adding more columns to be considered is quite easy then, as you just need to add a single line to the values() clause
I need to get some data from a column.
For example I have data in a column like peter#msn.com. I want to get msn.com.
How I can I get that?
try this:
DECLARE #a VARCHAR(100) = 'peter#msn.com'
SELECT RIGHT(#a, LEN(#a) - CHARINDEX('#', #a))
SELECT SUBSTRING(Columnname,7,LEN(Columnname));