Rounding up decimal number with condition in T-SQL - sql

I have a ASP function as below for rounding up the amount:
function GetRoundedVal(amount)
NoOfRight = right(formatnumber(amount,2),1)
if NoOfRight = 0 then
roundedAmount = amount
elseif NoOfRight = 1 then
roundedAmount = amount - 0.01
elseif NoOfRight = 2 then
roundedAmount = amount - 0.02
elseif NoOfRight = 3 then
roundedAmount = amount + 0.02
elseif NoOfRight = 4 then
roundedAmount = amount + 0.01
elseif NoOfRight = 5 then
roundedAmount = amount
elseif NoOfRight = 6 then
roundedAmount = amount - 0.01
elseif NoOfRight = 7 then
roundedAmount = amount - 0.02
elseif NoOfRight = 8 then
roundedAmount = amount + 0.02
elseif NoOfRight = 9 then
roundedAmount = amount + 0.01
else
end if
GetRoundedVal = roundedAmount
end function
The result should be like this:
+----------------+--------+
| Original Value | Result |
+----------------+--------+
| 19.91 | 19.90 | Original Value - 0.01
| 19.92 | 19.90 | Original Value - 0.02
| 19.93 | 19.95 | Original Value + 0.02
| 19.94 | 19.95 | Original Value + 0.01
| 19.95 | 19.95 |
| 19.96 | 19.95 | Original Value - 0.01
| 19.97 | 19.95 | Original Value - 0.02
| 19.98 | 20.00 | Original Value + 0.02
| 19.99 | 20.00 | Original Value + 0.01
+----------------+--------+
The question is can we do this directly on T-SQL?
SQL Server V14.
If it even possible.
Thanks.

You can try this.
DECLARE #MyTable TABLE(OriginalValue DECIMAL(18,2))
INSERT INTO #MyTable VALUES (19.91), (19.92), (19.93), (19.94), (19.95), (19.96), (19.97), (19.98), (19.99)
SELECT
OriginalValue,
ROUND( ( OriginalValue / 0.05 ), 0, 0 ) * 0.05 Result
FROM #MyTable
Result:
OriginalValue Result
--------------------------------------- ---------------------------------------
19.91 19.90000000
19.92 19.90000000
19.93 19.95000000
19.94 19.95000000
19.95 19.95000000
19.96 19.95000000
19.97 19.95000000
19.98 20.00000000
19.99 20.00000000

try This
DECLARE #Inp TABLE
(
Org DECIMAL(10,4),
Res DECIMAL(10,2)
)
INSERT INTO #Inp
(
Org
)
VALUES(19.91),
(19.92),
(19.93),
(19.94),
(19.95),
(19.96),
(19.97),
(19.98),
(19.99)
SELECT
*,
Result = CAST(
Org +
CASE RIGHT(CAST(Org AS DECIMAL(10,2)),1)
WHEN 1 THEN -0.01
WHEN 2 THEN -0.02
WHEN 3 THEN 0.02
WHEN 4 THEN 0.01
WHEN 5 THEN 0
WHEN 6 THEN -0.01
WHEN 7 THEN -0.02
WHEN 8 THEN 0.02
WHEN 9 THEN 0.01
ELSE 0 END
AS DECIMAL(10,2))
FROM #Inp
----------

Related

Why does this piecewise linear mixed model not produce equal estimates at the knot

I am wondering if someone could help me interpret my piecewise lmm results. Why does ggpredict() produce different estimates for the knot at 10 weeks (end of tx; see ‘0’ in graph at end)? I've structured the data like so:
bpiDat <- bpiDat %>%
mutate(baseToEndTx = ifelse(week <= 10, week, 1)) %>%
mutate(endOfTxToFu = case_when(
week <= 10 ~ 0,
week == 18 ~ 8,
week == 26 ~ 16,
week == 34 ~ 24
)) %>%
select(id, treatment, baseHamd, week, baseToEndTx, endOfTxToFu,
painInterferenceMean, painSeverityMean, bpiTotal) %>%
mutate(baseHamd = scale(baseHamd, scale=F))
Which looks like this:
id treatment baseHamd week baseToEndTx endOfTxToFu painSeverityMean
1 1 4.92529343 0 0 0 6.75
1 1 4.92529343 2 2 0 7.25
1 1 4.92529343 4 4 0 8.00
1 1 4.92529343 6 6 0 NA
1 1 4.92529343 8 8 0 8.25
1 1 4.92529343 10 10 0 8.00
1 1 4.92529343 18 1 8 8.25
1 1 4.92529343 26 1 16 8.25
1 4.92529343 34 1 24 8.00
The best fitting model:
model8 <- lme(painSeverityMean ~ baseHamd + baseToEndTx*treatment + endOfTxToFu + I(endOfTxToFu^2)*treatment,
data = bpiDat,
method = "REML",
na.action = "na.exclude",
random = ~baseToEndTx | id)
This is how I’m visualizing:
test1 <- ggpredict(model8, c("baseToEndTx", "treatment"), ci.lvl = NA) %>%
mutate(x = x - 10) %>%
mutate(phase = "duringTx")
test2 <- ggpredict(model8, c("endOfTxToFu", "treatment"), ci.lvl = NA) %>%
mutate(phase = "followUp")
t <- rbind(test1, test2)
t <- t %>%
pivot_wider(names_from = "phase",
values_from = "predicted")
ggplot(t) +
geom_smooth(aes(x,duringTx,col=group),method="lm",se=FALSE) +
geom_smooth(aes(x,followUp,col=group),method="lm",se=FALSE) +
geom_point(aes(x,duringTx,col=group)) +
geom_point(aes(x,followUp,col=group)) +
ylim(2,6)
Which produces this:


Filtering a view using three bit columns

The view can be filtered on these three columns:
Profit(bit), Loss(bit), NoImpact(bit)
Backstory: On a webpage a user can choose to filter the data based on three checkboxes (Profit, Loss, No impact).
What I am looking for: If they check 'Profit' return everything where 'Profit' = 1, if then they check 'Loss' show 'Profit' AND 'Loss' results but exclude 'NoImpact', and so forth.
This is what I've tried so far and part of my stored proc:
WHERE (
((#ProfitSelected is null OR #ProfitSelected = 'false') OR (Profit = #ProfitSelected))
--I've tried using AND here as well.
OR ((#LossSelected is null OR #LossSelected = 'false') OR (Loss = #LossSelected))
OR ((#NoImpactSelected is null OR #NoImpactSelected = 'false') OR (NoImpact = #NoImpactSelected))
)
END
exec dbo.SearchErrorReports #ProfitSelected = 1, #LossSelected = 1, #NoImpactSelected = 0
Thank you.
EDIT: As requested here are some tests and desired results:
TEST exec dbo.SearchErrorReports #ProfitSelected = 1, #LossSelected = 1, #NoImpactSelected = 0
Result
id | Profit | Loss | NoImpact
----------------------------------------
1 | 1 | 0 | 0
2 | 1 | 0 | 0
3 | 0 | 1 | 0
4 | 0 | 1 | 0
5 | 0 | 1 | 0
TEST exec dbo.SearchErrorReports #ProfitSelected = 0, #LossSelected = 1, #NoImpactSelected = 0
Result
id | Profit | Loss | NoImpact
----------------------------------------
1 | 0 | 1 | 0
2 | 0 | 1 | 0
3 | 0 | 1 | 0
TEST exec dbo.SearchErrorReports #ProfitSelected = 1, #LossSelected = 1, #NoImpactSelected = 1
Result
id | Profit | Loss | NoImpact
----------------------------------------
1 | 1 | 0 | 0
2 | 0 | 1 | 0
3 | 0 | 1 | 0
4 | 0 | 0 | 1
5 | 1 | 0 | 0
6 | 0 | 0 | 1
Etc and all the different permutations.
If I understand the question correctly, the following WHERE clause should return the expected results:
WHERE
(#ProfitSelected = 1 AND Profit = 1) OR
(#LossSelected = 1 AND Loss = 1) OR
(#NoImpactSelected = 1 AND NoImpact = 1) OR
(#ProfitSelected = 0 AND #LossSelected = 0 AND #NoImpactSelected = 0)
#Zhorov helped me a lot. I had to modify his query slightly to have all test cases covered:
WHERE (#ProfitSelected = 0 AND #LossSelected = 0 AND #NoImpactSelected = 0) OR
(#ProfitSelected = 1 AND Profit = 1) OR
(#LossSelected = 1 AND Loss = 1) OR
(#NoImpactSelected = 1 AND NoImpact = 1) OR
(#ProfitSelected = 1 AND #LossSelected = 1 AND #NoImpactSelected = 1)

Display commend in ampl

I have a 2 dimension variable in ampl and I want to display it. I want to change the order of the indices but I do not know how to do that! I put my code , data and out put I described what kind of out put I want to have.
Here is my code:
param n;
param t;
param w;
param p;
set Var, default{1..n};
set Ind, default{1..t};
set mode, default{1..w};
var E{mode, Ind};
var B{mode,Var};
var C{mode,Ind};
param X{mode,Var,Ind};
var H{Ind};
minimize obj: sum{m in mode,i in Ind}E[m,i];
s.t. a1{m in mode, i in Ind}: sum{j in Var} X[m,j,i]*B[m,j] -C[m,i] <=E[m,i];
solve;
display C;
data;
param w:=4;
param n:=9;
param t:=2;
param X:=
[*,*,1]: 1 2 3 4 5 6 7 8 9 :=
1 69 59 100 70 35 1 1 0 0
2 34 31 372 71 35 1 0 1 0
3 35 25 417 70 35 1 0 0 1
4 0 10 180 30 35 1 0 0 0
[*,*,2]: 1 2 3 4 5 6 7 8 9 :=
1 64 58 68 68 30 2 1 0 0
2 44 31 354 84 30 2 0 1 0
3 53 25 399 85 30 2 0 0 1
4 0 11 255 50 30 2 0 0 0
The output of this code using glpksol is like tis:
C[1,1].val = -1.11111111111111
C[1,2].val = -1.11111111111111
C[2,1].val = -0.858585858585859
C[2,2].val = -1.11111111111111
C[3,1].val = -0.915032679738562
C[3,2].val = -1.11111111111111
C[4,1].val = 0.141414141414141
C[4,2].val = 0.2003367003367
but I want the result to be like this:
C[1,1].val = -1.11111111111111
C[2,1].val = -0.858585858585859
C[3,1].val = -0.915032679738562
C[4,1].val = 0.141414141414141
C[1,2].val = -1.11111111111111
C[2,2].val = -1.11111111111111
C[3,2].val = -1.11111111111111
C[4,2].val = 0.2003367003367
any idea?
You can use for loops and printf commands in your .run file:
for {i in Ind}
for {m in mode}
printf "C[%d,%d] = %.4f\n", m, i, C[m,i];
or even:
printf {i in Ind, m in mode} "C[%d,%d] = %.4f\n", m, i, C[m,i];
I don't get the same numerical results as you, but anyway the output works:
C[1,1] = 0.0000
C[2,1] = 0.0000
C[3,1] = 0.0000
C[4,1] = 0.0000
C[1,2] = 0.0000
C[2,2] = 0.0000
C[3,2] = 0.0000
C[4,2] = 0.0000

How can i get the total count in if else condition loop in vb.net?

I want to get the total count from the condition for loop, Let's say for the first row(I), the code will check through the if condition, if the condition meets the specific month then use the specific for loop to get the column count (for example : if the row's month is 1 then apply For k As Integer = 4 To dt.Columns.Count - 1 to get the count, if the row's month is 2 then apply For k As Integer = 4 To dt.Columns.Count - 2 to get the count and etc) follow by second row(I) and so on, after the if else condition k then return the total count ,how can i achieve it?
I have tried the method below but my code below did not work as what had been described above, it only return the count for the first condition,Please guide me on this :
For I As Integer = 0 To dt.Rows.Count - 1
'If dt.Rows(I).Item("Month").ToString = "1" Or dt.Rows(I).Item("Month").ToString = "3" Or dt.Rows(I).Item("Month").ToString = "5" Or dt.Rows(I).Item("Month").ToString = "7" Or dt.Rows(I).Item("Month").ToString = "8" Or dt.Rows(I).Item("Month").ToString = "10" Or dt.Rows(I).Item("Month").ToString = "12" Then
For k As Integer = 4 To dt.Columns.Count - 1
If dt.Rows(I).Item(k).ToString() = "1" Then
count1 += 1
Else
count1 = 0
End If
If count1 > 13 Then
Dx = True
End If
Next k
'ElseIf dt.Rows(I).Item("Month").ToString() = "2" Or dt.Rows(I).Item("Month").ToString() = "4" Or dt.Rows(I).Item("Month").ToString() = "6" Or dt.Rows(I).Item("Month").ToString() = "9" Or dt.Rows(I).Item("Month").ToString() = "11" Then
'For k As Integer = 4 To dt.Columns.Count - 2
'If dt.Rows(I).Item(k).ToString() = "1" Then
' count1 += 1
'Else
' count1 = 0
'End If
' If total > 13 Then
' Dx = True
' End If
'Next k
'End If
Next I
DataTable (column represents the date, month 11 has 30 columns and month 12 has 31 columns)
----------------------------------------------------------------------------
Id | year | month | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | till 31
----------------------------------------------------------------------------
kek | 2019 | 10 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
kek | 2019 | 11 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
kek | 2019 | 12 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 |
link
Expected Output :
if the consecutive count from 11/11 till 12/12 is more than 13 then dx return true.
In your current code you're resetting count1 to zero in each Else.
Also this code:
total += count1
If total > 13 Then
Dx = True
End If
...looks like it should be outside the loop.
It seems to me that you need this:
For I As Integer = 0 To dt.Rows.Count - 1
Dim offset = 2
If {"1", "3", "5", "7", "8", "10", "12"}.Contains(dt.Rows(I).Item("Month").ToString())
offset = 1
End If
For k As Integer = 4 To dt.Columns.Count - offset
If dt.Rows(I).Item(k).ToString() = "1" Then
count1 += 1
End If
Next
Next
If count1 > 13 Then
Dx = True
End If
If you want to get fancier then try LINQ:
Dim query = _
From dr In dt.Rows.OfType(Of DataRow)()
Let offset = If({"1", "3", "5", "7", "8", "10", "12"}.Contains(dr.Item("Month").ToString()), 0, 1)
From k In Enumerable.Range(4, dt.Columns.Count - offset)
Where dr.Item(k).ToString() = "1"
Select 1
Dim total = query.Sum()
If total > 13 Then
Dx = True
End If

Having a key error when using group and sum in a dataframe

I would like to use groupby and sum a csv file
a b c d
1111 0.1 1 1
1111 0 1 0
2222 0.2 1 1
1111 0.2 2 1
2222 1 1
1111 0.3 2 0
3333 0.4 1 1
3333 0.5 2 1
1111 0.6 2 1
e: # if b < 0.2, group column a and sum of column c
f: # If b >= 0.2 group column a and sum of column c
g: # If d = 1, and b >= 0.2, g is sum of c
h: # If d = 0 and b < 0.2, h is sum of c
expected output:
e f g h
1111 2 6 4 1
2222 1 1
3333 3 3
I try:
df1 = df[(df['d'] == 1) & (df['b'] >= 0.2)]
df1.groupby('a')['c'].sum()
However, I got key error in a large file:
pandas.index.IndexEngine.get_loc, pandas.hastable.PobjectHashTable.get_item in column a.
Maybe you can try different approach:
First create conditions columns - e to h and then use mul for filling this mask with values of column c. Last use GroupBy.sum:
df['e'] = df['b'] < 0.2
df['f'] = df['b'] >= 0.2
df['g'] = (df['d'] == 1) & (df['b'] >= 0.2)
df['h'] = (df['d'] == 0) & (df['b'] < 0.2)
print df
a b c d e f g h
0 1111 0.1 1 1 True False False False
1 1111 0.0 1 0 True False False True
2 2222 0.2 1 1 False True True False
3 1111 0.2 2 1 False True True False
4 2222 NaN 1 1 False False False False
5 1111 0.3 2 0 False True False False
6 3333 0.4 1 1 False True True False
7 3333 0.5 2 1 False True True False
8 1111 0.6 2 1 False True True False
df.loc[:, ['e','f','g','h']]= df.loc[:, ['e','f','g','h']].mul(df.c, axis=0)
print df
a b c d e f g h
0 1111 0.1 1 1 1 0 0 0
1 1111 0.0 1 0 1 0 0 1
2 2222 0.2 1 1 0 1 1 0
3 1111 0.2 2 1 0 2 2 0
4 2222 NaN 1 1 0 0 0 0
5 1111 0.3 2 0 0 2 0 0
6 3333 0.4 1 1 0 1 1 0
7 3333 0.5 2 1 0 2 2 0
8 1111 0.6 2 1 0 2 2 0
df1 = df.groupby('a').sum()
print df1[['e','f','g','h']]
e f g h
a
1111 2 6 4 1
2222 0 1 1 0
3333 0 3 3 0