average and for in Cognos Analytics - cognos-bi

Presently I'm using the following code to obtain specific average based on conditions -
round(average( [Duration] for [P_CODE], [STEP_TYPE]='Electronics Processing'),2)
Unfortunately, I'm getting an error when we run this code as a part of our environment. I have to find a way to do the same to get the duration's average based on this STEP_TYPE instead of using the "for" in the average for the specific STEP_TYPE.
This Average is getting populated within a List shown below.
Thanks

Try this:
round(
average (
CASE
WHEN [STEP_TYPE] = 'Electronics Processing' THEN [Duration]
ELSE null
END
for [P_CODE])
,2)
Nulls are excluded from aggregates such as count() and average().

Related

Summing the value of the aggregate function Last()

enter image description hereI am trying to design a report in ssrs that returns the last opening stock for each product in the dataset. To achieve this I used
=Last(Fields!CustProdAdj_new_openingstockValue.Value).
This works fine. But where I encountered a problem is in getting the sum of all the opening stock for each product. I tried using
=sum(Last(Fields!CustProdAdj_new_openingstockValue.Value))
but I got the error message
[Error on Preview]
Please is there another way to go about this
I have tried using aggregate(), runningValue(), to no avail
This is the dataset
This is the report layout
On previewing having used max()
This is probably easier to do directly in the dataset query but assuming you cannot change that, then this should work...
This assumes your data is ordered by the CustProdAdj_createdon column and that this is a date, datetime or some other ordered value, change this bit if required.
=SUM(
IIF(Fields!CustProdAdj_createdon.Value = Max(Fields!CustProdAdj_createdon.Value, "MyRowGroupNameHere"),
CustProdAdj_new_openingstockValue,
0)
)
Change the MyRowGroupNameHere to be the name of the rowgroup spelled exactly as it is in the rowgroup panel below the main design panel. Case sensitive and include quotes.
What this does is, for each row within the rowgroup "MyRowGroupNameHere", compare the CustProdAdj_createdon date to the max CustProdAdj_createdon across the rowgroup. . If it is the same then return the CustProdAdj_new_openingstockValue else return 0.
This will return the value required on only 1 record within the group.
For example, if you had 1 row per day then only on the last day of the month would a value be returned other than 0 because only the date of the last record would match the maximum date within the group.
Then it simply sums the results of this up.

Running Sum Query

I'm new to Access and I'm trying to develop my own Inventory Management Database.
I'm trying to make a query that could display a running total of the Inventory on Hand as of a specific date. This is how my table looks:
It's sorted according to ITEM_ID then TRANDATE in ascending order. I'd like to add a calculated field beside the NET field that would show a running total of the specific ITEM_ID after a specific date. Negative numbers in the NET field represent a sale while the positive ones represent a purchase. I tried using the DSUM function as it is widely recommended in creating a running sum field. My expression is this
DSum([NET],"InvtyTransT", "[ITEM_ID]=" & [ITEM_ID] And "[TRANDATE]<=#" & [TRANDATE] & "#"). But it only shows the total of the NET field (6827) in each record like this:
What I needed is like this:
(I used an IF function in excel to compute this)
Please help. I think I might have missed something in my expression. I've tried revising it several times and it would always give me the same wrong answer in every record.
Thanks in advance.
Try correlated sub-query.
SELECT t.*, (SELECT SUM(t2.NET)
from InvtyTransT as t2
WHERE (t2.TRANDATE <= t.TRANDATE AND t2.ITEM_ID = t.ITEM_ID)
) AS rSUM
FROM InvtyTransT AS t;

SQL Server Report Builder SUM IF

I have a table which display various data and what I want is it to calculate a percentage in which the total has increased by, so for example:
Total is £1000
TotalAfterMarkup is £1500
=SUM(Fields!Total.Value -
Fields!TotalAfterMarkup.Value) /
(Fields!TotalAfterMarkup.Value)
the above code calculates the Percentage to -50% which is incorrect but ill be able to work that out. What i want is to now put this statement into an SUM IF statement. currently i get the #Error if the TotalAfterMarkup < 1 so i need something like
IF TotalAfterMarkup.Value >0 THEN
=SUM(Fields!Total.Value -
Fields!TotalAfterMarkup.Value) /
(Fields!TotalAfterMarkup.Value)
ELSE 0.00%
is this possible, thanks for any help guys
Unfortunately I don't have enough rep to comment, otherwise I wouldn't post this as an answer. Really, I need a little more information. Are you doing this as an expression within the report builder or as part of your dataset?
If its part of your dataset then I suggest using a case statement.
case
when TotalAfterMarkup.Value >0
then SUM(Fields!Total.Value -
Fields!TotalAfterMarkup.Value) /
(Fields!TotalAfterMarkup.Value)
else '0.00%'
end [MyFieldName]
Using an IIF Statement: ("Dataset = Your Dataset Name")
=IIF(Fields!TotalAfterMarkup.Value, "CostTable") >0, SUM(Fields!TotalCost.Value, "CostTable") - (Fields!TotalAfterMarkup.Value, "CostTable") / (Fields!TotalAfterMarkup.Value, "CostTable"), "0.00%")
I think this should work:
=IIF(TotalAfterMarkup.Value > 0,(SUM(Fields!Total.Value -
Fields!TotalAfterMarkup.Value)/(Fields!TotalAfterMarkup.Value)),0)
The IIF function is the one you use for evaluating conditions in SSRS:
Expression Examples (Report Builder and SSRS) - See decision function section

Oracle SQL find minumum value above a certain threshold

Had a quick google to see if this can be done without much luck, but is there any way in oracle sql to return the minumum value of something but above a certain number (i.e. minimum value above negative numbers). Currently I'm using this line of code
min(ROUND(IA.ASM_START_DATE -REF.ASM_START_DATE,0)) over (partition by IA.ASM_ID) min_wk
To return the lowest difference grouped by ID - it's working to a point, but I want it to bring back the lowest difference above -10. Ideally I'm trying to achieve this in the select rather than using the where query, as I want to use it to identify issues but not exclude them from the report completely.
A simple hack is to use a case statement to set any values that are too low to null so they won't change the minimum:
min(case when ROUND(IA.ASM_START_DATE -REF.ASM_START_DATE,0)<-10 then null else ROUND(IA.ASM_START_DATE -REF.ASM_START_DATE,0) end) over (partition by IA.ASM_ID)

Ms Access : Query to work out percentage

I have a database which currently records the amount of times someone does a certain procedure and they scores they have received. The scoring is done by select a value of either N, B or C.
I currently have written a query which will count the total number of times a procedure is done and the amount of times each score is received.
Here is the result of the query (original: http://www.flickr.com/photos/mattcripps/6673555339/)
and here is the code
TRANSFORM Count(ed.[Entry ID]) AS [CountOfEntry ID]
SELECT ap.AdultProcedureName, ap.Target, Count(ed.[Entry ID]) AS [Total Of Entry ID]
FROM tblAdultProcedures AS ap LEFT JOIN tblEntryData AS ed ON ap.AdultProcedureName = ed.[Adult Procedure]
GROUP BY ap.AdultProcedureName, ap.Target
PIVOT ed.Grade;
If a score of N or B is given that is deemed below standard and C is deemed at standard. Is there a way I can add something to my query which will show me in percentage how many of the procedures we at standard and how many below?
I really cant get my head round this so any help would be great.
Thanks in advance
UPDATE TabProd
SET PrecProd = (PrecProd * 1.1)
WHERE Código IN (1,2,3,4)
I did something very similar to this on a pretty large scale.
My issue was the need to be able to run queries over specific (but user variable) timeframes and output similar percentage of total results in a report.
I won't get into the date issue but my solution was to run the "sum" function on the total line on my specific reject criteria to get totals of the rejects then use a divide expression to create a new column element (defined expression) in the same query pulling from the joined table of "Total net production" - joined by a common reference - job ID.
For your case it sounds like you want to sum the two failure types - which you would simply add defined expressions dividing your total instances into your various failure modes and formatting in your output report as percents. To finish the data portion of your report you then need a third expression defining your "non-fail percent" - which would be 1.0 - N/total - B/total - both of which you will have previously defined in the query to determine the N and B failure rates.
Then its a matter of pulling that information into your report and formatting. It definitely CAN be done.
Hope this helps.