Get dynamic column names from another table for SQL Update - sql

I have a temp table like below:
And a main table with the below columns:
ACC_NO, MTH1, MTH2, MTH3, MTH4, MTH5, MTH6, MTH7, MTH8, MTH9, MTH10, MTH11, MTH12, SELECTED
I would like to update the main table for only the column names exists in the temp table. In this case, my desired output would be:
Update table_name
Set MTH2 = some_value,
MTH4 = some_value,
MTH5 = some_value,
MTH6 = some_value,
MTH7 = some_value
Where selected = 'Y'

For the revised question, you can use case expressions:
Update table_name
Set MTH2 = (CASE WHEN EXISTS (SELECT 1 FROM temp tt WHERE tt.monthperiod = 'MTH2') THEN some_value ELSE mth2 END),
MTH4 = (CASE WHEN EXISTS (SELECT 1 FROM temp tt WHERE tt.monthperiod = 'MTH4') THEN some_value ELSE mth4 END),
. . .
Where selected = 'Y'

Related

Moving a cell value to another row based on ID numbers

I am looking to move B to the above row. It can either be placed where the Null value is in Column B or another column can be created. The value of B is linked to value A through an ID. The ID for value B is always X + 2 (the values in the ID column are integers).
I can’t just move the value up because the table I am working with has thousands of rows. It must be linked to the ID’s.
Please let me know if you have any questions. Any assistance is much appreciated. Thank you.
ID
Column A
Column B
X
A
NULL
X+2
NULL
B
Keep in mind I am very new to SQL. Below is what I tried. It created a new column that only contains NULL values.
Select
Column_B
From
Table_Name
Where
Table_Name.ID = Table_Name.ID +2 ) AS Col_B_Value
You can use a conditional subselect for that
UPDATE Table_Name T1
SET Column_B = (Select
Column_B
From
Table_Name
Where
Table_Name.ID = T1.ID +2 )
WHERE Column_B IS NULL
Some databases could have a problem so you can make
UPDATE Table_Name T1
SET Column_B = (Select
T2.Column_B
From
(SELECT ID,Column_B FROM Table_Name) T2
Where
T2.ID = T1.ID +2 )
WHERE Column_B IS NULL
You could just do it with 2 updates statements
UPDATE Table
SET Column B = 'B'
WHERE ID = 'X'
UPDATE Table
SET Column B = NULL
WHERE ID = 'X+2'
If you need to do it in a select statement you could do it with a case statement too
SELECT ID,
Column A,
CASE WHEN ID = X AND Column B = NULL THEN 'B'
ELSE Column B END
FROM Table

Oracle SQL Queries - Change Field Value

How can I change field value with my query?
I would like to see “TR” when it is ‘TÜRKEI’ in the database.
SELECT * FROM LAND WHERE LNDNR_LAND = '111';
D XXX 01/01/0001 111 TÜRKEI
Try this
select col1,col2,...., case when colN ='TÜRKEI' then 'TR' else colN end as colN
from your_table
Assuming column name is col and you
you can use SQL Standard case:
select t.*, case when col = 'TÜRKEI' then 'TR' else col end as col_new
from your_table t;
or Oracle specific decode:
select t.*, decode(col, 'TÜRKEI', 'TR', col) as col_new
from your_table t;
If you want to update the table with the value, you can simple update
update your_table
set col = 'TR'
where col = 'TÜRKEI';
If you want to change the value in database then you have use Update statement.
The SQL UPDATE statement is used to update existing records in the tables.
e.g
update tablename
set column1 = value
where condition;

Select statement to return constant when no records found in table in SQL Server 2008

I am have a table with data and now i need to return zero in select statement if there is no records in table for example. I need to use it in Stored Procedure.
-- If no records exists in below select statement
SELECT ID,Text,Date FROM tblData WHERE ID = 12
IF (##ROWCOUNT = 0)
BEGIN
SELECT -5 AS ID
END
Output:
ID Text Date
ID
-5
Expected output
ID
-5
If you want to return 1 row even when there is no match, you can use aggregation:
SELECT (CASE WHEN COUNT(*) = 0 THEN -5 ELSE MAX(ID) END) as ID
FROM tblData
WHERE ID = 12;
I always use an Exists statment.
if exists(SELECT ID FROM tblData WHERE ID = 12)
select 0 as RowsExist
else
select 1 as RowsExist
For a single scalar value you could use something like;
SELECT ISNULL((SELECT ID FROM tblData WHERE ID = 12), 0) as ID
Rhys
SELECT (CASE WHEN Ta.ID IS NULL THEN TBL.ID
ELSE Ta.ID END) AS ID,Ta.Text,Ta.Date
FROM (VALUES(-5)) AS TBL(ID)
LEFT JOIN
(
SELECT ID,Text,Date FROM tblData WHERE ID = 12
)
AS Ta ON Ta.ID = Ta.ID

oracle sql - compare row 1 and two, show results in row 3?

My table holds type 2 change history; so if recordID 123 has a change made by a user, then 'latest_effective' gets set to FALSE and the newly created record with recordID 123 has 'latest_effective' set to TRUE.
I'm looking for a query that returns the new record & the record that was just changed with a third row that is TRUE if there is a change in that column and FALSE if not.
select RecordID,1 latest_effective,Column1,Column2, etc...
from YourTable
where latest_effective = 1
union all
select RecordID,0 latest_effective,Column1,Column2, etc...
from YourTable
where latest_effective = 0
union all
select t.RecordID,-1 latest_effective,
case t.Column1 when f.Column1 then 'FALSE' else 'TRUE' end AS Column1
case t.Column2 when f.Column2 then 'FALSE' else 'TRUE' end AS Column2, etc...
from YourTable AS t
inner join YourTable AS f
on f.RecordID = t.RecordID
and f.latest_effective = 0
where t.latest_effective = 1
order by RecordID,latest_effective desc

In this example can UPDATE statements be combined?

I quite often have rows of code like the following:
UPDATE my_table SET name = 'x' WHERE Original = 'a'
UPDATE my_table SET name = 'y' WHERE Original = 'b'
UPDATE my_table SET name = 'z' WHERE Original = 'c'
UPDATE my_table SET name = 'k' WHERE Original = 'd'
UPDATE my_table SET name = 'm' WHERE Original = 'e'
UPDATE my_table SET name = 'n' WHERE Original = 'f'
Can I combine/shorten this code into one UpDate statement - or are they best just left as they are?
UPDATE my_table
SET name =
CASE
WHEN Original = 'a' THEN 'x'
WHEN Original = 'b' THEN 'y'
...
END
That will update EVERY row. So if there's an Original value you haven't specified, it will be set to NULL. So you might want to limit the update to just those you want to update, with a WHERE clause, like so:
WHERE Original IN ('a', 'b', ...)
OR, as an alternative, you could use an ELSE statement, which leaves the name value as is, if it doesn't have a match in the WHEN statements, like so:
CASE
WHEN Original = 'a' THEN 'x'
WHEN Original = 'b' THEN 'y'
...
ELSE name
END
You could use a case statement:
UPDATE my_table
SET name =
case Original
when 'a' then 'x'
when 'b' then 'y'
...
else name -- Preserve original
end
The else clause makes sure you're not modifying a name if it's not matched in the case.
You could use a table value constructor and a from clause, if these values aren't already in a table:
update mt set name = t.name
from
my_table mt
inner join
(values
('a','x'),
('b','y'),
('c','z'),
('d','k'),
('e','m'),
('f','n')
) t(original,name)
on
mt.Original = t.original