+ 1

How do sql variables work?

I have a table with salary of multiple employees in a table. and i need to calculate zscore for these emplyees using a derived number (mean and stddevn), i know i can do this in the select statement. but if i wanted to use variables. i am new to sql and just trying out things here. please bare me if my question is stupid declare @Weight int, @weight2 int, @Mean int, @StdDevn int, @Variance int set @Weight = 30 set @weight2 = (POWER(@weight,2)/100) select @Mean = AVG(salary) FROM [live].[dbo].[ProdKey] select @StdDevn = STDEV(salary) FROM [live].[dbo].[ProdKey] select @Variance = (POWER(@StdDevn,2)/100) select * FROM [live].[dbo].[ProdKey], (([salary]-@Mean)/@StdDevn) as Zscore Error desc - Must declare variable @Mean, Incorrect syntax near '-'

5th Sep 2017, 6:15 AM
rakesh c
rakesh c - avatar
2 Antworten
+ 1
fixed it:) my final select statement was wrong. should have been select *, (...) as zscore from table name. it was earlier written as select * from table name, (...) as zscore
5th Sep 2017, 8:04 AM
rakesh c
rakesh c - avatar
+ 5
i prefer select statement ... that will be very easy , no idea about ur way of doing this 😕
5th Sep 2017, 7:50 AM
Changed
Changed - avatar