quantity total
measure lv1 lv2 lv3 summary lv1% lv2% lv3%
xyz | 2 1 4 7
frs | 4 4 1 9 how to find the each level % ?
sdfkj| 4 1 1 6
From my package data I used case statement to display those columns and quantity is the measure of my cross tab.
My question is: how can I find the % value of each level as we know the metric is like each cell divided by total summary. but how will that reflect in my table if the cross table measure is quantity. xyz, frs, sdfkj are rows displayed for my query row and lv1,lv2,lv3 are my columns and total summary is the summary option to find the total of 3 levels.i am using cognos 10.2.2
Have you tried:
lv1%
[lvl1]/([lv1] + [lv2] + [lv3])
lv2%
[lvl2]/([lv1] + [lv2] + [lv3])
lv3%
[lvl3]/([lv1] + [lv2] + [lv3])
Related
Is there a way to use an Over and Intersect function to get the average sales for the first 3 periods (not always consecutive months, sometimes a month is skipped) for each Employee?
For example:
EmpID 1 is 71.67 ((80 + 60 + 75)/3) despite skipping "3/1/2007"
EmpID 3 is 250 ((350 + 250 + 150)/3).
I'm not sure how EmpID 2 would work because there are just two data points.
I've used a work-around by calculated column using DenseRank over Date, "asc", EmpID and then used another Boolean calculated column where DenseRank column name is <= 3, then used Over functions over the Boolean=TRUE column but I want to figure the correct way to do this.
There are Last 'n' Period functions but I haven't seen anything resembling a First 'n' Period function.
EmpID Date Sales
1 1/1/2007 80
1 2/1/2007 60
1 4/1/2007 75
1 5/1/2007 30
1 9/1/2007 100
2 2/1/2007 200
2 3/1/2007 100
3 12/1/2006 350
3 1/1/2007 250
3 3/1/2007 150
3 4/1/2007 275
3 8/1/2007 375
3 9/1/2007 475
3 10/1/2007 300
3 12/1/2007 200
I suppose the solution depends on where you want this data represented, but here is one example
If((Rank([Date],"asc",[EmpID])<=3) and (Max(Rank([Date],"asc",[EmpID])) OVER ([EmpID])>=3),Avg([Sales]) over ([EmpID]))
You can insert this as a calculated column and it will give you what you want (assuming your data is sorted by date when imported).
You may want to see the row numbering, and in that case insert this as a calculated column as well and name it RN
Rank([Date],"asc",[EmpID])
Explanation
Rank([Date],"asc",[EmpID])
This part of the function is basically applying a row number (labeled as RN in the results below) to each EmpID grouping.
Rank([Date],"asc",[EmpID])<=3
This is how we are taking the top 3 rows regardless if Months are skipped. If your data isn't sorted, we'd have to create one additional calculated column but the same logic applies.
(Max(Rank([Date],"asc",[EmpID])) OVER ([EmpID])>=3)
This is where we are basically ignoring EmpID = 2, or any EmpID who doesn't have at least 3 rows. Removing this would give you the average (dynamically) for each EmpID based on their first 1, 2, or 3 months respectively.
Avg([Sales]) over ([EmpID])
Now that our data is limited to the rows we care about, just take the average for each EmpID.
#Chris- Here is the solution I came up with
Step 1: Inserted a calculated column 'rank' with the expression below
DenseRank([Date],"asc",[EmpID])
Step 2: Created a cross table visualization from the data table and limited data with the expression below
I am trying to create a Group in the exported Excel Workbook using OpenXML.
My source data table looks like this:
Row State Product Sales
1 NY A 100
2 NY A 200
3 NY B 300
4 CA A 100
5 CA A 200
6 CA B 300
I would like to create an outline by State and then Product with a subtotal on each group
I tried
ws.Outline.SummaryVLocation = XLOutlineSummaryVLocation.Top;
ws.Rows(1, 3).Group(); // Create an outline (level 2) for rows 1-4
ws.Rows(4, 6).Group();
But it's not giving me what I want, and I don't see an option to add the subtotals.
How can I achieve this?
The code example in the documentation which you use is either outdated or just wrong.
If you want to group rows 2 to 4 you need to use the code ws.Rows(3, 4).Group(); (see picture). This is consistent with Excel itself, there you have to select only rows 3 and 4 before clicking the group button to get the same result.
When you try to group rows 1 to 3 like in your code you group them all under row 0 which leads to errors since there is no row 0.
You can control this behaviour to some extend with the XLOutlineSummaryVLocation property. If you use Bottom instead of top you use the top two rows to group rows 2 to 4: ws.Rows(2, 3).Group();
With all this said two more points:
You need to use Excel row numbers not the numbers in your column "Row".
All this grouping and collapsing is only for display purposes. To sum up the sales numbers you have to use the subtotal functions in Excel (which I find rather confusing and unhelpful in this case) or add columns and results directly in C#.
Using this code should lead to your desired result (see picture below):
ws.Outline.SummaryVLocation = XLOutlineSummaryVLocation.Top;
ws.Cell(1, 5).SetValue("Product subtotals");
ws.Cell(1, 6).SetValue("State subtotals");
ws.Rows(3, 4).Group(); // group rows 2 to 4 (state NY), outline level 1
ws.Cell(2, 6).SetFormulaA1("=SUM(D2:D4)"); // subtotal for all NY sales
ws.Row(3).Group(); // group rows 2 and 3 (product A), outline level 2
ws.Cell(2, 5).SetFormulaA1("=SUM(D2:D3)"); // subtotal for all NY, product A sales
ws.Cell(4, 5).SetFormulaA1("=SUM(D4)"); // subtotal for all NY, product B sales
ws.Rows(6, 7).Group(); // group rows 5 to 7 (state CA), outline level 1
ws.Row(6).Group(); // group rows 5 and 6 (product A), outline level 2
ws.CollapseRows(2); // collapse group level 2 (products)
Hello I have a problem with the function avg. I have a table like this and I would like to take the average of each row. I also have the zero in some cells and would like to avoid count them.
data rep val1 val2 val3
1 a 0 3 3
2 a 1 4 0
3 a 1 1 1
4 a 1 3 0
And I would like this result
data AVG
1 3
2 2.5
3 1
4 2
thank you
Assuming you have at least one non-zero value:
SELECT data, (val1+val2+val3)/((val1!=0) + (val2!=0) + (val3!=0)) avg
FROM **table_name**
I think divide by zero returns null see manual, depending on your db settings, so you could do:
SELECT data, COALESCE((val1+val2+val3)/((val1!=0) + (val2!=0) + (val3!=0)),0) avg
FROM **table_name**
Any null values in a row will cause each query to always return null and 0 for the row respectively.
I have a column in table A. the column name is Sequence number. The Structure of table A is numbers from 1,2,3,4.....3600.
Now on the basis of table A. I want the below output from the SQL select query for SQL server 2008.
seq no dynamic col
1 1
2 1
3 1
4 1
5 1
6 2
7 2
8 2
9 2
10 2
11 2
12 3
13 3
My Second column is getting generated at the run time.
And the business logic is that, if the seq number mod 6 = 0 then increment the value of dynamic column.
Thanks in advance
Try this:
select seqno, (seqno/6) +1 dynamiccol
from t
Fiddle Demo
Take this as pseudo code because I'm not familiar with SQL Server specifically, but it should give you somewhere to go.
SELECT
seq_no,
ROUNDDOWN(seq_no/6)+1 AS dynamic_col
FROM
my_table
I have the table below
relID value charge
1 2 5
1 8 2
2 1 10
2 4 6
2 9 2
For the above table i need for a given value ex 10 to find what to charge for each relID
In the above for value<10 i need to get charge=5 for relID=1 and charge=2 for relID=2
I am trying to use 1 sql command to get it and i am kind of lost
Can anyony help
Thanks
Your question isn't very clear but I think this will work for you
SELECT t.relID,
(
SELECT charge
FROM table
WHERE relID = t.relID
AND value < 10
ORDER BY value
LIMIT 1
) AS charge
FROM table AS t
Let me rephrase.
Here is the table
relID value charge
1 2 5
1 8 2
2 1 10
2 4 6
2 9 2
Explain the table :
Lets say that the value and charge are money.
If the user has value 2 then i must charge with 5 using relID 1
If the user has value 8 then i must charge with 2 using relID 1
same for the relID 2
So when a user come with a value 10 i must find what to charge.So for the given value 10 i must find in the table all records with value<10.
In the example the values for value <10 are
For relID=1 are (2,8)
For relID=2 are (1,4,9)
Now for each relID i need to get the max value.
For relID=1 max value is 8 so charge is 2
For relID=2 max value is 9 so charge is 2
I plain english there are value rate
0-2 charge 5
2-8 charge 2
and ...
i hope to be clear now
select *
from Table t
where value =
(select max(value)
from Table
where value <= 10
and relId = t.relId)