Add conditions within a simple calculation - conditional-statements

I calculated a simple addition but want to add a condition in it;
1*(acute_miorhistory_mi) +
1*(chf) +
1*(pvd) +
1*(cvd) +
1*(copd) +
1*(dementia) +
2*(paralysis) +
1*(diabetes) +
2*(diabetes_comp) +
2*(renal_disease) +
1*(mild_liver_disease) +
3*(liver_disease) +
1*(ulcers) +
1*(rheum_disease) +
6*(aids) +
2*(carcinoma) +
6*(metacancer).
what I want to do is to:
not add diabetes if diabetes_comp exists
not add mild_liver_disease if liver_disease exist
not to add carcinoma if metacancer exists.
so how to add if command here?

Here's a nice way to add the conditions you need without changing the structure of your original command.
So the condition for adding carcinoma is that metacancer doesn't exist, or if carcinoma=0. In spss you can use (carcinoma=0) as a numerical value of 0 (true) or 1 (false):
1*(acute_miorhistory_mi) +
. . .
1*(diabetes)*(diabetes_comp=0) +
2*(diabetes_comp) +
2*(renal_disease) +
1*(mild_liver_disease)*(liver_disease=0) +
3*(liver_disease) +
. . .
2*(carcinoma)*(metacancer=0) +
6*(metacancer).

Related

Subtract in SQL Server code returning NULL

I'm having problems in this SQL server code, even using the ISNULL or COALESCE functions it keeps returning NULL, could someone tell me how to solve it?
CASE WHEN TIPO_X = 'F'
AND TIPO_CALCULOX IN ('N','S','D')
AND TRPR_COD_X IN (7,13,15,17) THEN CASE WHEN EVEN_COD_X IN (623,70623,947,70947,1871,71871,2697,72697,2871,72871,30623,30947,31871,22697,22871,20623,20947,21871) THEN CONVERT(int, (SELECT SUM(VLR_X)
FROM GMS_RELATORIO_GERAL
WHERE CHAPA_X = MATRÍCULA_DO_SEGURADO
AND EVEN_COD_X IN (623 + 7062 + 947 + 70947 + 1871 + 71871 + 2697 + 72697 + 2871 + 72871 + 30623 + 30947 + 31871)
AND MONTH(DT_COMP_X) = #MES)) - CONVERT(int,(SELECT SUM(VLR_X)
FROM GMS_RELATORIO_GERAL
WHERE CHAPA_X = MATRÍCULA_DO_SEGURADO
AND EVEN_COD_X IN (22697 + 22871 + 20623 + 20947 + 21871)
AND MONTH(DT_COMP_X) = #MES))
END
ELSE ''
END AS RENUMERAÇÃO_DA_CONTRIBUIÇÃO
Somehow I doubt this part of the code is as intended:
AND EVEN_COD_X IN (623 + 7062 + 947 + 70947 + 1871 + 71871 + 2697 + 72697 + 2871 + 72871 + 30623 + 30947 + 31871)
It's equivalent to this:
AND EVEN_COD_X IN (397898)
You probably want commas there instead.
Among your three EVEN_COD_X IN clauses, the last two are wrong : values should be separated with a comma (,).
CASE WHEN EVEN_COD_X IN (623 + 7062) -- It should be (623, 7062)
A good practice is :
to simplify your code (use CTE or temporary tables instead of doing everything in one query) ;
to use indentation (mistakes like this will become more visible) ;
for debugging, simplify it even more until it works.

token expression in flat file connection

I have loaded the variables in the format for example:
where produkt = 'Muj zivot2 R' and uraz = 'Uraz'
and I need the output in the file name to be:
Muj zivot2 R_Uraz
token worked for me, but it doesn't work in this case
" + TOKEN(" #[User::where] ","''",2) + "_" + TOKEN(" #[User::where] ","''",4) + "
You can use the following expression:
TOKEN(#[User::where],"'",2) + "_" + TOKEN(#[User::where],"'",4)
Output
Muj zivot2 R_Uraz

sql Update column with sum other columns data

I want to update with sum full datatable data row wise but how?
See what want I do:
This code only for shown sum data with row-wise but its not working with NULL value.
select
sum(tc6671 + tf6671 + pc6671 + pf6671 + tc8572 + tf8572 + pc8572 + pf8572 +
tc6672 + tf6672 + tc6673 + tf6673 + pc6673 + pf6673 + pc6674 + pf6674 + tc5852 + tf5852 +
tc5853 + tf5853) as Test
from
cmt_7th;
But I want to update my specific column with all sum data.
I was tried this code but failed:
update cmt_7th
set roll = sum(tc6671+tf6671+pc6671+pf6671+tc8572+tf8572+pc8572+pf8572+
tc6672+tf6672+tc6673+tf6673+pc6673+pf6673+
pc6674+pf6674+tc5852+tf5852+tc5853+tf5853);
And I was tried also by this code:
UPDATE c
SET c.total = c.tc6671+c.tf6671+c.pc6671+c.pf6671+c.tc8572+c.tf8572+c.pc8572+
c.pf8572+c.tc6672+c.tf6672+c.tc6673+c.tf6673+c.pc6673+c.pf6673+
c.pc6674+c.pf6674+c.tc5852+c.tf5852+c.tc5853+c.tf5853
FROM cmt_7th c
Instead of tc6671+tf6671, use coalesce to handle nulls.
Like coalesce(tc6671,0)+coalesce(tf6671,0)+...
So your update statement will be something like
UPDATE c
SET c.total = coalesce(c.tc6671,0) + coalesce(c.tf6671,0) + coalesce(c.pc6671,0)
+ coalesce(c.pf6671,0) + coalesce(c.tc8572,0) + coalesce(c.tf8572,0)
+ coalesce(c.pc8572,0) + coalesce(c.pf8572,0) + coalesce(c.tc6672,0)
+ coalesce(c.tf6672,0) + coalesce(c.tc6673,0) + coalesce(c.tf6673,0)
+ coalesce(c.pc6673,0) + coalesce(c.pf6673,0) + coalesce(c.pc6674,0)
+ coalesce(c.pf6674,0) + coalesce(c.tc5852,0) + coalesce(c.tf5852,0)
+ coalesce(c.tc5853,0) + coalesce(c.tf5853,0)
FROM cmt_7th c
But if you use select sum(coalesce(c.tc6671,0) ... then you will get only 1 row and column with sum of sum of all columns.
See what I mean here
http://rextester.com/LUIPOW59089

Rounding in sql server 2008 R2

I have this query
(case when a.item_no >= '77000' and a.item_no <='77099' then
(op_drill + op_machine + op_ssd + op_freight + op_paint + ((actual_tooling * 1.2)))
else (op_drill + op_machine + op_ssd + ((op_drill + op_machine + op_ssd) *.27) + ((op_drill + op_machine + op_ssd + op_freight + (op_curforg *1.25) + ((op_drill + op_machine + op_ssd)*.27)) * .075)+
op_freight + (op_curforg *1.25) + op_paint)
end) as new_cost,
it returns 10.1201575
In vb.net I can run this statement
dblNewCost = Math.Round(dblNewCost, 2, MidpointRounding.AwayFromZero)
which returns 10.13
Is there a way to duplicate this is sql server. I tried round(num,2) but that didn't showup correctly
I think you have some other issue. SQL Servers ROUND() function works just like VB's. There is no way using Math.Round(10.1201575, 2, MidpointRounding.AwayFromZero) would return 10.13. Compare the number that is being rounded on both side to make sure you are comparing apples-to-apples.

How to shorten the boolean function

I have the following function:
f(x) = (x2 + x1x3x5)(x4 + x3x5x6)(x5 + x6)
How can I make the expression like:
f(x) = x1x2x3 + x2x3x4 + ...
out of this? Is there any method?
I'm not sure if SO is the right place to post this...I guess it's not, but still, I found the tag and around 100 posts with it, so here I am :P
As far as I remember the boolean algebra you can just multiply them as ordinary numbers.
So (x2 + x1x3x5)(x5 + x6) will be x2x5 + x2x6 + x1x3x5x5 + x1x3x5x6. But again as far as I remember this applied only to "AND" not to "OR"
Well this looks more like a math question.
But if I understand what you want correctly it would look like this
x2x4x5+x2x4x6+x2x3x5x6x5+x2x3x5x6x6+x1x3x5x4 ext.
basically
(a1+a2)a3=a1a3+a2a3
(a1+a2)(a3+a4)=a1a3+a1a4+a2a3+a3a4
Here you go:
(x2x4x5 + x1x3x5)(x4x6) + (x2x5 + x1x3x5)(x3x5x6)
(x2x4x5x6 + x1x3x4x5x6) + (x2x5 + x1x3x5)(x3x5x6)
x2x4x5x6 + x1x3x4x5x6 + x2x3x5x6 + x1x3x5x6
x2x4x5x6 + x1x3(x4x5x6 + x5x6) + x2x3x5x6
x2x5x6(x4+1) + x1x3(x4x5x6 + x5x6)
x2x4x5x6 + x1x3(x5x6(x4+1))
x2x4x5x6 + x1x3(x4x5x6)
x2x4x5x6 + x1x3x4x5x6
(x2+x1x3)x4x5x6
I probably made a mistake somewhere, so you should test it first.
Your original:
f(x) = (x2 + x1x3x5)(x4 + x3x5x6)(x5 + x6)
Now using some simple maths:
f(x) = (x2x4 + x2x3x5x6 + x1x3x5x4 + x1x3x5x3x5x6)(x5 + x6)
f(x) = x2x4x5 + x2x3x5x6x5 + x1x3x5x4x5 + x1x3x5x3x5x6x5 + x2x4x6 + x2x3x5x6x6 + x1x3x5x4x6 + x1x3x5x3x5x6x6
Simplifying gets us the answer (though not necessarily shorter),
f(x) = x2x4x5 + x2x3x5x6 + x1x3x5x4x5 + x1x3x5x6 + x2x4x6 + x2x3x5x6 + x1x3x5x4x6 + x1x3x5x6