I am trying to create a single Excel IF function that incorporates the below IF statements:
If Date_Time_of_Request <= Authorization_Start_Date then REVIEW_TYPE = 'Prospective';
Else If Authorization_Start_Date <= Date_Time_of_Request <= Discharge_Date then REVIEW_TYPE = 'Concurrent';
Else If Date_Time_of_Request > = Discharge_Date then REVIEW_TYPE = 'Retrospective';
Else If Date_Time_of_Request > = Authorization_End_Date then REVIEW_TYPE = 'Retrospective';
My attempt at this is: =IF(AS2 <= J2, "PROSPECTIVE", IF(AND(AS2 >= J2,AS2 <= V2),"CONCURRENT",IF(OR(AS2 > V2,AS2 > H2),"RETROSPECTIVE","UNKNOWN")))
However, V2 is blank in most rows, and I need it to move onto the next statement if V2 is blank. As it is currently written, AS2 > V2 being true most of the time (even with it being blank).
Something like:
and(AS2>V2,LEN(V2)>0)
Related
I have sql data of 2,907,735 rows and 32 columns.. and I want to perform simple mathematical calculations for each row using the attributes in that row.
Currently, I had to program this in R which is taking so much time, and I'm not sure SQL has the capability to do this simple programming calculations are not.
If it has the capability, I think SQL will be faster.
Please help.
q1=0
q2=0
q3=0
q4=0
frac2final=0
sumcnt = 0
cumcnt = 0
year = 0
startpoint1 = 0
startpoint2 = 0
sumexpect = 0
cntatmind = 0
cntatmaxd = 0
exatmind = 0
exatmaxd = 0
ex1=0
ex2=0
ex3=0
ex4=0
cumcntfirst = 0
for (i in 1:2907735)
{
frac2final = a[i,25]
sumcnt = a[i,31]
cumcnt = frac2final + sumcnt
year = a[i,12]
sumexpect = a[i,32]
cntatmind = a[i,4]
cntatmaxd = a[i,5]
exatmind = a[i,6]
exatmaxd = a[i,7]
if(year !=2016) {
startpoint1 = 0
startpoint2=0}
else
{startpoint1 = frac2final
startpoint2 = frac2final}
#cumcntfirst = startpoint2 + cntatmind
if (startpoint1<=0.25) q1 = max( (min(0.25,cumcnt) - startpoint1),0) else q1=0
startpoint1 = startpoint1 + q1
if(startpoint1<=0.5) q2 = max( (min(0.5,cumcnt) - startpoint1),0) else q2=0
startpoint1 = startpoint1 + q2
if(startpoint1<=0.75) q3 = max( (min(0.75,cumcnt) - startpoint1),0) else q3=0
startpoint1 = startpoint1 + q3
q4 = max(0,sumcnt-q1-q2-q3)
a[i,33] = q1
a[i,34] = q2
a[i,35] = q3
a[i,36] = q4
}
Remember in SQL we work with sets, not loops. An "Intermediate result" would be an in intermediate result set, which is synonymous with a subquery (for the most part).
As an example using your R as a starting point:
SELECT q1,
startpoint1 + q1 AS startpoint1
FROM (
startpoint1,
SELECT CASE
WHEN startpoint1 <= 0.25
THEN CASE
WHEN 0 > CASE
WHEN 0.25 > cumcnt
THEN 0.25
ELSE cumcnt
END - startpoint1
THEN 0
ELSE CASE
WHEN 0.25 > cumcnt
THEN 0.25
ELSE cumcnt
END - startpoint1
END
ELSE 0
END AS q1
FROM (
SELECT CASE
WHEN year <> 2016
THEN 0
ELSE frac2final
END AS startpoint1,
CASE
WHEN year <> 2016
THEN 0
ELSE frac2final
END AS startpoint2
FROM yourTable
) subquery1
) subquery2
There is no function to replicate your min and max here so we have to use CASE statements to do the logic. There are many examples of UDFs out there to give this functionality though. The trick is that min and max DO exist in tsql, but it takes the min/max of values in a column when aggregating many records together. Your use here is synonymous with UDFs generally called GREATEST() and LEAST() to compare two or more fields from the same record.
To get a grasp on how this is working, run the inner-most subquery and see what it produces, then run the next subquery (which has subquery1 inside of it) to see how it uses those results... then you can just reuse this logic to build your remaining fields for your final result set.
I have, as a part of a bigger query, some subqueries that I would like to convert to CASE statements instead.
The subquery looks like this (and works):
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM [F0001].[dbo].[ProdTr] WHERE AcYrPr = '201601' AND ProdTr.TrTp = 1 AND [F0001].[dbo].[ProdTr].CustNo = '12773') AS dg_period_1
However, I don't seem to find any logical way to put this into a CASE-statement.
Any help would be appreciated!
(
SELECT CASE
WHEN SUM(t1.DAm) <> 0
THEN (SUM(t1.DAm) + SUM(t1.StcCst)) * 100 / SUM(t1.DAm)
ELSE 0 /* or whatever you want to have in this case */
END AS 'DG'
FROM [F0001].[dbo].[ProdTr] t1
WHERE t1.AcYrPr = '201601' AND
t1.TrTp = 1 AND
t1.CustNo = '12773'
) AS dg_period_1
I also removed some unneeded parentheses and simplified an operation (x - (y * -1) = x + y)
You could use the following statement with CASE provided you want to return a null when SUM(DAm) is null or 0.
(SELECT CASE
WHEN SUM(DAm) IS NOT NULL and SUM(DAm) <> 0 THEN (((SUM(DAm) - (SUM(StcCst) * -1)) * 100) /SUM(DAm))
ELSE NULL
END AS 'DG'
FROM [F0001].[dbo].[ProdTr]
WHERE AcYrPr = '201601'
AND ProdTr.TrTp = 1
AND [F0001].[dbo].[ProdTr].CustNo = '12773') AS dg_period_1
I have these two case statements and can not for the life of me figure out how to combine them to show in a MSSQL view. Any help would be great.
CASE WHEN [ordertype] = '2' THEN [CommissionAmt1] * - 1 ELSE [CommissionAmt1] END
and
CASE WHEN (is_member('Buyer') = 1 OR is_member('CustomerService') = 1) THEN 0 ELSE CommissionAmt1 END
Just adding the first case to wherever the CommissionAmt1 is referenced in the second statement.
CASE WHEN (is_member('Buyer') = 1 OR is_member('CustomerService') = 1) THEN
0
ELSE
CASE WHEN [ordertype] = '2' THEN
[CommissionAmt1] * - 1
ELSE
[CommissionAmt1]
END
END
Or going the other way. It was hard to understand which way the calculation needs to be performed. The only hint was []
CASE WHEN [ordertype] = '2' THEN
(
CASE WHEN (is_member('Buyer') = 1 OR is_member('CustomerService') = 1) THEN
0
ELSE
CommissionAmt1
END
) * - 1
ELSE
CASE WHEN (is_member('Buyer') = 1 OR is_member('CustomerService') = 1) THEN
0
ELSE
CommissionAmt1
END
END
Either way, you would be able to save some calculations by sub querying the dependent value.
SELECT
*,
ValueWithDependant=CASE WHEN (Dependant>0) THEN (SomeValue / Dependant) ELSE NULL END
FROM
(
SELECT
X,Y,Z,
Dependant=CASE WHEN SomeValue=1 THEN 1 ELSE 0 END
FROM
SomeTable
)AS DETAIL
I am still new to vba.
I am trying to create a new function however, it keeps giving me an output that I am not expecting.
my code is as follows:
Function bonusplanA(a, b, c)
Dim example As Range
Set example = Range("a:c")
Value = Application.WorksheetFunction.CountIf(example, ">=90")
Value1 = Application.WorksheetFunction.CountIf(example, ">=80")
If Value = 3 Then
bonusplanA = "$20,000 bonus"
Else
If Value1 = 3 Then
bonusplanA = "$10,000 bonus"
Else
bonusplanA = "NO BONUS"
End If
End If
End Function
You need to define your function like this:
Function bonusplanA(a, b, c)
If a >= 90 And b >= 90 And c >= 90 Then
bonusplanA = "$20,000 bonus"
Else
If a >= 80 And b >= 80 And c >= 80 Then
bonusplanA = "$10,000 bonus"
Else
bonusplanA = "NO BONUS"
End If
End If
End Function
The problem in your example was that Range("a:c") does not make a range of your a,b,c variables; instead it selects the range composed of columns A, B and C.
You need to use parameters a, b and c directly, not through the Range function.
Otherwise, the logic was sound. :)
I am making an rdl report and I have three check-boxes that if checked need to alter my WHERE statement. If none are checked the result should only match by date range. If one or more are checked it needs to return the fields that match the variables corresponding string.
WHERE
(EffectiveDate BETWEEN #StartDate AND #EndDate)
AND (((#IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR #IncludeSEWPrefix = 0)
AND ((#IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR #IncludePAWPrefix = 0)
AND ((#IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR #IncludeRPLPrefix = 0))
My code so far works when none are checked and when one is checked, but returns nothing when more than one check-box has been checked. So to try and fix this I altered the code to this
WHERE
(EffectiveDate BETWEEN #StartDate AND #EndDate)
AND ((((#IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR #IncludeSEWPrefix = 0)
OR ((#IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR #IncludePAWPrefix = 0)
OR ((#IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR #IncludeRPLPrefix = 0)))
Which resulted in all rows being returned no matter what was selected. Can someone tell me where I am going wrong?
I believe this is the correct rearrangement. Trickier problem than it first appears. The issue was seperating lines like ((#IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR #IncludeSEWPrefix = 0) with AND meant that if two includes were true, a row would need to have both PrefixId's, which can't happen. And if you separated them with OR, then having just one include false, means that every row will pass. So instead, check that a row has the prefix of any that are included, otherwise all includes have to be off.
WHERE EffectiveDate BETWEEN #StartDate AND #EndDate
AND
(
(#IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR
(#IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR
(#IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR
(#IncludeSEWPrefix = 0 AND #IncludePAWPrefix = 0 AND #IncludeRPLPrefix = 0)
)
Try this
WHERE
(EffectiveDate BETWEEN #StartDate AND #EndDate)
AND
(
(#IncludeSEWPrefix = 1 AND PrefixId = 'SEW' OR #IncludeSEWPrefix = 0) AND
(#IncludePAWPrefix = 1 AND PrefixId = 'PAW' OR #IncludePAWPrefix = 0) AND
(#IncludeRPLPrefix = 1 AND PrefixId = 'RPL' OR #IncludeRPLPrefix = 0)
)
You have more parenthesis than needed, it does not hurt but just be aware.
Maybe this can help:
WHERE (EffectiveDate BETWEEN #StartDate AND #EndDate)
AND ( ((#IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR (#IncludeSEWPrefix = 0 AND #PrefixId <> 'SEW'))
OR ((#IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR (#IncludePAWPrefix = 0 AND #PrefixId <> 'PAW'))
OR ((#IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR (#IncludeRPLPrefix = 0 AND #PrefixId <> 'RPL'))
)