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

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 ...

Related

postgreSQL getting column values as new column and adding

I have the following table
ident
name
count
A1
X
1
A1
Y
2
A1
X
6
A2
X
2
A2
Z
3
What i need is a new table which should look like:
ident
X
Y
Z
A1
7
2
0
A2
2
0
3
so it should give me for every distinct id a sum of all the existing names.
But the columns with are build out of the names should be build automaticly.
i have try
SELECT
ident ,
MAX(CASE WHEN (name = 'X') THEN 1 ELSE NULL END) AS X
FROM
mytable
GROUP BY ident
ORDER BY ident
but with this code i have to make the columns and just can set the sum to 0 or 1.
Thx
Use conditional aggregation, which in Postgres looks like this:
select ident,
sum(count) filter (where name = 'X') as x_sum,
sum(count) filter (where name = 'Y') as y_sum,
sum(count) filter (where name = 'Z') as z_sum
from mytable
group by ident;
The FILTER clause is Standard SQL, but Postgres is one of the few databases that actually supports it. You can, of course, do the same thing with CASE expressions, but FILTER gives the optimizer more hints that can help performance (and many people find it cleaner).
you are looking for sum of count:
SELECT
ident ,
SUM(CASE WHEN (name = 'X') THEN count ELSE NULL END) AS X,
SUM(CASE WHEN (name = 'Y') THEN count ELSE NULL END) AS Y,
SUM(CASE WHEN (name = 'Z') THEN count ELSE NULL END) AS Z
FROM
mytable
GROUP BY ident
ORDER BY ident

Comparing two column values in postgres

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.

Updating column B value case when conditional statement is true

I need to update Column C value to 1.5 when Column A = Column B Else it should be 4 in SQL Server.
I wrote something like this
UPDATE MyTable
SET Column C = 1.5 CASE WHEN Column A = Column B ELSE 4 END NewColumn.
I have never used UPDATE and CASE WHEN statement.
The correct syntax is:
UPDATE MyTable
SET C = (CASE WHEN A = B THEN 1.5 ELSE 4 END)
You can also use IIF() as
UPDATE MyTable
SET C = IIF(A=B, 1.5, 4);

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;

Return different rows for each column in a row

I have data which is presented in multiple rows and columns with 0 or 1 values. What I'm trying to do is create a unique row for each 1, but there are sometimes multiple 1's in a row. For ex:
**A B C D**
1 0 1 1
0 0 0 1
1 1 0 0
I would like to have return six rows, all in one column like so
**RETURN**
A
C
D
D
A
B
Thanks in advance!
You can do this with a union all statement:
select val
from ((select 'A' as val from t where A = 1) union all
(select 'B' from t where B = 1) union all
(select 'C' from t where C = 1) union all
(select 'D' from t where D = 1)
) t
As a note: I hope you have other columns that you can include in the output. SQL tables are, by definition, not ordered. So, you really have no idea in your example of the original source for any given value.