Reporting Services - Count Column Values if equals A - reporting-services

I have a dataset called 'dsAllStuTargetData' - i'm trying to count the number of the 'A' values that appear in the column 'Target'.
I'm doing this using a textbox with an expression, I can count the total number of values using the following:
=Count(Fields!Target.Value, "dsAllStuTargetData")
However when I try to count where the value equals 'A' it doesn't work.
=Count(IIF(Fields!Target.Value, "dsAllStuTargetData")="A",1,0)

For this case you need a Sum, not a Count, i.e. something like:
=Sum(IIf(Fields!Target.Value = "A", 1, 0), "dsAllStuTargetData")
Count will just count the number of rows; the IIf doesn't do anything there - something like CountDistinct can be affected in certain cases but this will not work here.
However, Sum will take the total of all rows which meet the IIf condition, i.e. the total of all 1 values in the DataSet, which is what you're after.

IIF wants it's arguments in the format:
IIF(condition, true part, false part)
Which would equate to something like
Count(IIF(Fields!Target.Value = "A",1,0),"dsAllStuTargetData")
Does that work?

Related

exclude nulls in column SSRS

new to SSRS
I have table with column 1 is Department, and column 2 is The calculation
For example sum of the cost...
I have used the expression below to sum the cost
but I want to exclude the department that are null, but its no joy..
=Sum(IIF(Fields!ReturnOrder.Value = "1" + IsNothing(Fields!Department.Value) = 1, Fields!Cost.Value, 0))
column 1 is still showing the null department. I do not wish to show this...
I just want to have a column 1 showing the department names, that does not show the null rows.. and column 2 sum of cost where return value = 1
please help
To make your expression work the way you have it, you need to change the plus to an AND for logical operations and leave the ISNOTHING as a Boolean (without the = 1):
=Sum(IIF(Fields!ReturnOrder.Value = "1" AND NOT(IsNothing(Fields!Department.Value)), Fields!Cost.Value, 0))
I'm not sure what the ReturnOrder is for but left your condition in.
The expression you are looking for is
=Sum(IIF(Fields!ReturnOrder.Value = "1" AND IsNothing(Fields!Department.Value) = False, Fields!Cost.Value, 0))
Tip: In case your value is decimal instead of 0 use Cdec(0) to avoid errors.
To avoid displaying null departments I would suggest filtering your SQL query.
You can also do it by filtering the tablix
Expression: IsNothing(Fields!Department.Value)
Type: Boolean
Operator: Equal =
Value: False
Doing so will hide Null departments and your expression can be simplified to
=Sum(IIF(Fields!ReturnOrder.Value = "1", Fields!cost.Value, 0))
The best way is to alter your query:
SELECT *
FROM [YourTable]
WHERE [Department] IS NOT NULL;
You can also select the details row, click the Properties tab, and enter a formula in the "Hidden" property
=IIF(IsNothing(Fields!Department.value), True, False)
This says, "if department is null, hide this row, otherwise show it". The first method is better because less data is returned to your report. The second method requires that all rows are returned, and the report has to sort through which ones to show.
Select the detail row (click the three lines), select the Properties tab, and replace "False" with that formula:

MySQL Case not working ROUND the value

I'm trying to ROUND() or not the selected value. The query looks likes this:
SELECT b.Series,
CASE
WHEN Series = 'DMS' THEN ROUND(b.Quantity,0)
ELSE ROUND(b.Quantity,2)
END AS Quantity
FROM bill b
I also tried
CASE Series
WHEN 'DMS' THEN ROUND(b.Quantity,0)
ELSE ROUND(b.Quantity,2)
END AS Quantity,
and
IF(b.Series = 'DMS', ROUND(b.Quantity,0), ROUND(b.Quantity,2)) AS Quantity,
Every time I get the 2 decimals at the end.
When the Series is 'DMS' Quantity should be like an integer (without decimals), in the other cases Quantity should have two decimals.
In a result set, the data type is an attribute of the column for the entire result set.
For any given column in a result set, the value in that column for each row must necessarily be of the same data type.
The data type of the returned column for this query will, by necessity, be set by the server to something along the lines of DECIMAL(11,2) in order to accommodate all the possible values.
I would anticipate that what you are seeing is actually correctly rounded, but with an "unexpected" .00 at the end.
CAST(CASE ... END AS CHAR) AS Quantity would -- potentially -- get you a result that looks more like you're expecting, by casting everything to a string.
That's obviously some very sloppy type-handling, but it's no more unreasonable than expecting different types to emerge in the same column... which can't be done.
The more correct solution is to return them as two different columns, with two CASE expressions or IF().
ROUND(IF(Series = 'DMS',b.Quantity,NULL),0) AS dms_quantity,
ROUND(IF(Series = 'DMS',NULL,b.Quantity),2) AS non_dms_quantity
Note that both IF() tests evaluate the same expression and have their arguments reversed rather than the second one using != with the arguments the same, so that NULL values for series, if possible, are handled correctly by the second test. (Anything != NULL cannot evaluate to true; the third argument to IF() is used for both FALSE AND NULL results).

COUNTNULL() as a MySQL function. How would it operate?

I am sure it would be an aggregate function because it is going to count a collection of data.
However, how does any COUNT() function operate in MySQL to perform its respective actions?
Not 100% clear what you are looking for, but for selecting a count of null values in a column, I use something like this:
SELECT SUM(CASE WHEN columnname IS NULL THEN 1 ELSE 0 END) FROM tablename;
When the value is NULL, it is assigned the value 1 otherwise 0, then summed over whatever aggregate you need.
The COUNT(*) is an aggregate function. In the SELECT list, the expression COUNT(*) will return a count of rows. Without a GROUP BY clause, all rows will be collapsed into a single row, and the COUNT(*) aggregate will contain a non-negative integer value representing the number of rows that were collapsed... a "count" of the number of rows.
As you seem to be aware, other expressions involving the COUNT() aggregate operate a little differently, with respect to NULL values.
In the SELECT list, an expression COUNT(expr) operates exactly like COUNT(*) except for rows with values of expr that evaluate to NULL are not included in the count.
This all operates according to the specification.
As far as the non-existent COUNTNULL() function, it depends what you want that to achieve. If you wanted to get a count of the rows that had a NULL value for an expression, you could perform a conditional test, and return a non-NULL value, and use the existing COUNT aggregate, for example:
SELECT COUNT(CASE WHEN expr IS NULL THEN 1 ELSE NULL END) AS `COUNTNULL`
FROM ...
I don't remember where I learned this technique, but arguably the most elegant -- or at least minimalistic -- way to invert the logic of COUNT() is with this expression, which admittedly gives a first impression that black magic may somehow be involved... but it's perfectly legitimate:
COUNT(column1 IS NULL OR NULL)
...this correctly counts only the rows where column1 is null, because it is equivalent to the following expression...
COUNT( (column1 IS NULL) OR (NULL) )
It's a boolean expression that can only ever evaluate to 1 ("true," when column1 is null, and this row is thus counted), or NULL (otherwise, so the row will not be counted).
Logically, it's equivalent to the CASE expression offered by #spencer7593.

Trouble in SQL Summing a Word

I am trying to Sum() the column Status where Status = 'operational'. I am having trouble figuring out how to sum the actual word "operational".
I have tried multiple different variations of the statement below (the one I posted is the most basic form) but I get the error: data type varchar is invalid for sum operator.
Can anybody help?
SELECT SUM(status) As 'TotalOperationalSTIDevices'
FROM netinfo_device_details
WHERE LoopBackAddress LIKE '10.12%' AND Status = 'Operational'
Try
Select COUNT(*) As 'TotalOperationalSTIDevices' from netinfo_device_details where LoopBackAddress Like '10.12%' and Status = 'Operational'
You need to use COUNT:
SELECT COUNT(*) As TotalOperationalSTIDevices
FROM netinfo_device_details
WHERE LoopBackAddress LIKE '10.12%' AND Status = 'Operational';
The SUM aggregation function really does a SUM of a set of numbers. COUNT just counts the number of rows.
Since the actual content of the row is not relevant, you can use COUNT(*) instead of COUNT(status) if you want.

SSRS Row Number within table excluding hidden rows

I use the following expression to obtain a row number for a table in SSRS:
=RunningValue(CountDistinct("Table1"),Count,"Table1")
I also use an expression for the row visibility property. Lets just say that the Visibility expression is
=IIf(Fields!MyField.Value + Fields!MyField.Value <> 0, False, True)
My expression for the row number does not consider if the row is visible or not.
I could obviously change my dataset query, but is it possible to just alter my Row Number expression to only include rows that aren't hidden?
Thanks
You can probably achieve this by combining the logic of your two expressions.
Say you have a simple DataSet and a simple Tablix based on this:
Here, RowNum is calculated as:
=RunningValue(Fields!val1.Value, CountDistinct, "Tablix1")
Next, let's hide some rows using an expression based on the other two fields:
=IIf(Fields!val2.Value + Fields!val3.Value <> 0, False, True)
This breaks RowNum, but we can amend the expression to ignore the hidden rows. We do this by NULLing them out (i.e. for SSRS set as Nothing) - CountDistinct will not consider any Nothing values:
=RunningValue(IIf(Fields!val2.Value + Fields!val3.Value <> 0, Fields!val1.Value, Nothing)
, CountDistinct
, "Tablix1")
Now RowNum is ignoring the hidden rows as required: