SQL Server 2008 View Automatically Renaming Aliases to Expr1, Expr2, etc - sql-server

My aliases keep getting renamed to Expr1, Expr2, etc. after saving my view. I have multiple CASE WHEN THEN statements in the view that have the same aliases in each case statement (X0,Y0 and Z0.) Essentially all case statements after the first case statement replaces my aliases with this Expr1, Expr2.
Any help would be extremely appreciated. I also tried putting square brackets around the aliases but that did not work.
I should also note that when I go to save, I get a warning from SQL Server Management Studio about my order by clause. It's probably unrelated but you never know.
SELECT
TOP (100) PERCENT
s.dtmEvaluation AS SurgeryDate,
p.idPatient,
d.strLead AS LeadType,
ds.strDBSSite AS Target,
CASE
WHEN intSide = 0 THEN 'L'
ELSE 'R'
END AS Side,
ROUND(s.dblACX, 2, 1) AS ACX,
ROUND(s.dblACZ, 2, 1) AS ACY,
ROUND(s.dblACZ, 2, 1) AS ACZ,
ROUND(s.dblPCX, 2, 1) AS PCX,
ROUND(s.dblPCY, 2, 1) AS PCY,
ROUND(s.dblPCZ, 2, 1) AS PCZ,
ROUND(s.dblInitX, 2, 1) AS InitialX,
ROUND(s.dblInitY, 2, 1) AS [Initial Y],
ROUND(s.dblInitZ, 2, 1) AS [Initial Z],
s.dblACPCAngle AS InitialACPCAngle,
s.dblCenterAngle AS InitialCLangle,
s.dblMicroPasses AS MicroPasses,
s.dblMacroPasses AS MacroPasses,
ROUND(s.dblFinalX, 2, 1) AS FinalX,
ROUND(s.dblFinalY, 2, 1) AS FinalY,
ROUND(s.dblFinalZ, 2, 1) AS FinalZ,
ROUND(s.dblMeasuredX, 2, 1) AS MeasX,
ROUND(s.dblMeasuredY, 2, 1) AS MeasY,
ROUND(s.dblMeasuredZ, 2, 1) AS MeasZ,
s.dblMeasuredACPCAngle,
s.dblMeasuredCtrAngle,
ROUND(SQRT(POWER(s.dblMeasuredX - s.dblFinalX, 2) + POWER(s.dblMeasuredY - s.dblFinalY, 2) + POWER(s.dblMeasuredZ - s.dblFinalZ,2)), 2, 1) AS Delta,
CASE
WHEN s.intSide = 1 THEN s.dblMeasuredx + ((dblelectrodelength - 1) + 0 * (dblelectrodespacing + dblelectrodelength) + (dblelectrodelength / 2)) * SIN(RADIANS(dblMeasuredCtrAngle))
ELSE dblMeasuredx - ((dblelectrodelength - 1) + 0 * (dblelectrodespacing + dblelectrodelength) + (dblelectrodelength / 2)) * SIN(RADIANS(dblMeasuredCtrAngle))
END AS X0,
s.dblMeasuredY +
(((d.dblElectrodeLength - 1)
+ 0 * (d.dblElectrodeSpacing + d.dblElectrodeLength)) +
d.dblElectrodeLength / 2) * COS(RADIANS(s.dblMeasuredACPCAngle))
* COS(RADIANS(s.dblMeasuredCtrAngle)) AS Y0,
s.dblMeasuredZ + (((d.dblElectrodeLength - 1) + 0 *
(d.dblElectrodeSpacing + d.dblElectrodeLength))
+ d.dblElectrodeLength / 2) *
SIN(RADIANS(s.dblMeasuredACPCAngle)) *
COS(RADIANS(s.dblMeasuredCtrAngle)) AS Z0,
CASE
WHEN intSide = 1 THEN dblMeasuredx +
((dblelectrodelength - 1) + 1 * (dblelectrodespacing +
dblelectrodelength) + (dblelectrodelength / 2))
* SIN(RADIANS(dblMeasuredCtrAngle))
ELSE dblMeasuredx -
((dblelectrodelength - 1) + 1 * (dblelectrodespacing +
dblelectrodelength) + (dblelectrodelength / 2))
* SIN(RADIANS(dblMeasuredCtrAngle))
END AS Expr1,
s.dblMeasuredY + (((d.dblElectrodeLength - 1) + 1 *
(d.dblElectrodeSpacing + d.dblElectrodeLength))
+ d.dblElectrodeLength / 2) *
COS(RADIANS(s.dblMeasuredACPCAngle)) *
COS(RADIANS(s.dblMeasuredCtrAngle)) AS Expr2,
s.dblMeasuredZ + (((d.dblElectrodeLength - 1) + 1 *
(d.dblElectrodeSpacing + d.dblElectrodeLength)) + d.dblElectrodeLength /
2)
* SIN(RADIANS(s.dblMeasuredACPCAngle)) *
COS(RADIANS(s.dblMeasuredCtrAngle)) AS Expr3,
CASE
WHEN intSide = 1 THEN dblMeasuredx + ((dblelectrodelength - 1)
+ 2 * (dblelectrodespacing + dblelectrodelength) +
(dblelectrodelength / 2)) * SIN(RADIANS(dblMeasuredCtrAngle))
ELSE dblMeasuredx - ((dblelectrodelength - 1)
+ 2 * (dblelectrodespacing + dblelectrodelength) +
(dblelectrodelength / 2)) * SIN(RADIANS(dblMeasuredCtrAngle))
END AS
Expr4,
s.dblMeasuredY + (((d.dblElectrodeLength - 1) + 2 *
(d.dblElectrodeSpacing + d.dblElectrodeLength)) + d.dblElectrodeLength
/ 2)
* COS(RADIANS(s.dblMeasuredACPCAngle)) *
COS(RADIANS(s.dblMeasuredCtrAngle)) AS Expr5,
s.dblMeasuredZ +
(((d.dblElectrodeLength - 1)
+ 2 * (d.dblElectrodeSpacing + d.dblElectrodeLength)) +
d.dblElectrodeLength / 2) * SIN(RADIANS(s.dblMeasuredACPCAngle)) *
COS(RADIANS(s.dblMeasuredCtrAngle))
AS Expr6,
CASE
WHEN intSide = 1 THEN dblMeasuredx +
((dblelectrodelength - 1) + 3 * (dblelectrodespacing +
dblelectrodelength) + (dblelectrodelength / 2))
* SIN(RADIANS(dblMeasuredCtrAngle))
ELSE dblMeasuredx -
((dblelectrodelength - 1) + 3 * (dblelectrodespacing +
dblelectrodelength) + (dblelectrodelength / 2))
* SIN(RADIANS(dblMeasuredCtrAngle))
END AS Expr7,
s.dblMeasuredY + (((d.dblElectrodeLength - 1) + 3 *
(d.dblElectrodeSpacing + d.dblElectrodeLength))
+ d.dblElectrodeLength / 2) *
COS(RADIANS(s.dblMeasuredACPCAngle)) *
COS(RADIANS(s.dblMeasuredCtrAngle)) AS Expr8,
s.dblMeasuredZ + (((d.dblElectrodeLength - 1) + 3 *
(d.dblElectrodeSpacing + d.dblElectrodeLength)) + d.dblElectrodeLength /
2)
* SIN(RADIANS(s.dblMeasuredACPCAngle)) *
COS(RADIANS(s.dblMeasuredCtrAngle)) AS Expr9,
s.dblAtlasScaleX,
s.dblAtlasScaleY,
s.dblAtlasScaleZ,
s.dblAtlasMovementX,
s.dblAtlasMovementY,
s.dblAtlasMovementZ,
s.dblAtlasRotationX,
s.dblAtlasRotationY,
s.dblAtlasRotationZ
FROM dbo.tblDBSSurgery AS s
INNER JOIN dbo.tblPatientDemographics AS p
ON p.idPatient =
s.idPatient
LEFT OUTER JOIN dbo.tblLookupLeads AS d
ON d.idLead = s.intLeadType
LEFT OUTER JOIN dbo.tblLookupDBSSites AS ds
ON ds.idDBSSite = s.intSite
WHERE (s.intProcedure = 0
OR s.intProcedure = 2)
AND (s.blnOutside = 0)
AND (NOT
(p.strMRN = '09999999'))
AND (NOT (p.strMRN = '08888888'))
ORDER BY surgerydate

Related

Business hours calculation 8AM to 8PM AST business hours in the format of days: hours : minutes its

Need to exclude weekends and before business hours and after business hours
Below code not working accurately.
Please suggest any other solution too
<TEXT(FLOOR( ROUND(720*((5*FLOOR((DATEVALUE(ClosedDate)-DATE(1996,01,01))/7) +
MIN(5,
MOD(DATEVALUE(ClosedDate)-DATE(1996,01,01), 7) +
MIN(1, 24/12*(MOD(ClosedDate-DATETIMEVALUE('1996-01-01 16:00:00'), 1)))
))-(5*FLOOR((DATEVALUE(CreatedDate)-DATE(1996,01,01))/7) +MIN(5,
MOD(DATEVALUE(CreatedDate)-DATE(1996,01,01), 7) +
MIN(1, 24/12*(MOD(CreatedDate-DATETIMEVALUE('1996-01-01 16:00:00'), 1)))
))), 0) / 720)) & " Day(s) " & TEXT( FLOOR( MOD(ROUND(720*((5*FLOOR((DATEVALUE(ClosedDate)-DATE(1996,01,01))/7) +
MIN(5,
MOD(DATEVALUE(ClosedDate)-DATE(1996,01,01), 7) +
MIN(1, 24/12*(MOD(ClosedDate-DATETIMEVALUE('1996-01-01 16:00:00'), 1)))
))-(5*FLOOR((DATEVALUE(CreatedDate)-DATE(1996,01,01))/7) +MIN(5,
MOD(DATEVALUE(CreatedDate)-DATE(1996,01,01), 7) +
MIN(1, 24/12*(MOD(CreatedDate-DATETIMEVALUE('1996-01-01 16:00:00'), 1)))
))), 0),720 ) / 60)) &' Hour(s)'&
TEXT(FLOOR(MOD( MOD(ROUND(720*((5*FLOOR((DATEVALUE(ClosedDate)-DATE(1996,01,01))/7) +
MIN(5,
MOD(DATEVALUE(ClosedDate)-DATE(1996,01,01), 7) +
MIN(1, 24/12*(MOD(ClosedDate-DATETIMEVALUE('1996-01-01 16:00:00'), 1)))
))-(5*FLOOR((DATEVALUE(CreatedDate)-DATE(1996,01,01))/7) +MIN(5,
MOD(DATEVALUE(CreatedDate)-DATE(1996,01,01), 7) +
MIN(1, 24/12*(MOD(CreatedDate-DATETIMEVALUE('1996-01-01 16:00:00'), 1)))
))), 0),720),60)))& "Minute(s)"),' ')/>

Why my R seems take forever to run a Mixed effects logistic regression

I indeed got a relatively big dataset and my mixed effects logistic regression is like below. Is that normal to take that long to run? or I made some mistakes?
library(lme4)
glmer_EBRD_undersample_1 <- glmer(leave_happened ~
performance_rating_2016 + performance_rating_2017 + performance_rating_2018 + performance_rating_2019 + performance_rating_2020
+ gender
+ target_group
+ target_pmf_band
+ target_hq_or_ro
+ target_office_location_country_distilled
+ target_org_unit_cost_centre_code_distilled
+ target_ebrd_region_distilled
+ target_contract_group_distilled
+ target_position_tenure_group
+ target_length_of_service_group_distilled
+ leaves_to_date
+ moves_to_date
+ joins_to_date
+ applied_count_to_date
+ line_reviewed_to_date
+ interviewed_to_date
+ offered_to_date
+ hired_to_date
+ (1 | person_id)
,
data = train_undersample_1,
family = binomial,
control = glmerControl(optimizer = "bobyqa"),
nAGQ = 10
)
summary(glmer_EBRD_undersample_1)
Also gave a warning like this: Warning in commonArgs(par, fn, control, environment()) :
maxfun < 10 * length(par)^2 is not recommended.

Compute daily returns from cumulative returns

I have a dataframe with cumulative stock returns from 1 to 5 days:
1dReturn 2dReturn 3dReturn 4dReturn 5dReturn
Ticker
LUNA -3.077 -3.077 -6.923 -6.915 -6.615
YTEN -2.139 -2.139 -18.182 -16.043 -16.578
I would like to compute the daily returns. Is there a function for that?
Code below creates the table above:
df = pd.DataFrame({'1dReturn': [-3.077, -2.139],
'2dReturn': [-3.077, -2.139],
'3dReturn': [-6.923, -18.182],
'4dReturn': [-6.915, -16.043],
'5dReturn': [-6.615, -16.578],},
index=['LUNA', 'YTEN'])
The formula to arrive at the daily returns works as follows:
daily returns day 2: cD2/d1
daily returns day 3: cD4/(d1*d2)
daily returns day 4: cD5/(d1*d2*d3)
daily returns day 5: cD5/(d1*d2*d3*d4)
where cD1 is the cum return of day 1 and d1 is the daily return for d1 etc.
np.exp(np.log(cumReturn + 1.0).diff()) - 1
cumReturn is the cumulative return series in Pandas.
R_cum_i = (1 + R_daily_i) * (1 + R_daily_i-1) ... - 1
R_cum_i-1 = (1 + R_daily_i-1) * (1 + R_daily_i-2) ... - 1
so
R_cum_i =(R_cum_i-1 + 1) * (1 + R_daily_i-1) - 1
1 + R_daily_i-1 = (R_cum_i + 1) / (R_cum_i-1 + 1)
1 + R_daily_i-1 = exp(log((R_cum_i + 1) / (R_cum_i-1 + 1)))
1 + R_daily_i-1 = exp(log(R_cum_i + 1) - log(R_cum_i-1 + 1))
1 + R_daily_i-1 = exp(log(R_cum_i + 1).diff())
then
R_daily_i-1 = exp(log(R_cum_i + 1).diff()) - 1

typescript date formatting not working

I am trying to get a date for mat like "2018-05-17T08:09:02", but when I tried below code I get "2018-05-17T8:9:2"
can some one help to get "2018-05-17T08:09:02" , this format
let d = new Date();
console.log("date>> "+d.getFullYear() + "-" + ((d.getMonth() + 1) < 10 ? '0' : '') +
(d.getMonth() + 1) + "-" + d.getDate() + "T" +( d.getHours() )+ ":"+ d.getMinutes() + ":"+ d.getSeconds());
According to How to format numbers by prepending 0 to single-digit numbers?
Your desirable answer is
let d = new Date();
console.log("date>> "+d.getFullYear() + "-" + ((d.getMonth() + 1) < 10 ? '0' : '') +
(d.getMonth() + 1) + "-" + d.getDate() + "T" +("0" + d.getHours()).slice(-2)+ ":"+ ("0" + d.getMinutes()).slice(-2) + ":"+ ("0" + d.getSeconds()).slice(-2));
But as #Aleksey Solovey already mentioned in the above, I also recommand to use d.toISOString().slice(0,-5).

Partial differentiation/gradient of anonymous function with array input

I have following anonymous function (with x as an array):
f = #(x) 312*x(2) - 240*x(1) + 30*x(3) - 24*x(4) + 282*x(1)*x(2) + 30*x(1)*x(3) + 18*x(1)*x(4) + 54*x(2)*x(3) + 6*x(2)*x(4) + 6*x(3)*x(4) + 638*x(1)^2 + 207*x(2)^2 + 6*x(3)^2 + 3*x(4)^2 + 4063
I want to make gradient of this function and save it for future use. Also with array input.
X = [ 0;...
0;...
0;...
0];
F = f(X)
G = g(X)
Is it possible to archive this with this type of function? Or maybe it is possible to somehow make it via diff command? Something like this:
g = [diff(f, x(1));...
diff(f, x(2));...
diff(f, x(3));...
diff(f, x(4))]
I guess the following is what you want. I'm afraid, you need the Symbolic Math Toolbox for a simple solution, otherwise I'd rather calculate the derivatives by hand.
x = [1 2 3 4];
%// define function
syms a b c d
f = 312*b - 240*a + 30*c - 24*d + 282*a*b + 30*a*c + 18*a*d + 54*b*c + ...
6*b*d + 6*c*d + 638*a^2 + 207*b^2 + 6*c^2 + 3*d^2 + 4063
%// symbolic gradient
g = gradient(f,[a,b,c,d])
%// eval symbolic function
F = subs(f,[a,b,c,d],x)
G = subs(g,[a,b,c,d],x)
%// convert symbolic value to double
Fd = double(F)
Gd = double(G)
or alternatively:
%// convert symbolic function to anonymous function
fd = matlabFunction(f)
gd = matlabFunction(g)
%// eval anonymous function
x = num2cell(x)
Fd = fd(x{:})
Gd = gd(x{:})
f =
638*a^2 + 282*a*b + 30*a*c + 18*a*d - 240*a + 207*b^2 + 54*b*c +
6*b*d + 312*b + 6*c^2 + 6*c*d + 30*c + 3*d^2 - 24*d + 4063
g =
1276*a + 282*b + 30*c + 18*d - 240
282*a + 414*b + 54*c + 6*d + 312
30*a + 54*b + 12*c + 6*d + 30
18*a + 6*b + 6*c + 6*d - 24
F =
7179
G =
1762
1608
228
48
fd =
#(a,b,c,d)a.*-2.4e2+b.*3.12e2+c.*3.0e1-d.*2.4e1+a.*b.*2.82e2+a.*c.*3.0e1+a.*d.*1.8e1+b.*c.*5.4e1+b.*d.*6.0+c.*d.*6.0+a.^2.*6.38e2+b.^2.*2.07e2+c.^2.*6.0+d.^2.*3.0+4.063e3
gd =
#(a,b,c,d)[a.*1.276e3+b.*2.82e2+c.*3.0e1+d.*1.8e1-2.4e2;a.*2.82e2+b.*4.14e2+c.*5.4e1+d.*6.0+3.12e2;a.*3.0e1+b.*5.4e1+c.*1.2e1+d.*6.0+3.0e1;a.*1.8e1+b.*6.0+c.*6.0+d.*6.0-2.4e1]
x =
[1] [2] [3] [4]
Fd =
7179
Gd =
1762
1608
228
48

Resources