How to create a calculated column that computes the non-null value from one of two columns - sql

I am creating a new database that requires a calculated column that pulls a percentage from one column (GrossMarginPercentage) and multiplies it by either an estimated value or an actual value column. Only one will contain a value and the other will be null.
Would any functions help me tell the new computed column what column (estimated or actual) to pull from and multiply by GrossMarginPercentage?
I tried:
Alter Table ChurnInfo
add DecMargin as case (when DecEstimated = 'Null' then
DecActual * GrossMarginPercentage else DecEst*GrossMarginPercentage end )
Solution: https://stackoverflow.com/a/56657223/11073192

I think you just want coalesce():
Alter Table ChurnInfo
add DecMargin as (coalesce(DecActual, DecEst) * GrossMarginPercentage);

Related

Copying data from one column to another in the same table sets data to null in original column

I created a new column [LastLoginDate-NoTime] with the data type Date. I already have another column [LastLoginDate] that is of Datetime datatype.
Columns with the values
I am trying to copy values from the LastLoginDate column to the LastLoginDate-NoTime column using this query:
UPDATE [dbo].[SapUsersExt]
SET [LastLoginDate] = [LastLoginDate-NoTime]
But the problem I am having is that when I execute this query, it sets the data to null in the original column.
Screenshot: Error
I am also trying to convert the data from the LastLoginDate to just date format in the new column LastLoginDate-NoTime so that I can use it in my application. How would I do that?
I am trying to copy values from the LastLoginDate column to the LastLoginDate-NoTime column using this query
In that case, you're doing it exactly backwards - you should use this SQL instead:
UPDATE [dbo].[SapUsersExt]
SET [LastLoginDate-NoTime] = [LastLoginDate]
The first column - right after the SET - is the target column into which your values will be written.
The second column, after the = symbol, is where the data comes from (column or expression).
You had it backwards - setting the column with the actual values, to all NULL ....
This of course only works for a "one time" update - this will not keep your columns in sync over time, when new data is being inserted. For such a case, you'd need a computed column
ALTER TABLE dbo.SapUsersExt
ADD LastLoginDateOnly AS CAST(LastLoginDate AS DATE) PERSISTED;
or a trigger.
Or maybe, you don't even really need to actually store that date-only value - just use
SELECT
CAST(LastLoginDate AS DATE),
.......
if you need to date-only value from LastLoginDate

Update column B based on Column A for all rows in table

I need to insert hash value into column b based on value of column a, but I need to do this for every row in table.
I always get this error no matter what I tried:
ERROR: more than one row returned by a subquery used as an expression
I have been trying different versions of the following:
UPDATE table
SET column b = md5((SELECT column a FROM table))
WHERE column a IS NOT NULL;
Any suggestions on how to perform this operation?
No need for a subquery here. As I understand, you want to store the checksum of column_a in column_b. As one would expect, Postgres' md5() function expects a single, scalar argument of string datatype, so:
UPDATE table
SET column_b = md5(column_a)
WHERE column_a IS NOT NULL;
Note that it would probably be simpler to use a computed column (available in Postgres 12) to store this derived information.

SQL function in column

I have a table in my SQL database. It has already some data and I have to add a new column. The value of the column should be created by a SQL function I'd definded. The parameters are two values from the table.
When I SELECT the data it's to late to call the function.
So I have value1 and value2 in my table. And I have a third column which should calculated like this function(value1, value2).
I only found solutions which execute the function at the SELECT.
Can anyone help me how to do this?
Use a computed column:
alter t add function_column as (function(value1, value2));
This will be calculated when it is accessed -- which means every time you use it. If you want the value calculated only once, then you would need to add a new column and a trigger to keep it up-to-date.

How to create a new column in query then populate with a simple calc X*Y

I have two columns. One is MW and the other is Price. I want to create a sql query that creates a third imaginary column called AwardPrice, and within that it will be one column (MW) * another column (Price). These two columns already exist in the same fact table.
I would recommend a computed column:
alter table t add AwardPrice as (MW * Price);
This value is calculated when you query the table, so the value is always accurate and up-to-date.
How to create a new column in query . . .
I want to create a sql query that creates...
I suppose that you want to do that in a SELECT statement as
SELECT *, MW * Price As AwardPrice
FROM YourTableNameHere;

Adding a new column in a temporary table

I have a temporary table in a PostgreSQL function and I want to insert a new VARCHAR column. It should have a value that depends on another column of the table, named "amount".
When the amount is positive I would like the value of the column row to be credit and when the value is negative the column should be debit.
I have one more request: I want to round the value of amount column in 2 decimal digits
You want ALTER TABLE ... ADD COLUMN followed by an UPDATE.
I initially said ALTER TABLE ... ADD COLUMN ... USING but that was wrong on two counts. ADD COLUMN takes a DEFAULT not USING - and You can't do it in one pass because neither a DEFAULT expression nor a USING expression may not refer to other columns.
So you must do:
ALTER TABLE tablename ADD COLUMN colname varchar;
UPDATE tablename SET colname = ( CASE WHEN othercol < 0 THEN 'Credit' ELSE 'Debit' END );
Think carefully about whether zero should be 'Debit' or 'Credit' and adjust the CASE accordingly.
For rounding, use round(amount,2). There isn't enough detail in your question for me to be sure how; probably by UPDATEing the temp table with UPDATE thetable SET amount = round(amount,2) but without the context it's hard to know if that's right. That statement irreversibly throws information away so it should only be used on a copy of the data.