iif blank show zero - sql

I have a visual studio report that I want to state that if a calculation box is blank due to there being no figures available on that particluar project then show zero.
My calculation is :- =((Sum(Fields!TotalCost.Value, "Accrued") + Sum(Fields!TotalCost.Value, "serv1")) /
(Sum(Fields!Quantity.Value, "serv1") + Sum(Fields!Quantity.Value, "Accrued"))
)
And I want to try and include an IIF statement in that to show zero if blank.
Any ideas on how to best achieve my aim?
so far I have got to
=iif((Sum(Fields!TotalCost.Value, "Accrued") + Sum(Fields!TotalCost.Value, "serv1")) /
(Sum(Fields!Quantity.Value, "serv1") + Sum(Fields!Quantity.Value, "Accrued"))
) = "" ,false,0 ) but I am getting a little confused.

Most likely value is not blank string but a Nothing. Try following construct:
=IIf(IsNothing(((Sum(Fields!TotalCost.Value, "Accrued") + Sum(Fields!TotalCost.Value, "serv1")) / (Sum(Fields!Quantity.Value, "serv1") + Sum(Fields!Quantity.Value, "Accrued"))), 0, ((Sum(Fields!TotalCost.Value, "Accrued") + Sum(Fields!TotalCost.Value, "serv1")) / (Sum(Fields!Quantity.Value, "serv1") + Sum(Fields!Quantity.Value, "Accrued")))
It's a bit awkward since you have to repeat the expression twice, to avoid it you may want to write a custom function.

Related

Trying to do a simple expression in SSRS

I am trying to calculate the average price of an order. I have the following simple expression:
=( Max(Fields!Price, "Totals") - Max(Fields!Discount_Value.Value, "Totals") + Max(Fields!Tax_Value.Value, "Totals") + Max(Fields!Freight_Charges.Value, "Totals") - Max(Fields!Total_Freight_Cost.Value, "ShipCodesShipped") / Max(Fields!Total_Orders_Count.Value, "Totals"))
It does not seem to work at all and just shows up as #Error. Does anyone know what I'm doing wrong?
The first part of your expression
=( Max(Fields!Price, "Totals") ...
should be
=(Max(Fields!Price.Value, "Totals") ...

Can't get delphi SQL LIKE to work with %

I'm doing a school project and need to code a query to filter a dataset to certain variables. All my SQL works fine, except I can't get the LIKE statement to work with %-signs. I believe my syntax is wrong. Can anybody please tell me what I'm doing wrong. Thanks
The code:
qryMovie.SQL.Clear;
qryMovie.SQL.Add('SELECT * FROM Movies');
qryMovie.SQL.Add('WHERE Genre = ' + QuotedStr(genre));
qryMovie.SQL.Add('AND Price BETWEEN ' + minPrice + ' AND ' + maxPrice);
qryMovie.SQL.Add('AND Title LIKE %' + title + '%');
qryMovie.Open;
Error produced:
'Syntax error in query expression 'Genre = 'Action/Adventure'
AND Price BETWEEN 0 AND 200
AND Title LIKE %Star Wars%''
LIKE %Star Wars%
but you need
LIKE '%Star Wars%'
You need to quote % with ':
qryMovie.SQL.Add(' AND Title LIKE ''%' + title + '%''');
Anyway you should use binded parameters instead of concatenating SQL string. It is error-prone and could lead to SQL Injection attacks.

Tableau Calculated Fields IF THEN Statement adding multiple fields

I'm exploring Consumer Expenditure microdata (individual level data) from BLS and I'm looking to create a new field for investable assets by adding a number of different fields and bucketing respondents into $250K+ and <$250K. I'm using Tableau Public.
My formula is below. Various fields are things like total value of stock holdings, retirement accounts, checking & saving accounts, etc.
If [Irax] + [Irabx] + [Liquidb] + [Liquidbx] + [Othastx] + [Othastbx] + [Stockbx] + [Stockx] >= 250000 THEN "$250K+"
ELSEIF [Irax] + [Irabx] + [Liquidb] + [Liquidbx] + [Othastx] + [Othastbx] + [Stockbx] + [Stockx] > 250000 THEN "<$250K"
END
The calculation is valid, however the result is not accurate. The formula buckets everyone into the >$250K bucket, even though there are clearly individuals that have over that amount.
What is happening here?
Define a field called investable assets =
[Irax] + [Irabx] + [Liquidb] + [Liquidbx] + [Othastx] + [Othastbx] + [Stockbx] + [Stockx]
Then define a numeric parameter called [investment threshold] defaulting to 250000
Then finally a calculated field called rich guy =
SUM([investable assets]) > [investment threshold]
Now you can use [rich guy] as desired, and tweak your parameter interactively.
There are other variations, you could use LOD calcs or sets instead. You can define an alias for [rich guy] to display "Loaded and Broke" instead of "True and False". But this is a typical approach for spotlighting.
BTW, the only thing that is especially different than your approach is the use of the function SUM()

Crystal Reports 8.5 showing multi-values from parameters on report footer

In Crystal Reports 8.5 when I have setup a parameter for multi-value the user enters 90654-90658A. Normally I would use Join() but being that this is not just text but numeric I have tried a few things but with no results.
Local NumberVar i;
Local NumberVar j;
Local StringVar param_values;
if 0 in {?CPT} then
"CPT #s: All CPTs"
else
(
for i := 1 to UBound ({?CPT}) do
for j := Minimum ({?CPT}[ i ]) to Maximum ({?CPT}[ i ]) do
param_values := param_values + "," + CStr (j, "#");
"CPT #s: " + Mid (param_values, 2)
)
This works fine for 90654-90658 but when the user selects 90654-90658A it fails.
Also the selection criteria will not pass to SQL in the query sent to SQL with the correct where clause. Meaning there is not indication that I am even asking for a where. It should show in the select for sql a where table.data >= '90654' and table.data <= '90658A'
I am lost as to where I am going wrong with this. Any help would be great this is my first time seeking an answer on this site but I have not received any help on this request.
Thanks
I tried a similar query with the Xtreme.mdb database, referencing the Customer table. I created a string, range parameter that accepted multiple values (i.e. multiple ranges).
When I supplied it with two ranges, the follow query was generated:
SELECT `Customer`.`Postal Code`
FROM `Customer` `Customer`
WHERE (
(`Customer`.`Postal Code`>='04000' AND `Customer`.`Postal Code`<='04999') OR
(`Customer`.`Postal Code`>='55000' AND `Customer`.`Postal Code`<='55999')
)
As you can see, Crystal Reports will build the necessary BETWEEN or >= <= statements.
In you situation, try:
( "0" IN {?CPT} OR {TABLE.FIELD} IN {?CPT} )
You could adapt your formula field to display the values of the parameter, if you want.
I do appreciate everyones input but I was able to work through the problem. For the record selection I put in the following. {TABLE.FIELD} in CStr({#MinCPT}) to CStr({#MaxCPT}). This pulled the range after I created two formulas. One MinCPT and the other MaxCPT. Here is the formula. Left (ToText (Minimum ({?CPT})),2 ) & Mid (ToText (Minimum ({?CPT})),4 ,3 ) and the same for Max. The report works fine now.
Thanks Again.

MDX CurrentMember with SSAS 2008 doesn't work as stated by MSDN

First of all I use the SQL Management Studio for this query (no Excel 2007 that seems to have problems):
WITH
SET [Project period dates] AS
{
StrToMember("[Time].[Date].&[" + [Project].[ParentProject].CURRENTMEMBER.PROPERTIES("Project Start Iso") + "]"):
StrToMember("[Time].[Date].&[" + [Project].[ParentProject].CURRENTMEMBER.PROPERTIES("Project End Iso") + "]")
}
MEMBER [Measures].[Test] AS ([Project period dates].COUNT)
SELECT
{
[Measures].[Test]
}
on 0,
NONEMPTY ([Project].[ParentProject].MEMBERS)
DIMENSION PROPERTIES [Project].[ParentProject].[Project Duration], [Project].[ParentProject].[Project Start Iso], [Project].[ParentProject].[Project End Iso]
on 1
FROM
[MyCube]
WHERE
(
[Orgunit].[Orgunit].&[448]
)
This query delivers a list of projects with its three properties and a calculated member that is based upon my calculated set. The properties show the right values, but the calculated member shows always the same: the result of the very first project it should be calculated for.
I don't really understand why, because MSDN says:
The current member changes on a hierarchy used on an axis in a query.
Therefore, the current member on other hierarchies on the same
dimension that are not used on an axis can also change; this behavior
is called 'auto-exists'.
They give examples with calculated members, but I think that should also work with calculated sets, I have read that query-based calculated sets are dynamic by nature. Maybe somebody can tell me if I understood that wrong or what else is my problem here.
The named set are only computed once within a query. That is why your calculated member always return the same value.
You just have to remove the named set from your query:
MEMBER [Measures].[Test] AS {
StrToMember("[Time].[Date].&[" + [Project].[ParentProject].CURRENTMEMBER.PROPERTIES("Project Start Iso") + "]"):
StrToMember("[Time].[Date].&[" + [Project].[ParentProject].CURRENTMEMBER.PROPERTIES("Project End Iso") + "]")
}.COUNT