Comparing two column values in postgres - sql

I have table having following columns and records. I need to compare the two column values(ColumnA and ColumnB), if ColumnB>ColumnA then and update the third column from 'N' TO 'Y'
CREATE TABLE Test(ColumnA int,ColumnB int,Result Varchar(2))
INSERT INTO Test values(1,3,'N')
INSERT INTO Test values(2,1,'N')
INSERT INTO Test values(1,5,'N')
INSERT INTO Test values(8,7,'N')
I need to update Result Column='Y' for first and third row because columnB>ColumnA
Result
ColumnA ColumnB Result
1 3 Y
2 1 N
1 5 Y
8 7 N

This can be done with a simple CASE expression:
update test
set result = case
when columna > columnb then 'Y'
else 'N'
end
;
Online example: https://rextester.com/ZHIUZD82060
I would recommend to use a boolean column instead of a varchar to store "yes/no" flags. Then the update becomes as simple as set result = column_a > columnb

update test
set result =
case
when columnb > columna then 'Y'
else 'N'
end;
Hope this will help.

Related

SQL QUERY regarding single row as output

I want a query in which it only returns only one output row based on the values in a column.
What I need is
Table A has values
A
----|----
1 |
2 |
3 |
required output
A
----
yes
so I need a query in which if table a has 3 in its column then the output I need to get is yes or else no.
my code:-
WITH
number_tb AS (select * from t1),
out_put as (select case when a = 3 then 'yes' else 'no' end a_case from number_tb)
select * from out_put
output:-
a_case
-----
no
no
yes
I only need single row output. if 3 is present then yes or no, I don't need it for each row.
Is it possible to do so??
If a proper boolean true/false value is also acceptable, you can use
select exists (select * from t1 where a = 3) as a;
If you want a string with yes/no instead you can use a CASE expression to turn the boolean value into a string.
select case
when exists (select * from t1 where a = 3) then 'Yes'
else 'No'
end as a;

Is there a way to create a new column and assign values based on criteria on an existing column in a SQL query?

I want to create a new column in my query output and assign values to it based on existing columns in my input. Let's say i have column A in my input and want to create column B in my query output. How do I add the following logic in a sql select statement?
If A = 1 then set B to x
Else if A = 2 then set B to y
Else if A = 3 then set B to z
Else set B to null
I have to do this for multiple new columns in my output within the same query.
This is a case expression:
select . . .,
(case A when 1 then 'x' when 2 then 'y' when 3 then 'z' end) as b
Something like this?
SELECT A,
CASE
WHEN A = 1 THEN 'x'
WHEN A = 2 THEN 'y'
ELSE 'z'
END AS B
FROM ...

SQL Update all rows for one column with a list of values

How can I set all rows of a column by manually typing the values?
My table has 3 rows, I want the column named mycolumn to have 3 values a, b and c (currently those values are NULL):
update mytable set mycolumn = ('a','b','c')
ORA-00907 missing right parenthesis
EDIT: my table is very simple, I have one column ID INT NOT NULL with values 1, 2, 3 and another column mycolumn with all NULL values and I want those values to become 'a' where ID = 1, 'b' where ID=2 etc.
EDIT2: I might have a huge amount of rows, so I want to avoid typing every single ID value where to replace mycolumn. Isn't it possible to match the ID values of 1 to 3 to the values 'a', 'b', 'c' in an automatic way, something like match(ID, ('a','b','c')) perhaps
I just want to replace all values of mycolumn by increasing order of ID. ID being strictly equivalent to what I call a row number in a matrix
EDIT3: I'd like a solution which would work in a general case with all sorts of values, not only the letters of the alphabet given here for simplicity. What if for example my values to replace in mycolumn are ('oefaihfoiashfe', 'fiaohoawdihoiwahopah', 'aefohdfaohdao')? However the ID row numbers will always be a sequence from 1 to N by 1.
Obviously, you should do this in a single update. Like this:
update mytable
set mycolumn = case id when 1 then 'a' when 2 then 'b' when 3 then 'c' end
;
More compact (but also more cryptic, and only works in Oracle, while case expressions are in the SQL standard):
update mytable
set mycolumn = decode(id, 1, 'a', 2, 'b', 3, 'c')
;
Note - this only works if there really are only three rows. If you have many more rows, make sure to add where id in (1, 2, 3) at the end. Otherwise all the OTHER values (in the other rows) will be updated to null!
You can try an update like the one below. This will update 1 > a, 2 > b, 3 > c, 4 > d, etc. When you reach ID 27, since there are no more letters, it will begin at a again and continue down the alphabet.
UPDATE mytable
SET mycolumn = CASE MOD (id, 26)
WHEN 0 THEN 'z'
ELSE CHR (MOD (id, 26) + 96)
END;
Update
To update based on any list of values, you can try an update statement like the one below. If you add a 4th item to the comma delimited list, ID 4 in mytable will be set to whatever you specified as the 4th value.
UPDATE mytable
SET mycolumn =
(SELECT COLUMN_VALUE
FROM (SELECT ROWNUM AS row_num, t.COLUMN_VALUE
FROM TABLE (
sys.odcivarchar2list ('oefaihfoiashfe',
'fiaohoawdihoiwahopah',
'aefohdfaohdao')) t)
WHERE row_num = id);
Hmmm . . . A row can only have one value. Perhaps something like this to assign random values:
update mytable
set mycolumn = (case floor(dbms_random.random * 3)
case 0 then 'a' case 1 then 'b' else 'c'
end)
if you want the 3 rows to have different values a, b and c then you will have to write 3 update statements.
update mytable set mycolumn = 'a' where id = 1;
update mytable set mycolumn = 'b' where id = 2;
update mytable set mycolumn = 'c' where id = 3;

Conditional value in sqlite column

I have two integer columns in my sqlite table: a and b. I need to create a third column, c, which should contain either Y if a+b mod 2 == 1 or N if the previous condition is not satisfied. I am not sure how to define such a column with a conditional value in my query.
You can do this readily in a query:
select a, b, (case when (a + b) % 2 = 1 then 'Y' else 'N' end) as col3
from table t;
You can do this in an update statement as well:
update t
set col3 = (case when (a + b) % 2 = 1 then 'Y' else 'N' end) ;
You need to be sure that col3 exists. You can do that with alter table:
alter table t add column col3 int;

Alternative option for case condtion in sql

How to check the column value zero or not
I want to insert 50 column values, each column i want to check whethere the value is 0 or not. If the value is 0 then it should be null
Query
insert into table1 values (Case when column1 = '0' then null else column1 end, .....
Case when column50 = '0' then null else column50 end)
The above query is working, but query length is too long because i am using the above query of 50 column's
there is any alternative option is there for check the column value is 0 or not.
like this if(column1, 0) then null
Need Query Help
Try using NULLIF:
insert into table1 values (NULLIF(column1, '0'), .....
NULLIF(column50, '0'))