Summing up columns in SSRS with expressions - expression

I am looking for some help totaling up a column. This column is using expressions from other columns to get its total.
This is the expression I am using:
=IIF(avg(Fields!UOS_2017.Value) <= 0, sum(Fields!PROD_HRS.Value) * - 1,
((avg(Fields!BUD_PROD_HRS___UOS.Value) * avg(Fields!UOS_2017.Value)) -
sum(Fields!PROD_HRS.Value)))
I want to sum this column.
I do want to note that this expression does work but only if the UOS_2017 column has a zero value. I do not know what to do.
Here is what my design view looks like.
I am also going to attached the results.
These are the results
Thank you in advance for your help.

I hate to add this as an answer, but could you try one of these and see what happens? I am not familiar with avg function, but if you're pivoting data you may have to add sum/avg outside of if statement as well:
=SUM(IIF(avg(Fields!UOS_2017.Value) <= 0, sum(Fields!PROD_HRS.Value) * - 1, ((avg(Fields!BUD_PROD_HRS___UOS.Value) * avg(Fields!UOS_2017.Value)) - sum(Fields!PROD_HRS.Value))))
=SUM(IIF(avg(Fields!UOS_2017.Value) <= 0, (Fields!PROD_HRS.Value) * - 1, ((avg(Fields!BUD_PROD_HRS___UOS.Value) * avg(Fields!UOS_2017.Value)) - (Fields!PROD_HRS.Value))))

Related

Max Val function not working as I expected it to work

I write this query I dont know why its not working for me can anyone suggest for me which is right
SELECT MAX(NVL(CPV.SR_NO,0)+1) INTO :CPV.SR_NO FROM CPV
WHERE VOUCHER_ID=4;
I have to bring MAX value I put 0 but it never bring 1 for first record after one record it worked properly mean if table is empty then first entry not give 1 after one record saved than its showed 1 even nvl is shown to null to 0 then + 1 please correct me
thanks
If there are no rows with VOUCHER_ID = 4, then you are taking MAX over zero rows, and that is always NULL - it doesn't matter what expression you are taking the MAX over. The NVL you used will only have effect if there are rows with VOUCHER_ID = 4, but in those rows you may have NULL in the SR_NO column. It won't help if there are no rows to begin with.
You could write the code like this **:
SELECT MAX(SR_NO) INTO :CPV.SR_NO FROM CPV WHERE VOUCHER_ID=4;
:CPV.SR_NO := NVL(:CPV.SR_NO, 0) + 1;
That is - apply the NVL (and also add 1) outside the SELECT statement.
With that said - what is the business problem you are trying to solve in this manner? It looks very much like an extremely bad approach, no matter what the problem you are trying to solve is.
** Note - I haven't seen qualified bind variable names before, like :CPV.SR_NO, but since the query works for you, I assume it's OK. EDIT - I just tried, and at least in Oracle 12.2 such names are invalid for bind variables; so I'm not sure how it was possible for your code to work as posted.
ANOTHER EDIT
The whole thing can be simplified further. We just need to pull the NVL out of MAX (and also the adding of 1):
SELECT NVL( MAX(SR_NO), 0) + 1 INTO :CPV.SR_NO FROM CPV WHERE VOUCHER_ID=4;

Handling zero as amount format in Oracle

I have an amount column which needs to be in 225,000.00 format, for which below is the query that i have written
Select TRIM(to_char(pen_amt,'999,999,999,999,999.99')) as PenAmount from transact;
Above query is giving correct result for all values except 0, for 0, its coming as .00, instead of it, it should come as 00.00. How to do it?
Add a 0 where you want the precision to start.
Select TRIM(to_char(pen_amt,'999,999,999,999,909.99')) as PenAmount from transact;

How to filter clickhouse table by array column contents?

I have a clickhouse table that has one Array(UInt16) column. I want to be able to filter results from this table to only get rows where the values in the array column are above a threshold value. I've been trying to achieve this using some of the array functions (arrayFilter and arrayExists) but I'm not familiar enough with the SQL/Clickhouse query syntax to get this working.
I've created the table using:
CREATE TABLE IF NOT EXISTS ArrayTest (
date Date,
sessionSecond UInt16,
distance Array(UInt16)
) Engine = MergeTree(date, (date, sessionSecond), 8192);
Where the distance values will be distances from a certain point at a certain amount of seconds (sessionSecond) after the date. I've added some sample values so the table looks like the following:
Now I want to get all rows which contain distances greater than 7. I found the array operators documentation here and tried the arrayExists function but it's not working how I'd expect. From the documentation, it says that this function "Returns 1 if there is at least one element in 'arr' for which 'func' returns something other than 0. Otherwise, it returns 0". But when I run the query below I get three zeros returned where I should get a 0 and two ones:
SELECT arrayExists(
val -> val > 7,
arrayEnumerate(distance))
FROM ArrayTest;
Eventually I want to perform this select and then join it with the table contents to only return rows that have an exists = 1 but I need this first step to work before that. Am I using the arrayExists wrong? What I found more confusing is that when I change the comparison value to 2 I get all 1s back. Can this kind of filtering be achieved using the array functions?
Thanks
You can use arrayExists in the WHERE clause.
SELECT *
FROM ArrayTest
WHERE arrayExists(x -> x > 7, distance) = 1;
Another way is to use ARRAY JOIN, if you need to know which values is greater than 7:
SELECT d, distance, sessionSecond
FROM ArrayTest
ARRAY JOIN distance as d
WHERE d > 7
I think the reason why you get 3 zeros is that arrayEnumerate enumerates over the array indexes not array values, and since none of your rows have more than 7 elements arrayEnumerates results in 0 for all the rows.
To make this work,
SELECT arrayExists(
val -> distance[val] > 7,
arrayEnumerate(distance))
FROM ArrayTest;

custom expression to subtract from and then multiply the sum of a column in spotfire

What would the custom expression for: 2*(Max[X] - 0.5) for every group be, in Spotfire?
Do I need to split this into multiple steps or can create a custom expression that would do it all at once for every group of data I have.
I figured it out. I did it in two steps:
1. Created a calculated column Max_X = Max([X]) OVER [GROUP]
2. Created a calculated column D = 2*([Max_X] -0.5)

Cognos 10 Filtering a calculated measure

Using a Crosstab dimensional report I having issues filtering out a specific value that I am getting from my calculated measure:
I am using the following calculated measure in Reporting Studio to get the required output..
ROUND(([A1_SCHEDQTY]+[B1_SCHEDQTY]) / ([A1_QTY]+[B1_QTY]) * 100, 1)
However, in a few cases I am getting an output of '/0' I am unable to filter this out. I have tried using an IF statement, but I believe my syntax is incorrect.
IF (ROUND(([A1_SCHEDQTY]+[B1_SCHEDQTY]) / ([A1_QTY]+[B1_QTY]) * 100, 1) = '/0') THEN NULL ELSE ROUND(([A1_SCHEDQTY]+[B1_SCHEDQTY]) / ([A1_QTY]+[B1_QTY]) * 100, 1)
Any advice?
You need to check the divisor of the equation for 0 instead of checking the entire equation for '/0'. If you check the entire equation, the divide by 0 still takes place.
Try this:
IF ([A1_QTY]+[B1_QTY] = 0) THEN (NULL) ELSE (ROUND(([A1_SCHEDQTY]+[B1_SCHEDQTY]) / ([A1_QTY]+[B1_QTY]) * 100, 1))

Resources