SSRS: How to count true rows - sql-server

I have a report with a dataset that has a column with booleans. In the table footer I want to display x / y where x is how many rows that were true and y is how many rows there was total.
Currently I have this:
=Count(Fields!Transfered.Value).ToString() + " / " + CountRows().ToString()
But the first becomes same as the last part. I then tried this:
=Sum(Fields!Transfered.Value).ToString() + " / " + CountRows().ToString()
But that generates an error, which I guess I can understand. I thought that if I converted the booleans into numbers where true is 1 and false is 0, then it could work out nicely. But how can I do that? Or is it a smarter way to do this all together?
Update: Have now also tried
=Sum(CInt(Fields!Transfered.Value)).ToString() + " / " + CountRows().ToString()
And got a negative result... O.o
Also found a way that worked, which I posted as an answer. But I won't accept it as an answer yet incase someone has a better way to do this =)

I can tell you why things went wrong...
Count(Fields!Transfered.Value) is simply the number of rows. aka CountRows()
Sum(Fields!Transfered.Value) is trying to aggregate "true" or "false" = error
Sum(CInt(Fields!Transfered.Value)) will sum -1 and 0 because VB.NET true = -1
Sum(IIF(Fields!Transfered.Value, 1, 0)) fixes the sign issue = your solution
To avoid the extra IIF, you could use negate the sum of all the -1s
= -Sum(CInt(Fields!Transfered.Value)).ToString() + " / " + CountRows().ToString()
In the end, either solution would be OK and both are equally kludgy

Figured out a way to do it, although there is probably a better more clear and logical way...
=Sum(IIF(Fields!Transfered.Value, 1, 0)).ToString() + " / " + CountRows().ToString()

I came across this today and at least verified that I wasn't the only one that got a negative sum value for bools out of SSRS. Thanks.
My solution was to use ABS for absolute value. I like that better than just negating the expression solely because it's easier to see visually where the '-' might be missed if you're not careful in reading the expression.
but it's essentially the exact same thing

Here is a way where you count on a grouped value
=CountDistinct(IIF(Fields!TrueOrFalseField.Value,
Fields!GroupedField.Value,
"BadValueToBeRemovedFromCount"))
-IIF(CountDistinct(Fields!TrueOrFalseField.Value)=1
and First(Fields!TrueOrFalseField.Value)
, 0, 1)
The second part
-IIF(CountDistinct(Fields!TrueOrFalseField.Value)=1
and First(Fields!TrueOrFalseField.Value)
, 0, 1)
remove 1 from the count if it has True and False value or if there are only one distinct false value.

Related

HNT exchange rates are broken

I would like to obtain the HNT/Helium price via the API. Getting the rates through https://api.coinbase.com/v2/exchange-rates?currency=HNT seems to work like with any other currency, but the rates are completely off.
I noticed that on the website, there actually are two tokens called HNT: Helium and Hento (with Helium definitely being the main one, I had not heard of the other one). However, the rates provided by the API don't correspond to either.
The current results:
API result
0.02013283 = $49.67
HNT - Helium (https://www.coinbase.com/price/helium)
$38.92
HNT - Hinto (https://www.coinbase.com/price/hinto)
$0.0669
That's pretty awesome.
Searching for HINTO on CB comes back with no results but you can get to it directly with the link you provided. Searching for HNT only returns Helium. Yet the API returns the un-searchable results for Hinto when using HNT...
But to answer your question, in case you haven't discovered the solution yet, you can simply change the currency to HELIUM
https://api.coinbase.com/v2/exchange-rates?currency=HELIUM
repsonse:
{"data":{"currency":"HELIUM","rates": [...] {"USD":"30.096511279999998"} [...] }}
From a cell, you could call this function:
"colon separated parameters"
=INDEX( SPLIT( INDEX ( SPLIT( IMPORTDATA("https://api.coinbase.com/v2/exchange-rates?currency=HELIUM", "|", ","), ":"), 0, 48), ","), 0, 1)
"semi colon separated parameters"
=INDEX( SPLIT( INDEX ( SPLIT( IMPORTDATA("https://api.coinbase.com/v2/exchange-rates?currency=HELIUM"; "|"; ","); ":"); 0; 48); ","); 0; 1)
This will get you quoted value: "22.87244131153848"
IMPORTDATA will get the JSon, I use the "|" separator so it comes alltogether
{"data":{"currency":"HELIUM","rates":{"AED":"92.539849932...
SPLIT will separate values on every ":"
col 1. {"data"
col 2. {"currency"
col n. ...
INDEX will get the column where the price of Helium is (the FJD is the next element)
"22.87244131153848","FJD"
SPLIT will separate again the value on the ","
col 1: "22.87244131153848"
col 2: "FJD"
INDEX will finally get the resultant value
"22.87244131153848"

Need help correcting a divide by zero error

In one of the reports we are getting a divide by zero error. What we want is if the Prior Year value is 0 then Percent Difference should be 100%. I’ve tried all kinds of ways to get this to work with no success.
Below is the script that is causing the problem. Do you know how to correct this?
=Iif(Sum(Fields!LYSUMAMOUNT.Value, "Group3") <> 0, ((Sum(Fields!SUMAMOUNT.Value, "Group3") - Sum(Fields!LYSUMAMOUNT.Value, "Group3")) / Sum(Fields!LYSUMAMOUNT.Value, "Group3")) * 100, 0)
Thank you in advance!
This is a bug in SSRS or the way Microsoft implemented IIF evaluation. It tries to evaluate both true and false expression irrespective of IIF condition. You can try as below to resolve your issue. Here I have added IIF condition to division as well and if it is zero use 1 as divisor.
=Iif(Sum(Fields!LYSUMAMOUNT.Value, "Group3") <> 0, ((Sum(Fields!SUMAMOUNT.Value, "Group3") - Sum(Fields!LYSUMAMOUNT.Value, "Group3")) / Iif(Sum(Fields!LYSUMAMOUNT.Value, "Group3") <> 0, Sum(Fields!LYSUMAMOUNT.Value, "Group3"),1)) * 100, 0)

Select then parse using substring and cast gives different result from computation

I parsed a string which I use substring to get the last 11 i think numbers of characters. I accomplished that one but when I use cast and round it gives a different result from the manual computation I did.
Here's my query
SELECT round(cast(SUBSTRING('351856040520298,241111;1R,141117003059,A,1420.4629N,12058.7028E,0.0,77,0.9,20000006;2R,141117003059,11,98.3,12.58,04.10,282098820.9', 123,11)as float)/3600, 0, 1)
This gives me a result of 583. But when I try to manually compute using the computation below
282098820.9 / 3600
The result is
78360.7835
Is there something wrong with my query?
Thanks for the help.
The problem is with your SUBSTRING. It only returns 2098820.9 instead of 282098820.9. Try using RIGHT to extract the last 11 characters.
SELECT round(cast(right('351856040520298,241111;1R,141117003059,A,1420.4629N,12058.7028E,0.0,77,0.9,20000006;2R,141117003059,11,98.3,12.58,04.10,282098820.9', 11)as float)/3600, 0, 1)

TSQL and Modulo

How can I do this in T-Sql:
SQRT(id) % 1 = 0
I can't cast the result of the Sqrt() function because this renders the logic above useless.
Any Ideas on how I can achieve this?
Thanks
You could do something like this:
floor(sqrt(id)) = sqrt(id)
To see whether the sqrt is whole, try this: SQRT(id) * SQRT(id) = id. This might run into floating point precision issues though. I think this fixes them reliably for all numbers up to a certain threshold:
CONVERT(INT, SQRT(id)) * CONVERT(INT, SQRT(id)) = id
After the threshold, the precision will not be enough. You'll see false negatives but never false positives.
You could cast it to a varchar and then take the substring after the decimal point. More info here: http://dbaspot.com/sqlserver-programming/411976-how-get-numbers-after-decimal-tsql.html

SSIS How to get part of a string by separator

I need an SSIS expression to get the left part of a string before the separator, and then put the new string in a new column. I checked in derived column, it seems no such expressions. Substring could only return string part with fixed length.
For example, with separator string - :
Art-Reading Should return Art
Art-Writing Should return Art
Science-chemistry Should return Science
P.S.
I knew this could be done in MySQL with SUBSTRING_INDEX(), but I'm looking for an equivalent in SSIS, or at least in SQL Server
Better late than never, but I wanted to do this too and found this.
TOKEN(character_expression, delimiter_string, occurrence)
TOKEN("a little white dog"," ",2)
returns little the source is below
http://technet.microsoft.com/en-us/library/hh213216.aspx
of course you can:
just configure your derived columns like this:
Here is the expression to make your life easier:
SUBSTRING(name,1,FINDSTRING(name,"-",1) - 1)
FYI, the second "1" means to get the first occurrence of the string "-"
EDIT:
expression to deal with string without "-"
FINDSTRING(name,"-",1) != 0 ? (SUBSTRING(name,1,FINDSTRING(name,"-",1) - 1)) : name
You can specify the length to copy in the SUBSTRING function and check for the location of the dash using CHARINDEX
SELECT SUBSTRING(#sString, 1, CHARINDEX('-',#sString) - 1)
For the SSIS expression it is pretty much the same code:
SUBSTRING(#[User::String], 1, FINDSTRING(#[User::String], "-", 1)-1)
if SUBSTRING length param returns -1 then it results in error,
"The length -1 is not valid for function "SUBSTRING". The length parameter cannot be negative. Change the length parameter to zero or a positive value."

Resources