Combining Case Statements in a View - sql

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

Related

RunningSum inside another

I have a report with the following data
IdSource 20170629 20170628 20170626 20170625 20170624 20170623
Id1. OK KO N/A KO OK KO
I want to count the number of days my data (status of a workflow) is KO. N/A means the worflow is not expected, so it should not be counted.
Expected results :
IdSource 20170629 20170628 20170626 20170625 20170624 20170623
Id1. OK KO(2) N/A KO(1) OK KO(1)
The count must be reset at each OK.
In SQL, I would do
select t.*,
sum(case when status = 'KO' then 1 else 0 end) over (partition by id, cume_ko order by date) as nbDayKO
from (select t.*,
sum(case when status = 'OK' then 1 else 0 end) over (partition by id order by date) as cume_ko
from t
) t
I tried to use the RunningSum function without success:
I need to inverse the sort on the date
I cannot use the result of a runningSum inside another.
Thanks for your help
It's ugly and not very efficient, but I did not find any other solutions. Here is how I did it :
If ([status] = 0) Then 0
Else (
(If (RelativeValue([status];([dayOfWeek]);1) = 1) Then
If (RelativeValue([status];([dayOfWeek]);2) = 1) Then
If (RelativeValue([status];([dayOfWeek]);3) = 1) Then
If (RelativeValue([status];([dayOfWeek]);4) = 1) Then
If (RelativeValue([status];([dayOfWeek]);5) = 1) Then
If (RelativeValue([status];([dayOfWeek]);6) = 1) Then
If (RelativeValue([status];([dayOfWeek]);7) = 1) Then
If (RelativeValue([status];([dayOfWeek]);8) = 1) Then
If (RelativeValue([status];([dayOfWeek]);9) = 1) Then
If (RelativeValue([status];([dayOfWeek]);10) = 1) Then
If (RelativeValue([status];([dayOfWeek]);11) = 1) Then
If (RelativeValue([status];([dayOfWeek]);12) = 1) Then
If (RelativeValue([status];([dayOfWeek]);13) = 1) Then
If (RelativeValue([status];([dayOfWeek]);14) = 1) Then
If (RelativeValue([status];([dayOfWeek]);15) = 1) Then 15
Else 14
Else 13
Else 12
Else 11
Else 10
Else 9
Else 8
Else 7
Else 6
Else 5
Else 4
Else 3
Else 2
Else 1
Else 0)+[status])

Rewrite subquery with calculation in SQL to CASE statement

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

Is there a way to ignore the AND in a CASE when something is true?

I have this Where clause
Select * From Student_Info si
Inner Join Certifications cc
Inner Join Cert_Earned ce
Where si.grad_date = #grad_date
AND cc.org_no = #org_no
but I need an additional AND that should be ignored if it turns out the value is false, I will want ALL certificates
AND cc.industrial = CASE WHEN #industrial = 0 THEN Do Nothing
Else #industrial
This would normally be expressed as:
AND (#industrial = 0 OR ccc.industrial = #industrial)
It sounds like you just want to add a predicate that does an OR between two different conditions
AND (#industrial = 0 or ccc.industrial = #industrial)
You can do something like this with CASE functions:
AND 1 = (CASE WHEN #industrial = 0 THEN 1
ELSE CASE WHEN cc.industrial = #industrial THEN 1 END
END)
or if cc.industrial is not nullable, then maybe:
AND cc.industrial = CASE WHEN #industrial = 0 THEN cc.industrial
Else #industrial END

Converting an IIF statement into case statement

Can someone convert this statement into CASE Statement in SQL Server?
IIf((LoanBalance)=0 And CurrentPrincipal<>0 And EndPaymentDate>MaxOfPayment_Date,iif(Pledged_Loan=True,1,2), IIf(LoanBalance=0 And currentprincipal<>0,3,IIf(OffCycle_Payment=True,4,5))) AS POFF
CASE statements cascade (meaning the first matching condition is chosen and no others). So, you don't have to nest the logic using case:
SELECT (CASE WHEN LoanBalance = 0 And CurrentPrincipal <> 0 And
EndPaymentDate > MaxOfPayment_Date and
Pledged_Loan = True
THEN 1
WHEN LoanBalance = 0 And CurrentPrincipal <> 0 And
EndPaymentDate > MaxOfPayment_Date
THEN 2
WHEN LoanBalance = 0 And CurrentPrincipal <> 0
THEN 3
WHEN OffCycle_Payment = True
THEN 4
ELSE 5
END) AS POFF
I think that would be something about like this:
CASE WHEN LoanBalance=0
And CurrentPrincipal<>0
And EndPaymentDate>MaxOfPayment_Date
THEN CASE WHEN Pledged_Loan=True THEN 1 ELSE 2 END
WHEN LoanBalance=0
And currentprincipal<>0
THEN 3
WHEN OffCycle_Payment=True
THEN 4 ELSE 5 END END AS POFF
Ah, and I believe boolean in SQL Server would be stored as a bit, so that would make it this:
CASE WHEN LoanBalance=0
And CurrentPrincipal<>0
And EndPaymentDate>MaxOfPayment_Date
THEN CASE WHEN Pledged_Loan=1 THEN 1 ELSE 2 END
WHEN LoanBalance=0
And currentprincipal<>0
THEN 3
WHEN OffCycle_Payment=1
THEN 4 ELSE 5 END END AS POFF
Try this
SELECT CASE WHEN LoanBalance = 0
AND CurrentPrincipal <> 0
AND EndPaymentDate > MaxOfPayment_Date
THEN CASE WHEN Pledged_Loan
THEN 1
ELSE 2
END
ELSE CASE WHEN LoanBalance = 0 AND currentprincipal <> 0
THEN 3
ELSE CASE WHEN OffCycle_Payment
THEN 4
ELSE 5
END
END
END AS POFF

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/