Updating value in temporary table using select statement - sql

Insert into #Temp(Rownumber, Percentage)
select RowNumber,
(MatchFirstName.PercentMatch * FunctionWeights.FunctionWeight)
from dbo.MatchFirstName(#FirstName), dbo.FunctionWeights
where FunctionWeights.FunctionName = 'MatchFirstName'
In FunctionWeights table, I have weights column and FunctionName column that stores different function names.
dbo.MatchFirstName(#FirstName) is the TVF here. Now , I have a table called FunctionWeights that stores a constant value corresponding to these function names.
Before the value is inserted into Temp table in Percentage column, I want it to retrieve the constant value corresponding to the function name that select statement has and multiply it which percentage value which was retrieved from the function. How can I do this?
With above query, multiplied value is not retrieved in the percentage column of Temp table but only the PercentageMatch value from TVF.
SNAPSHOTS:
FunctionWeights Table
select * from #Temp after insert
The percentage column should have 100 multiplied by the value from FunctionWeights.FunctionWeight. But instead it has 100 in all columns.

Your FunctionWeight for MatchFirstoName is 1, so multiplying with it doesn't make any difference. If MatchFirstName returns 100 in PercentMatch then all rows will be 100.
If you have the fixed function names for the select, you could just fetch the FunctionWeight into a variable before the select.

May be you should give an alias to your TVF and use it to access the column from it, because the reference to the TVF in the from clause includes parameters for it where as the one in the Select clause doesn't.
Insert into #Temp(Rownumber, Percentage)
select RowNumber,
(T.PercentMatch * FunctionWeights.FunctionWeight)
from dbo.MatchFirstName(#FirstName) T, dbo.FunctionWeights
where FunctionWeights.FunctionName = 'MatchFirstName'

Related

SQL - check if column has a value for 200 columns

i have a table which has 200 colums, i want to check if every single of this column has a value and skip them in my select query if they do not.
Not sure how to use select function in this example
Select * from A;

How to create a column in SQL containing using the AVG() function?

I have a table with one column and 46 rows and I want to create another column that contains the average of the first column in all 46 rows
e.g.
This is the table:
CREATE TABLE table2
SELECT column1
FROM table1
I want to add another column that contains for each row (46 rows) the value of AVG(column1)
How do can it be done?
You wouldn't use create table to add a column. In this case, a query using a window function is sufficient:
SELECT t1.*, AVG(column1) OVER () as avg_column1
FROM table1 t1;
This is Standard SQL and should work in any database.

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

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);

MS SQL Computed column

I want to create a column based on COUNT(*) on another table, and when a record is deleted from that table it should decrease the value in this new column and vice versa. So, here is the query:
SELECT COUNT (*) FROM dbo.Korisnik1_FakturaStavka GROUP BY dbo.Korisnik1_FakturaStavka.FakturaID
And it returns this:
And when I try to create a computated column like this:
CREATE TABLE test(
NumberOF as (SELECT COUNT (*) FROM dbo.Korisnik1_FakturaStavka GROUP BY dbo.Korisnik1_FakturaStavka.FakturaID) )
I get the following error:
Subqueries are not allowed in this context. Only scalar expressions are allowed.
Here is the main table that I want to compute from:
How can I resolve this ?
You can define a UDF:
create function dbo.NumberOfFakturaID(#id int) returns int as begin
return (select count(1) from Korisnik1_FakturaStavka where id=#id)
end
and then use it as the computed column:
CREATE TABLE test(FakturaID int, NumberOF as dbo.NumberOfFakturaID(FakturaID))
But putting that sort of calc as a computed column should be used with care.
This is too long for a comment.
You can do this by defining a function to calculate the count and using that function in the computed column definition. However, I don't think this is a good idea for frequently used columns, because you will be doing a lot of counting "behind the scenes".
Alternatives:
Set up a view or materialized view with the additional count column.
Do the count explicitly when you need it.
Set up a trigger to store the count in the first table, whenever rows are inserted/updated/deleted from the second table.

sql server add a row with same records except for ID

My code is as below"
select * into tbltemp
from table1 where ID='12345'
update tbltemp set ID='54321'where ID='12345'
insert into table1
select * from tbltemp where ID='54321'
drop table tbltemp
When executing insert into query, I got error saying 'Insert Error: Column name or number of supplied values does not match table definition.'
I wonder how I can deal with that?
My table1 has 50 columns with three computed columns.
Thanks for advice!
table1 and tbltemp must match by number of columns. You must explicitly name the columns do not use the * sign in insert into from select, if number of columns do not match.
I just realized that when I have computed columns, the query doesn't work well.
So I delete the computed columns before copy a new one, then do an insert into select *, then add computed columns back, in this way I can save the time for writing 50 fields.
You can't insert a computed column. You need to select especific fields in select and in value() statements. No select *