Adding an iif statement or where clause - reporting-services

Here is my current expression
=IIf(Fields!Units_Sold.Value = 0, 0, Fields!Total_Incidents.Value / IIf(Fields!Units_Sold.Value = 0, 1, Fields!Units_Sold.Value))
I need to add where Fields!.TECHNOLOGY = "Wired" to the above statement I tried a couple different things but with no success.

Try this:
=IIf(Sum(iif(Fields!technology.Value = "wired",Fields!Units_Sold.Value,0)) = 0,
0,
sum(iif(Fields!technology.Value="wired",Fields!Total_Incidents.Value,0)) /
IIf(Sum(iif(Fields!technology.Value = "wired",
Fields!Units_Sold.Value,0)) = 0, 1,
Sum(iif(Fields!technology.Value = "wired",Fields!Units_Sold.Value,0))
)
)
It is not tested but should work. Let me know if this could help you

Related

MS Access Case sensitive query giving incorrect result

Why do these queries give different results? Reference is a single character column and I would expect to have a result giving counts for upper and lower case letter 'r'.
Select SUM(IIF(StrComp([REFERENCE],'R',0) = 0, 1, 0)) AS BIG_R,
SUM(IIF(StrComp([REFERENCE],'r',0) = 0, 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
The result is that both BIG_R and LITTLE_R are the same and equal the count of BIG_R's
However,
Select SUM(IIF(StrComp([REFERENCE],'r',0) = 0, 1, 0)) AS LITTE_R,
SUM(IIF(StrComp([REFERENCE],'R',0) = 0, 1, 0)) AS BIG_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Again LITTLE_R and BIG_R are the same, but this time they equal the count of LITTLE_R's
This looks like a bug in the way MS Access processes this type of query, or have I missed something here?
Access (or probably rather JetEngine) thinks that StrComp is called twice with the same argument and optimizes away one of the two calls.
A workaround is to compare the ASCII character values (Asc("r") = 114, Asc("R") = 82):
Select
SUM(IIF(Asc([REFERENCE]) = Asc('R'), 1, 0)) AS BIG_R,
SUM(IIF(Asc([REFERENCE]) = Asc('r'), 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Yet another workaround:
Select SUM(IIF(StrComp([REFERENCE],Chr$(82),0) = 0, 1, 0)) AS BIG_R,
SUM(IIF(StrComp([REFERENCE],Chr$(114),0) = 0, 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Here the two inputs to StrComp are clearly different. So, the second call not optimized away.

SSRS Divide by Zero error on totals with weighted average

When attempting to get a total weighted average interest rate I occasionally receive Error when there is only one item in certain columns. Having trouble with the Iif statement handling this:
=Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 0, SUM(Fields!Current_Principal_Balance.Value * Fields!WAIR.Value))/Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 1, SUM(Fields!Current_Principal_Balance.Value))
Moved your brackets slightly, this seems to work:
=Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 0,
SUM(Fields!Current_Principal_Balance.Value * Fields!WAIR.Value)/Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 1, SUM(Fields!Current_Principal_Balance.Value)))

MS Access 2016 Reference a field on a sub Report within a Form

I had a form with a sub form. The form contained the following VBA conditional formatting and everything worked like a charm:
Private Sub Form_Current()
x = Date + 183
y = Date + 91
z = Date + 30
On Error Resume Next
Select Case Me.tblContractTotalOptionsIncluded
Case Is = Me!subfrmContractCLIN.Form.tblContractCLINDescriptionID - 1
Select Case Me!subfrmContractCLIN.Form.tblContractCLINExpirationDate
Case Is < Date
Me.tblContractNumber.BackColor = RGB(70, 0, 0)
Me.tblContractNumber.FontBold = True
Me.tblContractNumber.ForeColor = RGB(255, 0, 0)
End Select
Case Is > 5
Me.tblContractNumber.BackColor = RGB(70, 0, 0)
Me.tblContractNumber.FontBold = True
Me.tblContractNumber.ForeColor = RGB(255, 0, 0)
Case Else
Select Case Me!subfrmContractCLIN.Form.tblContractCLINExpirationDate
Case Is < Date
Me.tblContractNumber.BackColor = RGB(0, 0, 0)
Me.tblContractNumber.FontBold = True
Me.tblContractNumber.ForeColor = RGB(255, 255, 255)
Case Is < z
Me.tblContractNumber.BackColor = RGB(255, 0, 0)
Me.tblContractNumber.FontBold = True
Me.tblContractNumber.ForeColor = RGB(255, 242, 0)
Case Is < y
Me.tblContractNumber.BackColor = RGB(255, 242, 0)
Me.tblContractNumber.FontBold = True
Me.tblContractNumber.ForeColor = RGB(255, 0, 0)
Case Is < x
Me.tblContractNumber.BackColor = RGB(255, 242, 0)
Me.tblContractNumber.FontBold = True
Me.tblContractNumber.ForeColor = RGB(0, 0, 0)
Case Else
Me.tblContractNumber.BackColor = RGB(255, 255, 255)
Me.tblContractNumber.FontBold = False
Me.tblContractNumber.ForeColor = RGB(0, 0, 0)
End Select
End Select
End Sub
My users complained that they wanted things to look more like a F$%#*$g excel spreadsheet. So being the accommodating guy that I am, amongst other aesthetic changes, I’ve changed the “subfrmContractCLIN” sub form to a report named “subrptContractCLIN” because the Can Grow properties on reports work better than they do on a continuous form. To make the report I copied the Record Source SQL from the sub form and used it for the sub report. All the fields I had on the sub form are included in the sub report.
The following is an excerpt of the above code with my changes. As you may have guessed the code below does not work.
Select Case Me.tblContractTotalOptionsIncluded
Case Is = Me!subrptContractCLIN.Report.tblContractCLINDescriptionID - 1
Select Case Me!subrptContractCLIN.Report.tblContractCLINExpirationDate
Case Is < Date
Me.tblContractNumber.BackColor = RGB(70, 0, 0)
Me.tblContractNumber.FontBold = True
Me.tblContractNumber.ForeColor = RGB(255, 0, 0)
End Select
Case Is > 5
Me.tblContractNumber.BackColor = RGB(70, 0, 0)
Me.tblContractNumber.FontBold = True
Me.tblContractNumber.ForeColor = RGB(255, 0, 0)
Case Else
Select Case Me!subrptContractCLIN.Report.tblContractCLINExpirationDate
Any assistance you can provide me so I can help my excel loving, metathesiophobic users sleep at night would be greatly appreciated.

How do you properly construct an IF EXIST UPDATE ELSE INSERT?

I have a tables containing the dollars a customer spends as well as points they earn. I have a master table that I intend to accumulate the points/dollars and redistribute them to the stores so that points are shared accross all stores. The query to determine the difference of the master table (CMCustomer) and the store tables (cm01, cm03, etc...) are as follows, a separate query for each store.
use customer;
truncate cm01process;
INSERT INTO cm01process
select cm01.CustomerNumber,
cm01.LastName,
cm01.FirstName,
cm01.Address,
cm01.City,
cm01.State,
cm01.ZIPCode,
cm01.PhoneNo,
cm01.DriverLicenseNo,
cm01.SocialSecNo,
cm01.TaxExempt,
cm01.ExternalRefNumber,
cm01.AuxField,
cm01.Comments,
cm01.FSLevelNo,
cm01.FSDateOpened,
cm01.FSLastVisit,
IFNULL(IF( (cm01.FSVisitsToDate - CMCustomer.FSVisitsToDate) < 0, 0, (cm01.FSVisitsToDate - CMCustomer.FSVisitsToDate) ), 0 ) AS FSVisitsToDate,
IFNULL(IF( (cm01.FSVisitsThisPeriod - CMCustomer.FSVisitsThisPeriod) < 0, 0, (cm01.FSVisitsThisPeriod - CMCustomer.FSVisitsThisPeriod) ), 0 ) AS FSVisitsThisPeriod,
IFNULL(IF( (cm01.FSPurchaseToDate - CMCustomer.FSPurchaseToDate) < 0, 0, (cm01.FSPurchaseToDate - CMCustomer.FSPurchaseToDate) ), 0 ) AS FSPurchaseToDate,
IFNULL(IF( (cm01.FSPurchaseThisPeriod - CMCustomer.FSPurchaseThisPeriod) < 0, 0, (cm01.FSPurchaseThisPeriod - CMCustomer.FSPurchaseThisPeriod) ), 0 ) AS FSPurchaseThisPeriod,
IFNULL(IF( (cm01.FSDiscountToDate - CMCustomer.FSDiscountToDate) < 0, 0, (cm01.FSDiscountToDate - CMCustomer.FSDiscountToDate) ), 0 ) AS FSDiscountToDate,
IFNULL(IF( (cm01.FSDiscountThisPeriod - CMCustomer.FSDiscountThisPeriod) < 0, 0, (cm01.FSDiscountThisPeriod - CMCustomer.FSDiscountThisPeriod) ), 0 ) AS FSDiscountThisPeriod,
IFNULL(IF( (cm01.FSPointsToDate - CMCustomer.FSPointsToDate) < 0, 0, (cm01.FSPointsToDate - CMCustomer.FSPointsToDate) ), 0 ) AS FSPointsToDate,
IFNULL(IF( (cm01.FSPointsThisPeriod - CMCustomer.FSPointsThisPeriod) < 0, 0, (cm01.FSPointsThisPeriod - CMCustomer.FSPointsThisPeriod) ), 0 ) AS FSPointsThisPeriod,
IFNULL(IF( (cm01.FSPromoPointsToDate - CMCustomer.FSPromoPointsToDate) < 0, 0, (cm01.FSPromoPointsToDate - CMCustomer.FSPromoPointsToDate) ), 0 ) AS FSPromoPointsToDate,
IFNULL(IF( (cm01.FSPromoPointsThisPeriod - CMCustomer.FSPromoPointsThisPeriod) < 0, 0, (cm01.FSPromoPointsThisPeriod - CMCustomer.FSPromoPointsThisPeriod) ), 0 ) AS FSPromoPointsThisPeriod,
cm01.LastUpdated
from cm01
left join CMCustomer on cm01.CustomerNumber = CMCustomer.CustomerNumber;
This query works fine. The next query is where I begin to have problems.
-EDIT-
I have updated the second query. It is not producing errors and appears to do mostly what I want however it is not adding properly. In the query below it should insert into the new table (processData) the fields from a selection. On duplicates it is supposed to update the fields specified and on new rows just add them. It appears to work ok except when the UPDATE is performed it returns all zeroes in those columns. Example: CMCustomer has 55 points, cm01process had 0 points. Instead of adding the two points together and giving me 55 it is returning 0. Any ideas what I have wrong here?
use customer;
INSERT INTO processData (CustomerNumber,
LastName,
FirstName,
Address,
City,
State,
ZIPCode,
PhoneNo,
DriverLicenseNo,
SocialSecNo,
TaxExempt,
ExternalRefNumber,
AuxField,
Comments,
FSLevelNo,
FSDateOpened,
FSLastVisit,
FSVisitsToDate,
FSVisitsThisPeriod,
FSPurchaseToDate,
FSPurchaseThisPeriod,
FSDiscountToDate,
FSDiscountThisPeriod,
FSPointsToDate,
FSPointsThisPeriod,
FSPromoPointsToDate,
FSPromoPointsThisPeriod,
LastUpdated)
SELECT cm01process.CustomerNumber,
cm01process.LastName,
cm01process.FirstName,
cm01process.Address,
cm01process.City,
cm01process.State,
cm01process.ZIPCode,
cm01process.PhoneNo,
cm01process.DriverLicenseNo,
cm01process.SocialSecNo,
cm01process.TaxExempt,
cm01process.ExternalRefNumber,
cm01process.AuxField,
cm01process.Comments,
cm01process.FSLevelNo,
cm01process.FSDateOpened,
cm01process.FSLastVisit,
cm01process.FSVisitsToDate,
cm01process.FSVisitsThisPeriod,
cm01process.FSPurchaseToDate,
cm01process.FSPurchaseThisPeriod,
cm01process.FSDiscountToDate,
cm01process.FSDiscountThisPeriod,
cm01process.FSPointsToDate,
cm01process.FSPointsThisPeriod,
cm01process.FSPromoPointsToDate,
cm01process.FSPromoPointsThisPeriod,
cm01process.LastUpdated
FROM cm01process
LEFT JOIN CMCustomer ON cm01process.CustomerNumber = CMCustomer.CustomerNumber
ON DUPLICATE KEY UPDATE
processData.FSVisitsToDate = CMCustomer.FSVisitsToDate + cm01process.FSVisitsToDate,
processData.FSVisitsThisPeriod = CMCustomer.FSVisitsThisPeriod + cm01process.FSVisitsThisPeriod,
processData.FSPurchaseToDate = CMCustomer.FSPurchaseToDate + cm01process.FSPurchaseToDate,
processData.FSPurchaseThisPeriod = CMCustomer.FSPurchaseThisPeriod + cm01process.FSPurchaseThisPeriod,
processData.FSDiscountToDate = CMCustomer.FSDiscountToDate + cm01process.FSDiscountToDate,
processData.FSDiscountThisPeriod = CMCustomer.FSDiscountThisPeriod + cm01process.FSDiscountThisPeriod,
processData.FSPointsToDate = CMCustomer.FSPointsToDate + cm01process.FSPointsToDate,
processData.FSPointsThisPeriod = CMCustomer.FSPointsThisPeriod + cm01process.FSPointsThisPeriod,
processData.FSPromoPointsToDate = CMCustomer.FSPromoPointsToDate + cm01process.FSPromoPointsToDate,
processData.FSPromoPointsThisPeriod = CMCustomer.FSPromoPointsThisPeriod + cm01process.FSPromoPointsThisPeriod

add 2 values together in an ssrs-expression

I'm looking to bring in my expression 2 values that I what to add together.
=Sum(iif(Fields!Leadsource.Value = "set1", 1, 0) and (Fields!Leadsource.Value = "set", 1, 0))
but is just coming back as 0 when the value should be 400 or so.
Can any one point me in the right direction?
I'm not sure how SSRS evaluates your expression
=Sum(iif(Fields!Leadsource.Value = "set1", 1, 0) and (Fields!Leadsource.Value = "set", 1, 0))
I think SUM(1 AND 0) and SUM(1 AND 1) both equal 1.
Your expression needs to be changed a little - though I'm not sure which you need.
=Sum(IIF(Fields!Leadsource.Value = "set1" OR Fields!Leadsource.Value = "set", 1, 0))
Otherwise if you want to count the two different criteria separately, use:
=Sum(IIF(Fields!Leadsource.Value = "set1", 1, 0) + (Fields!Leadsource.Value = "set", 1, 0))