Add columns dynamically based on a condition in SQL - sql

I'm using SSMS 2012. Consider a table A with columns A, B, C. I would like to dynamically add a new column, D to this table based on a condition in one of the existing columns. How can this be achieved?
Query looks like the following,
select col1, col2 from #temptbl
To this #temptbl, I would like to add a column, col3 and the value in col3 is based on a condition on col2.

To add a column, you need to ALTER the table
Let say you want to add an integer for COL3
ALTER TABLE #temptbl
ADD col3 INTEGER
And to update the value in col3 based on a condition on col2, you need to do an UPDATE and use a CASE statement to check the condition :
Let say you want to set value 3 in COL3 if COL2 is equal to 2
UPDATE #temptbl
SET col3 = CASE WHEN col2 = 2 THEN 3 ELSE null END

After your edit it is still incredibly vague what you want to do but maybe this is close??? No idea what the condition or the output should be but when the condition is not met the output of this will be NULL.
select col1
, col2
, case when SomeCondition then SomeValue end as Col3
from #temptbl

Like Tom mentioned, you can only had a column for the whole table, not on a conditional basis. You can make it a computed column or when you query the table, have a CASE statement if adding a column is not possible.

Related

How do i append query results to create new columns in dataset on bigquery?

Would like to alter my original dataset to include the results from the query. Currently year and month are connected, would like to split the string and append the results to the original dataframe
I see that you are sourcing the value for your new column from your own source table.
You can do it two ways:
Idea1:
Let's say you have a table with columns:
col1, col2
and you want to add col3, you can always do something like:
CREATE OR REPLACE table your_source_table as
select col1, col2, (your_calculation_for_col3) as col3 from your_source_table
Idea 2:
Add a new column to your table and update value of it like below:
ALTER TABLE your_source_table
ADD COLUMN COL3 DATA_TYPE_FOR_COL3;
UPDATE your_source_table
SET col3 = your_new_calculated_value
WHERE TRUE;
see if any of this helps.

How can we have column in oracle whose data is dependent on other column from other table and should honour all the updates on dependent table

Let's say I have two tables table A and table B and they have one to many relation.
Table A
col1 | Col2
Table B
Col1 | Col3
Now I want to have a Col4 in Table A such that if any row of Col3 is true for a given Col1 value, then i want Col4 in Table A to be set as true else false.
All the updates that happend on Table B should always update my Col4 values in Table A.
Is this achievable thing by using any oracle/PL-SQL features?
Use an on AFTER INSERT OR UPDATE trigger
CREATE TRIGGER sync_tables
AFTER INSERT OR UPDATE
OF col1
ON tableA
BEGIN
INSERT INTO tableB(col1) VALUES :NEW.col1
END;

SQL Server insert into next column

SQL Server 2012: is it possible to do a insert into select statement but insert data into the next column if the previous is already populated?
I have a table with a number of columns to store dates, I want to take data from another table and insert into the next available null date column.
If you are saying that the previous column is already populated, then you're really looking for some sort of update. Try this:
UPDATE yourTable
SET
col1 = CASE WHEN col1 IS NULL THEN 'value' ELSE col1 END,
col2 = CASE WHEN col1 IS NOT NULL THEN 'value' ELSE col2 END;
The logic here is what you described, namely that we attempt to update col1 with some value. Should that column be empty, we make the update, otherwise we update col2 instead.

SQL: How to sort rows on two columns but using conditions on those columns

I have a table with two columns. We'll call the table Table1 and the columns Col1 and Col2 which are both text columns.
Some rows will have data in Col1 while Col2 will be null. Then there are some rows where Col1 will be null and Col2 will have data. And finally, some rows will have data in both. No rows exist where both columns are null.
I want to read all the rows but the sorting needs to be as follows: If Col1 has data, then that column is used, regardless what is in Col2. If Col1 is null, it uses Col2.
I'm not even sure if generating this by sorting is even possible. Thanks for any help.
There are several ways to do it. Here's one:
order by coalesce(col1, col2)
....
order by
case when Col1 is not null then Col1
when Col1 is null then Col2
end

Can I set a formula for a particular column in SQL?

I want to implement something like Col3 = Col2 + Col1 in SQL.
This is somewhat similar to Excel, where every value in column 3 is sum of corresponding values from column 2 and column 1.
Have a look at Computed Columns
A computed column is computed from an
expression that can use other columns
in the same table. The expression can
be a noncomputed column name,
constant, function, and any
combination of these connected by one
or more operators.
Also from CREATE TABLE point J
Something like
CREATE TABLE dbo.mytable
( low int, high int, myavg AS (low + high)/2 ) ;
Yes, you can do it in SQL using the UPDATE command:
UPDATE TABLE table_name
SET col3=col1+col2
WHERE <SOME CONDITION>
This assumes that you already have a table with populated col1 and col2 and you want to populate col3.
Yes. Provided it is not aggregating data across rows.
assume that col1 and col2 are integers.
SELECT col1, col2, (col1 + col2) as col3 FROM mytable