Summary Percentage Yes vs No - crystal-reports

Basically I'm looking for a formula to see how many times Yes was used vs. No.
I have something like this:
(({Command.result} ="Yes") / {Command.result})*100
Which makes sense in my head, but I keep getting:
A number, or currency amount is required.

Your current formula attempts to divide a boolean type through by a string. You can only perform division with numbers.
Instead, create two formulas as individual counts of Yes or No:
#YesCount:
If ({Command.result} = "Yes") Then 1 Else 0
#NoCount:
If ({Command.result} = "No") Then 1 Else 0
For the percentage, create two more formulas:
#YesPercent:
100 / Count ({Command.result}) * Sum ({#YesCount})
#NoPercent:
100 / Count ({Command.result}) * Sum ({#NoCount})

Related

TSQL divide one count by another to give a proportion

I would like to calculate the proportion of animals in column BreedTypeID with a value of 1. I think the easiest way is to count the n BreedTypeID = 1 / total BreedTypeID. (I also wnat them to have the same YearDOB and substring in their ID as shown) I tried the following:
(COUNT([dbo].[tblBreed].[BreedTypeID])=1 OVER (PARTITION BY Substring([AnimalNo],6,6), YEAR([DOB]))/ COUNT([dbo].[tblBreed].[BreedTypeID]) OVER (PARTITION BY Substring([AnimalNo],6,6), YEAR([DOB]))) As Proportion
But it bugged with the COUNT([dbo].[tblBreed].[BreedTypeID])=1
How can I specify to only count [BreedTypeID] when =1?
Many thanks
This will fix your problem, although I would suggest you use table aliases instead of schema.table.column. Much easier to read:
Just replace:
COUNT([dbo].[tblBreed].[BreedTypeID])=1
WITH
SUM( CASE WHEN [dbo].[tblBreed].[BreedTypeID] = 1 THEN 1 ELSE 0 END)

How to display quotient as a percentage

I am using Postgresql to generate a simple quotient of two fields " a / b " = -3 / 300 = -.01 or -1.00%. Instead it displays as zero. I've tried many variations of to_char with no success. How could I get "select -3/300 as quotient" to display the quotient in xxx.xxx% format? Thanks
To display with the percentage sign, you can use the following format. Don't forget to first multiply by 100.0 so 1) is it indeed a percentage and 2) as noted by #dhke the computation is done with floats, not integers.
select to_char(100.0*-3/300,'999D99%') a;
a
----------
-1.00%
(1 row)

Crystal Reports - Disable default rounding/Show all decimals

If I create a Formula Field A containing only 1/1000 and then drag it out into the report it will show "0,00" due to default number of decimals is set to 1.00 and default rounding is set to 0.01 for a field of with the type Number.
No problems.
When decimals and rounding are adjusted it will show 0,001.
When I create Formula Field B that only contains {#A} and increase decimals and rounding it will show as "0,001000000000".
But if I use A to create a text, totext({#A}) + ' test', the result will be "0,00 test" and I can not figure out a way to show the correct "0,001 test" without adding totext({#A},3). I don't want to use rounding because the number of decimals in A varies and I don't want to show any zeroes at the end.
I did modify this code (could be used in conditional formatting of decimals for a number field):
If CurrentFieldValue = Int(CurrentFieldValue) Then 0
Else If CurrentFieldValue * 10 = Int(CurrentFieldValue * 10) Then 1
Else If CurrentFieldValue * 100 = Int(CurrentFieldValue * 100) Then 2
Else If CurrentFieldValue * 1000 = Int(CurrentFieldValue * 1000) Then 3
Else If CurrentFieldValue * 10000 = Int(CurrentFieldValue * 10000) Then 4
Else DefaultAttribute
to
If {#A}= Int{#A} Then totext({#A},0)
Else If {#A}* 10 = Int{#A}* 10) Then totext({#A},1)
...
It works but I was hoping I wouldn't need all that extra code, I already have performance issues in my Crystal Reports and I don't want the risk of adding to that.
I just want to use the actual value in {#A} no matter the number of decimals.
I guess this could be caused by both default rounding settings as default number of decimals
And now I realise this might be caused by default number of decimals, not rounding.
If I change A to (1/1000)*6 it will show up as 0,01 unformatted, B will show "0,00600000 test". So it is a rounding problem.
Leave the output of your desired formula as a number.
Right click on the formula and select format field.
In the number tab select Custom Style and click customize.
For rounding option select 0.0000000001
Click the X-2 next to the Decimals option and enter this formula
local numbervar a;
local numbervar b;
for a := 1 to 10 do
(
if mid(strreverse(totext(CurrentFieldValue,10)),a,1) <> "0" then
(b := a;
a := 10);
);
11- b
I have found that the default rounding is defined by your system regional settings. If you increase the number of decimal places from 2 (usual) to say 8, all your problems will be resolved.

Rounding Off Decimal Value to Pevious and Next Hundreds

I am working in C#. I have decimal variable startFilter that contains value say 66.76. Now I want this decimal value to appear in the seach filter $0 to $100. But what I also want is, that the search filter starts from the first decimal value that comes in startFilter variable. So for instance in this case the search filter will start from $0 to $100 because the value in startFilter variable is 66.76, but in another case it can be $100 to $200 if the first value that comes in searchFilter is say $105.
Having said that, how should I round off the value in seachFilter to previous hundreds and the next hundreds. Like if the value is 66.76 it rounds off to 0 as floor and 100 as ceiling, so on and so forth.
Any idea how to do that in C#?
double value = ...
int rounded = ((int) Math.Round(value / 100.0)) * 100;
divide your original number by 100. get floor and celing values. Multiply each of them by 100.

convert function from Access SQL to T-SQL 2005

Can someone please convert this access sql function for me to work in t-sql 2005.
I am tring to take the selling price minus the cost as one number. And divide that by the original selling price to produce a second number
Thanks :)
=IIf([Selling Price]=0,0,([Selling Price]-Nz([Cost]))/[Selling Price])
IIRC it should be something along the lines of;
ISNULL((ISNULL([Selling Price],0) - ISNULL(Cost,0)),0) / ISNULL([Selling Price],0) AS Margin
But here I am getting a divide by Zero error.
any suggestions?
SELECT
CASE
WHEN ISNULL([Selling Price],0) = 0 THEN 0
ELSE ([Selling Price] - ISNULL([Cost],0))/[Selling Price]
END AS fieldName
FROM TableName
CASE
WHEN ISNULL([Selling Price], 0) = 0 THEN 0
ELSE ([Selling Price] - ISNULL([Cost], 0)) / [Selling Price]
END