How do I multiply and add columns? - sql

I basically want to do this function as a new column.
New Column = a1*a2 + b1*b2 + c1*c2
where a1,a2,b1,b2,c1,c2 are all existing columns with numerical values in each cell. Any help would be appreciated

Please check columns a1,a2,b1,b2,c1,c2 has "type" is bigint;
And use QSL:
ALTER TABLE demo.abc ADD COLUMN d bigint;
Update demo.abc set d = abc.a1 * abc.a2 + abc.b1 * abc.b2 + abc.b1*abc.c2;

Related

Reusing calculated column in update in same code-block

I am trying to find the (best) way to update column of table based on calculated another column.
First of all, I am using function to calculate value for 'BAS_CBR_EB_RWA' column and then I am using 'BAS_CBR_EB_RWA' value as an input for 'BAS_CBR_EB_TOTAL_CAPITAL' column calculation, as shown in below-mentioned code.
Could you please share how to reuse it to do next calculation. replacing 'BAS_CBR_EB_RWA' with right-hand calculation isn't desirable because we have too many calculation of this type and it'll confuse other users.
Thanks in advance for help.
IF INSTTABLE = 16 THEN
UPDATE LAO_DATA
SET BAS_CBR_EB_RWA = BAS2_RWA_CALC(BAS_CAPITAL_CALC_CD,
CBR_CUR_BOOK_BAL,
BAS_CAP_FACTOR_K,
V_BASEL_MIN,
V_BAS_RWA_RATE),
BAS_CBR_EB_TOTAL_CAPITAL = ROUND(BAS2_MGRL_CAPITAL(V_DATE,
BAS_CBR_EB_RWA,
0),
2),
WHERE (AS_OF_DATE = V_DATE);
--COMMIT;
END IF;
In Oracle, you can update a subquery. I'm not 100% sure if it works for UDFs, but you can try:
UPDATE (SELECT LD.*,
BAS2_RWA_CALC(BAS_CAPITAL_CALC_CD, CBR_CUR_BOOK_BAL, BAS_CAP_FACTOR_K, V_BASEL_MIN, V_BAS_RWA_RATE) as new_BAS_CBR_EB_RWA
FROM LAO_DATA LD
)
SET BAS_CBR_EB_RWA = new_BAS_CBR_EB_RWA,
BAS_CBR_EB_TOTAL_CAPITAL = ROUND(BAS2_MGRL_CAPITAL(V_DATE, nw_BAS_CBR_EB_RWA, 0), 2),
WHERE AS_OF_DATE = V_DATE;
A MERGE statement can be used. You may also replace ROWID with the primary key or a Unique key of the table.
Put all your first level of calculations with function calls inside the USING() block and the second level of calculation for RHS in the SET expression
MERGE INTO lao_data t
USING (
SELECT ROWID AS rid,bas2_rwa_calc(bas_capital_calc_cd,
cbr_cur_book_bal,
bas_cap_factor_k,
v_basel_min,v_bas_rwa_rate
) AS new_BAS_CBR_EB_RWA
FROM lao_data
WHERE as_of_date = V_DATE
)
s ON ( s.rid = t.rowid )
WHEN MATCHED THEN UPDATE
SET t.bas_cbr_eb_rwa = s.new_BAS_CBR_EB_RWA
t.bas_cbr_eb_total_capital
= round(bas2_mgrl_capital(v_date,s.nw_BAS_CBR_EB_RWA,0), 2) );

Custom user autonumber

I have been searching for a solution to this autonumber problem that I am having. There seems to be no definite answer anywhere.
I have a form which has a text field.
I want this form to display the next number from a field in a table.
Example: the table contains 3 records with the values D001, D002, D003
The form is used to enter new records (new data). So next time I enter data I want D004 to automatically show up on the text field for data code in the form.
How can this be done?
You can use the BeforeInsert event of the form:
Me!AutoNumber.Value = Format(Val(Nz(Right(DMax("[AutoNumber]", "[YourTable]"), 3), 0)) + 1, "\D000")
That is 1 way to do this is create a function to handle the autonumber problem u had
create function NextAutoNumber()
returns char(4)
as
begin
declare #lastval char(4)
set #lastval = (select max(autoNumber) from table)
if #lastval is null set #lastval = 'D001'
declare #i int set #i = right(#lastval,3) + 1 return 'C' + right('00' + convert(varchar(10),#i),3)
end
like this you can just call the function anytime and insert the auto number you need for the record

How to add corresponding list of values to SQL parameters

How do I make the query like this?
UPDATE DUA_DATA_FIL_AUD
SET REV = :rev,
SYS_UPDT_TS = :now
WHERE DUA_DATA_FIL_ID = 283
AND REV = 2524;
And so on for all the next 13 records and update all the corresponding columns?
Create a result of dynamic update queries using a select form the table you want to apply the updates on it's result set.
This shall create you a sort of script that you can copy and run in your command window to update the desired lines.
Hope this addresses what you really want :
SELECT 'UPDATE DUA_DATA_FIL_AUD SET REV = :rev, SYS_UPDT_TS = :now WHERE
DUA_DATA_FIL_ID =' || DUA_DATA_FIL_ID || 'AND REV =' || MAX(REV) || ';/'
FROM DUA_DATA_FIL_AUD GROUP BY DUA_DATA_FIL_ID,REV

MS-SQL - Extracting numerical portion of a string

I have an MS-SQL table, with a column titled 'ImportCount'.
Data in this column follows the below format:
ImportCount
[Schedules] 1376 schedule items imported from location H:\FOLDERA\AA\XX...
[Schedules] 10201 schedule items imported from location H:\FOLDERZZ\PERS\YY...
[Schedules] 999 schedule items imported from location R:\PERS\FOLDERA\AA\XX...
[Schedules] 21 schedule items imported from location H:\FOLDERA\MM\2014ZZ...
What I would like to do is extract that numerical portion of the data (which varies in length), but am struggling to get the right result. Would appreciate any help on this!
Thanks.
Try
select left(ImportCount, patindex('%[^0-9]%', ImportCount+'.') - 1)
select SUBSTRING(ImportCount,13,patindex('% schedule items%',ImportCount)-13) from table name
Try this..You can declare it as a SQL function also.
DECLARE #intText INT
DECLARE #textAplhaNumeric varchar(100)
set #textAplhaNumeric = '1376 schedule items imported from location'
SET #intText = PATINDEX('%[^0-9]%', #textAplhaNumeric)
BEGIN
WHILE #intText > 0
BEGIN
SET #textAplhaNumeric = STUFF(#textAplhaNumeric, #intText, 1, '' )
SET #intText = PATINDEX('%[^0-9]%', #textAplhaNumeric)
END
END
Select #textAplhaNumeric //output is 1376
It will work in case of NULL or empty values.
Please try:
SELECT LEFT(Val,PATINDEX('%[^0-9]%', Val+'a')-1) from(
SELECT
STUFF(ImportCount, 1, PATINDEX('%[0-9]%', ImportCount)-1, '') Val
FROM YourTable
)x

Dynamic XML node name in SQL XML

I am updating an XML column with values from columns in a temp table. I can update the table as below.
UPDATE tbWorkflow
SET xmlData.modify('insert
(<FromQueueName>
<CustomerID>{ sql:column("T.iVTollCustID") }</CustomerID>
<Date>{ sql:variable("#CurrDateTime") }</Date>
</FromQueueName>)
as first into (/configuration)[1]'),
vcQueue = 'qVtoll',
dtUpdTime = GETDATE()
FROM #Trxns T
WHERE T.biWorkflowID = tbWorkflow.biWorkflowID
However, I want the node name to be dynamic (from the temp table) like below. But it does not work.
UPDATE tbWorkflow
SET xmlData.modify('insert
(<**{ sql:column("T.vcQueue") }**>
<CustomerID>{ sql:column("T.iVTollCustID") }</CustomerID>
<Date>{ sql:variable("#CurrDateTime") }</Date>
</**{ sql:column("T.vcQueue") }**>)
as first into (/configuration)[1]'),
vcQueue = 'qVtoll',
dtUpdTime = GETDATE()
FROM #Trxns T
WHERE T.biWorkflowID = tbWorkflow.biWorkflowID
Any help is appreciated.
I think, you should use a subquery to select a string you need and then insert it into XML.
For example, in subquery get a <YourTag><CustomerID>123</CustomerID><Date>15.10.2012</Date></YourTag> and then you can simply insert it in XML
I found a workaround to do this.
I added another VARCHAR column to my temp table, created the xml node as a varchar and later modified my actual xml column using this varchar column.
UPDATE #Trxns
SET xmlVarchar = '<' + vcQueue + '>
<CustomerID>' + CONVERT(VARCHAR(10),iVTollCustID) + '</CustomerID>
<Date>' + CONVERT(VARCHAR(25), GETDATE(),22) + '</Date>' +
'</' + vcQueue + '>'
UPDATE tbWorkflow
SET xmlData.modify('insert
sql:column("xmlVarchar")
as first into (/configuration)[1]'),
vcQueue = 'qVtoll',
dtUpdTime = GETDATE()
FROM #Trxns T
WHERE T.biWorkflowID = tbWorkflow.biWorkflowID