Is there a way to select a range of cells at the end of each column on a cross table to perform a calculation - sap

I need to select a range of the last 3 to 5 cells w/in each column and calculate an average to which the result needs to be output beneath the selected cells. webi results
Desired output modeled in Excel

Maybe you may try to add a rank dimension that you add in your table (then you hide) and you compute your avg(columnName where rank > value)
2 ways to do it
- the rank should be in desc order then in the avg --> rank<=5
- the rank is in esc order --> You will need to use count() to get the number of line - 5

Related

Calculating the mode/median/most frequent observation in categorical variables in SQL impala

I would like to calculate the mode/median or better, most frequent observation of a categorical variable within my query.
E.g, if the variable has the following string values:
dog, dog, dog, cat, cat and I want to get dog since its 3 vs 2.
Is there any function that does that? I tried APPX_MEDIAN() but it only returns the first 10 characters as median and I do not want that.
Also, I would like to get the most frequent observation with respect to date if there is a tie-break.
Thank you!
the most frequent observation is mode and you can calculate it like this.
Single value mode can be calculated like this on a value column. Get the count and pick up row with max count.
select count(*),value from mytable group by value order by 1 desc limit 1
now, in case you have multiple modes, you need to join back to the main table to find all matches.
select orig.value from
(select count(*) c, value v from mytable) orig
join (select count(*) cmode from mytable group by value order by 1 desc limit 1) cmode
ON orig.c= cmode.cmode
This will get all count of values and then match them based on count. Now, if one value of count matches to max count, you will get 1 row, if you have two value counts matches to max count, you will get 2 rows and so on.
Calculation of median is little tricky - and it will give you middle value. And its not most frequent one.

How to check max from range in cursor?

I have a problem with transferring an Excel formula to SQL. My excel formula is: =IF(P2<(MAX($P$2:P2));"Move";"").
The P column in excel is a sequence of numbers.
a | b
------
1
2
7
3 MOVE
4 MOVE
8
9
5 MOVE
10
You can find more example on this screenshot:
I created a cursor with a loop but I don't know how to check max from range.
For example when I iterate for fourth row, I have to check max from 1-4 row etc.
No need for a cursor and a loop. Assuming that you have a column that defines the ordering of the rows (say, id), you can use window functions:
select t.*,
case when a < max(a) over(order by id) then 'MOVE' end as b
from mytable t
One option would be using MAX() Analytic function . But in any case, you'd have an extra column such as id for ordering in order to determine the max value for the current row from the first row, since SQL statements represent unordered sets. If you have that id column with values ordered as in your sample data, then consider using
WITH t2 AS
(
SELECT MAX(a) OVER (ORDER BY id ROWS BETWEEN
UNBOUNDED PRECEDING
AND
CURRENT ROW) AS max_upto_this_row,
t.*
FROM t
)
SELECT a, CASE WHEN max_upto_this_row > a THEN 'Move' END AS b
FROM t2
ORDER BY id;
Demo

Selecting TOP ranking through SQL in Unica

I have a table with 2 columns; sub_ID and RANK.
All sub_ID are unique and there are around 100k and have all been given a RANK between 1 - 38 in the RANK column.
I am working in Unica or IBM Campaign where in a SELECT cell I need to enter raw SQL that only returns 5000 subscribers where it selects based on the RANK in preference for 38 and less.
Use a sample process box. Limiting number of cells to 1 with number of records to 5000 and sorting based on rank column

SSRS displaying columns on a matrix to the right

I have a table that looks like so:
Is there any way I can display it like this in SSRS?
1 2 3 4
1 2.091 0.918 0.9 1.718
2 0.647 0.964 0.6 2.264
3 0.804 0.789 0.6 1.9
I tried using a matrix but it stops after showing the first column and doesn't expand to show the rest.
Attempt:
Results:
Another attempt:
Results:
to get something similar to the result you're looking for you need to write a query like:
select id
,row_number() over(partition by SubscaleNumber order by SubscaleNumber ) SubscaleNumber
, item
from yourtable
then the matrix should work fine.
You need to set the Row and Column grouping to get this matrix-style output.
When you click in your table you should see the brackets for the Row and Column groups. If you don't have a column group, add one.
Go into the properties of each group and make sure they are grouped by the column you want.
Start from scratch with a table or matrix.
Add a Row group on id
Add a Column Group on Item.

Average Distinct Values in a single column in Power Pivot

I have a column in PowerPivot that basically goes:
1
1
2
3
4
3
5
4
If I =AVERAGE([Column]), it's going to average all 8 values in the sample column. I just need the average of the distinct values (i.e., in the example above I want the average of (1,2,3,4,5).
Any thoughts on how to go about doing this? I tried a combination of =(DISTINCT(AVERAGE)) but it gives a formula error.
Thanks!!
Kevin
There must be a cleaner way of doing this but here is one method which uses a measure to get the sum of the values divided by the number of times it appears (to basically give the original value) then uses an iterative function to do it for each unique value.
Apologies for the uninspired measure names:
[m1] = SUM(table1[theValue]) / COUNTROWS(Table1)
[m2] = AVERAGEX(VALUES(Tables1[theValue]), [m1])
Assuming your table is caled table1 and the column is called theValue