Changing VBA code - into - SQL statement - sql

I have this code in Excel VBA:
If Cells(lngRow, ConstTASTMS_ActivateTime).Value < getdate() - 7 Then
Cells(lngRow, ConstTASTimeClass).Value = "BOH"
If Cells(lngRow, ConstTASTask_Status) = "Closed" Then
If Cells(lngRow, ConstTASActual_EndDate).Value <= getdate() - 7 Then
Cells(lngRow, ConstTASTimeClass).Value = "BOH-CO"
Else
Cells(lngRow, ConstTASTimeClass).Value = "BOH-C"
End If
End If
If Cells(lngRow, ConstTASTask_StatusReason) = "Canceled" Then
If Cells(lngRow, ConstTASActual_EndDate).Value <= getdate() Then
Cells(lngRow, ConstTASTimeClass).Value = "BOH-XO"
Else
Cells(lngRow, ConstTASTimeClass).Value = "BOH-X"
End If
End If
Else
Cells(lngRow, ConstTASTimeClass).Value = "New"
If Cells(lngRow, ConstTASTask_Status) = "Closed" Then Cells(lngRow, ConstTASTimeClass).Value = "New-C"
If Cells(lngRow, ConstTASTask_Status) = "Canceled" Then Cells(lngRow, ConstTASTimeClass).Value = "New-X"
End If
And now, I need to translate it into the SQL statement.
So far, I got this, and don't think it's correct.
Can someone please take a look and help me to correct it?
CASE WHEN TMS_ActivateTime < getdate() - 7 THEN
TimeClass = "BOH"
CASE WHEN Task_Status = "Closed" THEN
CASE WHEN Actual_EndDate <= getdate() - 7 THEN
TimeClass = "BOH-CO"
ELSE
TimeClass = "BOH-C"
CASE WHEN Task_StatusReason = "Canceled" THEN
CASE WHEN Actual_EndDate <= getdate() THEN
TimeClass = "BOH-XO"
ELSE
TimeClass = "BOH-X"
ELSE
TimeClass = "New"
CASE WHEN Task_Status = "Closed" THEN TimeClass = "New-C"
CASE WHEN Task_Status = "Canceled" THEN TimeClass = "New-X"
END

I'll give it a shot. As the CASE will not evaluate any further after a WHEN is true, we need to sometimes reverse the order in which the values are assigned to TimeClass.
SELECT
CASE WHEN TMS_ActivateTime < getdate() - 7 THEN
CASE WHEN Task_StatusReason = "Canceled" THEN
CASE WHEN Actual_EndDate <= getdate() THEN
'BOH-XO'
ELSE 'BOH-X'
END
WHEN Task_Status = 'Closed' THEN
CASE WHEN Actual_EndDate <= getdate() - 7 THEN
'BOH-CO'
ELSE 'BOH-C'
END
ELSE 'BOH'
END
ELSE
CASE
WHEN TaskStatus = 'Canceled' THEN 'New-X'
WHEN TaskStatus = 'Closed' THEN 'New-C'
ELSE 'New' END
END
AS TimeClass

While it is not necessary in SQL syntax, it is helpful to format your case statements to see where a field ends and begins, i.e.:
--Field 1
CASE
WHEN
THEN
ELSE
END
--Field 2
CASE
WHEN
THEN
CASE
--still Field 2
WHEN
THEN
END
END

Related

How to compare two dates in text boxes?

I want to compare two dates in text boxes.
Public Function CourseStatus(ByVal RefDate2 As Variant) As String
Dim Description As String
If Len(RefDate2) > 0 And IsDate(RefDate2) Then
Select Case DateDiff("d", Date, RefDate2)
Case Is > 60
CourseStatus = "In Date"
Case Is > 0
CourseStatus = "Expiring"
Case Is = [ParticipationDate]
CourseStatus = "Not Refreshed"
Case Else
CourseStatus = "Expired"
End Select
Else
CourseStatus = "Please Book"
End If
End Function
If [ParticipationDate] & [RefDate2] match return "Not Refreshed" as CourseStatus.
I need to do this before running the rest of the code to give "in Date" "Expiring" "Expired" and if none of this applies display "Please Book".
e.g
ParticipationDate 1/1/19
RefDate2 1/1/19
CourseStatus "Not Refreshed"
Sorry I didn't read your code super accurately.
Your datediff function returns a integer value not a date. You should use
Case is 0
to check for a date match.
You might use something like this:
If Len(RefDate2) > 0 And IsDate(RefDate2) Then
Select Case DateDiff("d", Date, RefDate2)
Case Is > 60
CourseStatus = "In Date"
Case Is > 0
CourseStatus = "Expiring"
Case Else
If DateDiff("d", RefDate2, [ParticipationDate]) = 0 Then
CourseStatus = "Not Refreshed"
Else
CourseStatus = "Expired"
End If
End Select
Else
CourseStatus = "Please Book"
End If

Converting VB conditions to SQL conditions

There are some VB conditions that I need to execute in SQL query. Is this possible? Here's the code:::::::::::::::::::::::::::::::::::::::::::::::
If (ds.Tables(0).Rows.Count > 0) Then
Dim timeIn = ds.Tables(0).Rows(0)("TimeIn").ToString
Dim timeOut = ds.Tables(0).Rows(0)("TimeOut").ToString
If Not String.IsNullOrEmpty(timeIn) And Not String.IsNullOrEmpty(timeOut) Then
Dim result = DateTime.Compare(Convert.ToDateTime(timeIn), Convert.ToDateTime(timeOut))
If result > 0 Then 'last timeIn later than last timeOut
attendanceStatus = "On"
Else
attendanceStatus = "Off"
End If
ElseIf Not String.IsNullOrEmpty(timeIn) And String.IsNullOrEmpty(timeOut) Then
attendanceStatus = "On"
End If
lblAttendStatus.Text = attendanceStatus
End If
End If
You can do that by including in your SQL statement after SELECT:
CASE WHEN TimeIn < TimeOut THEN 'Off' ELSE 'On' END AS attendanceStatus,

Error 16: Expression Too Complex - Nested Select Case

I am getting a "Run-Time Error 16: Expression Too Complex" for the sencond code block. I read online that the maximum allowed number of nested expressions is 8, but if nested statements are defined by every "Select Case," then I haven't yet hit that. Is it possible it's because the Cases are dependent upon a variable? The first code block shows the possible values of egapend, while the second shows the code that is returning the error. Thanks in advance for your help.
First:
Select Case ge1e2
Case Is <= 0
Select Case ge2e3
Case Is <= 0
egap18a = 0
egap18b = 0
Case Is > 0
egap18a = 0
egap18b = ge2e3
egapend = Sheet1.[z1IE]
End Select
Case Is > 0
Select Case ge2e3
Case Is <= 0
egap18a = ge1e2
egap18b = 0
egapend = Sheet1.[z1CZ]
Case Is > 0
Select Case ge1e2
Case Is >= 180
egap18a = ge1e2
egap18b = ge2e3
egapend = Sheet1.[z1CZ]
Case Is < 180
egap18a = ge1e2
egap18b = ge2e3
egapend = Sheet1.[z1IE]
End Select
End Select
End Select
Second:
Dim e1length As Long
Dim e2length As Long
Dim cp121 As Boolean
e1length = DateDiff("d", Sheet1.[z1CZ], Sheet1.[z1DA])
e2length = DateDiff("d", Sheet1.[z1IE], Sheet1.[z1IF])
Select Case extendedgap
Case True
Select Case egapend
Case Sheet1.[z1IE] 'end of egap is start of E2
Select Case e2length 'was borrower employed by E2 for more than 6 months?
Case Is >= 180
cp121 = True
Case Is < 180
Select Case ge1e2 'if not, was there a gap between E1 and E2
Case Is > 1
cp121 = False
Case Else
Select Case DateDiff("d", Sheet1.[z1IE], Sheet1.[z1DA]) 'If not, was employment between E1/E2 6 mos?
Case Is >= 180
cp121 = True
Case Is < 180
cp121 = False
End Select
End Select
End Select
Case Sheet1.[z1CZ] 'end of egap is start of E1
Select Case DateDiff("d", Sheet1.[z1CZ], Sheet1.[z1DA]) 'was borrower employed by E1 for at least 6 mos?
Case Is >= 180
cp121 = True
Case Is < 180
cp121 = False
End Select
End Select
It's really just a big nested conditional structure. Don't use Select...Case for that. Use it e.g. when you're looking at some enum value:
Select Case MsgBox("Yes, no, or cancel?", vbYesNoCancel)
Case vbYes
'stuff
Case vbNo
'stuff
Case vbCancel
'stuff
End Select
Refactoring step 1: Turn all these 2-branch Select...Case blocks into If...Else...End If blocks. That should already take care of the "expression too complex" compile error.
Refactoring step 2: Implement Boolean assignments as such:
For example in one place two places you have:
Select Case DateDiff("d", Sheet1.[z1IE], Sheet1.[z1DA]) 'If not, was employment between E1/E2 6 mos?
Case Is >= 180
cp121 = True
Case Is < 180
cp121 = False
End Select
Replace that with:
cp121 = (DateDiff("d", Sheet1.[z1IE], Sheet1.[z1DA]) >= 180)
Refactoring step 3: Extract functions and procedures out of the If and Else branches where applicable; eliminate the duplicated branches, Don't Repeat Yourself.
Once you have something that works, take it to Code Review for further refactoring and simplification ideas.

Query Timeout Expired in SSAS Cube Processing

While migrating a cube from 2008 to 2014, we had a cube processing failure with the message "Query Timeout Expired : HYT00". I looked into the error information and found a certain query executing for more than an hour which is causing the issue. The query is,
SELECT [dbo_IndicatorFact].[PY] AS [dbo_IndicatorFactPY0_0],[dbo_IndicatorFact].[BP] AS [dbo_IndicatorFactBP0_1],[dbo_IndicatorFact].[RE] AS [dbo_IndicatorFactRE0_2],[dbo_IndicatorFact].[UCPY] AS [dbo_IndicatorFactUCPY0_3],[dbo_IndicatorFact].[UCBP] AS [dbo_IndicatorFactUCBP0_4],[dbo_IndicatorFact].[UCRE] AS [dbo_IndicatorFactUCRE0_5],[dbo_IndicatorFact].[GRPY] AS [dbo_IndicatorFactGRPY0_6],[dbo_IndicatorFact].[GRBP] AS [dbo_IndicatorFactGRBP0_7],[dbo_IndicatorFact].[GRRE] AS [dbo_IndicatorFactGRRE0_8],[dbo_IndicatorFact].[NRPY] AS [dbo_IndicatorFactNRPY0_9],[dbo_IndicatorFact].[NRBP] AS [dbo_IndicatorFactNRBP0_10],[dbo_IndicatorFact].[NRRE] AS [dbo_IndicatorFactNRRE0_11],[dbo_IndicatorFact].[GRC2] AS [dbo_IndicatorFactGRC20_12],[dbo_IndicatorFact].[AnalysisCategoryId] AS [dbo_IndicatorFactAnalysisCategoryId0_13],[dbo_IndicatorFact].[IndicatorTypeId] AS [dbo_IndicatorFactIndicatorTypeId0_14],[dbo_IndicatorFact].[IndicatorNameId] AS [dbo_IndicatorFactIndicatorNameId0_15],[dbo_IndicatorFact].[CategoryId] AS [dbo_IndicatorFactCategoryId0_16],[dbo_IndicatorFact].[CountryId] AS [dbo_IndicatorFactCountryId0_17],[dbo_IndicatorFact].[FiscalQuarterId] AS [dbo_IndicatorFactFiscalQuarterId0_18]
FROM
(
SELECT vwIndicatorFact.IndicatorId AS Id,
CASE vwIndicatorFact.IndicatorTypeId WHEN 1 THEN vwIndicatorFact.PY ELSE CASE IndicatorFact_GRC2.PY WHEN 0 THEN 0 ELSE vwIndicatorFact.PY END END AS PY,
CASE vwIndicatorFact.IndicatorTypeId WHEN 1 THEN vwIndicatorFact.BP ELSE CASE IndicatorFact_GRC2.BP WHEN 0 THEN 0 ELSE vwIndicatorFact.BP END END AS BP,
CASE vwIndicatorFact.IndicatorTypeId WHEN 1 THEN vwIndicatorFact.RE ELSE CASE IndicatorFact_GRC2.RE WHEN 0 THEN 0 ELSE vwIndicatorFact.RE END END AS RE,
vwIndicatorFact.BPvsPY, vwIndicatorFact.BPvsPYpercent, vwIndicatorFact.REvsBP, vwIndicatorFact.REvsBPpercent, vwIndicatorFact.REvsPY,
vwIndicatorFact.REvsPYpercent,
CASE vwIndicatorFact.IndicatorTypeId WHEN 1 THEN CASE vwIndicatorFact.IndicatorNameId WHEN 1 THEN 1000000 ELSE IndicatorFact_UC.PY END ELSE CASE IndicatorFact_GRC2.PY
WHEN 0 THEN 0 ELSE CASE vwIndicatorFact.IndicatorNameId WHEN 1 THEN 1000000 ELSE IndicatorFact_UC.PY END END END AS UCPY,
CASE vwIndicatorFact.IndicatorTypeId WHEN 1 THEN CASE vwIndicatorFact.IndicatorNameId WHEN 1 THEN 1000000 ELSE IndicatorFact_UC.BP END ELSE CASE IndicatorFact_GRC2.BP
WHEN 0 THEN 0 ELSE CASE vwIndicatorFact.IndicatorNameId WHEN 1 THEN 1000000 ELSE IndicatorFact_UC.BP END END END AS UCBP,
CASE vwIndicatorFact.IndicatorTypeId WHEN 1 THEN CASE vwIndicatorFact.IndicatorNameId WHEN 1 THEN 1000000 ELSE IndicatorFact_UC.RE END ELSE CASE IndicatorFact_GRC2.RE
WHEN 0 THEN 0 ELSE CASE vwIndicatorFact.IndicatorNameId WHEN 1 THEN 1000000 ELSE IndicatorFact_UC.RE END END END AS UCRE, vwIndicatorFact.IndicatorNameId,
vwIndicatorFact.CategoryId, vwIndicatorFact.AnalysisCategoryId, vwIndicatorFact.CountryId, vwIndicatorFact.FiscalQuarterId, vwIndicatorFact.IndicatorTypeId,
CASE vwIndicatorFact.IndicatorTypeId WHEN 1 THEN IndicatorFact_GR.PY ELSE CASE IndicatorFact_GRC2.PY WHEN 0 THEN 0 ELSE IndicatorFact_GR.PY END END AS GRPY,
CASE vwIndicatorFact.IndicatorTypeId WHEN 1 THEN IndicatorFact_GR.BP ELSE CASE IndicatorFact_GRC2.BP WHEN 0 THEN 0 ELSE IndicatorFact_GR.BP END END AS GRBP,
CASE vwIndicatorFact.IndicatorTypeId WHEN 1 THEN IndicatorFact_GR.RE ELSE CASE IndicatorFact_GRC2.RE WHEN 0 THEN 0 ELSE IndicatorFact_GR.RE END END AS GRRE,
CASE vwIndicatorFact.IndicatorTypeId WHEN 1 THEN IndicatorFact_NR.PY ELSE CASE IndicatorFact_GRC2.PY WHEN 0 THEN 0 ELSE IndicatorFact_NR.PY END END AS NRPY,
CASE vwIndicatorFact.IndicatorTypeId WHEN 1 THEN IndicatorFact_NR.BP ELSE CASE IndicatorFact_GRC2.BP WHEN 0 THEN 0 ELSE IndicatorFact_NR.BP END END AS NRBP,
CASE vwIndicatorFact.IndicatorTypeId WHEN 1 THEN IndicatorFact_NR.RE ELSE CASE IndicatorFact_GRC2.RE WHEN 0 THEN 0 ELSE IndicatorFact_NR.RE END END AS NRRE,
IndicatorFact_GRC2.BP AS GRC2
FROM
dbo.vwIndicatorFact INNER JOIN
dbo.vwIndicatorFact AS IndicatorFact_UC ON vwIndicatorFact.IndicatorTypeId = IndicatorFact_UC.IndicatorTypeId AND
vwIndicatorFact.FiscalQuarterId = IndicatorFact_UC.FiscalQuarterId AND vwIndicatorFact.CountryId = IndicatorFact_UC.CountryId AND
vwIndicatorFact.CategoryId = IndicatorFact_UC.CategoryId AND IndicatorFact_UC.IndicatorNameId = 1 LEFT OUTER JOIN
dbo.vwIndicatorFact AS IndicatorFact_GR ON vwIndicatorFact.IndicatorTypeId = IndicatorFact_GR.IndicatorTypeId AND
vwIndicatorFact.FiscalQuarterId = IndicatorFact_GR.FiscalQuarterId AND vwIndicatorFact.CountryId = IndicatorFact_GR.CountryId AND
vwIndicatorFact.CategoryId = IndicatorFact_GR.CategoryId AND IndicatorFact_GR.IndicatorNameId = 3 AND
IndicatorFact_GR.AnalysisCategoryId = vwIndicatorFact.AnalysisCategoryId LEFT OUTER JOIN
dbo.vwIndicatorFact AS IndicatorFact_NR ON vwIndicatorFact.IndicatorTypeId = IndicatorFact_NR.IndicatorTypeId AND
vwIndicatorFact.FiscalQuarterId = IndicatorFact_NR.FiscalQuarterId AND vwIndicatorFact.CountryId = IndicatorFact_NR.CountryId AND
vwIndicatorFact.CategoryId = IndicatorFact_NR.CategoryId AND IndicatorFact_NR.IndicatorNameId = 5 AND
IndicatorFact_NR.AnalysisCategoryId = vwIndicatorFact.AnalysisCategoryId LEFT OUTER JOIN
dbo.vwIndicatorFact AS IndicatorFact_GRC2 ON IndicatorFact_GRC2.IndicatorTypeId = 2 AND vwIndicatorFact.FiscalQuarterId = IndicatorFact_GRC2.FiscalQuarterId AND
vwIndicatorFact.CountryId = IndicatorFact_GRC2.CountryId AND vwIndicatorFact.CategoryId = IndicatorFact_GRC2.CategoryId AND
IndicatorFact_GRC2.IndicatorNameId = 3 AND IndicatorFact_GRC2.AnalysisCategoryId = 2
)
AS [dbo_IndicatorFact]
This is basically multiple self joins on a particular view which contains 300k records. Our dba updated all the indexes and updated the stats, But we are still not able to execute this query quickly. If this query executes quicker, there's a chance that the cube will process quicker. The data in the view also has no inconsistencies. Need some advice on what could be the issue here.
Some background on this - this is part of migration project that we are working on. The old dev environment is able to execute the same query in about 20 seconds with similar number of records in the view. The new dev takes forever to execute the same query with the same view.
In your statement it can be anything: poor indexes, outdated statistics, bad statement, huge data....
I had similar problem and I've increased external command timeout in SSAS(it is 1 hour by default). Look here how to do it if you will not manage to optimize your query:
http://www.msbiguide.com/2013/01/how-to-increase-externalcommandtimeout-in-ssas/

Combining Case Statements in a View

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