Crystal report percentage difference between 2 columns in crosstab - crystal-reports

Not sure if this is possible or not in crystal. I have a crosstab report that has 2 columns. I need to take the percentage between the 2 values. Here is a quick sample of what I need it to look like:
Type 1 Type 2 Type 3 Total
========== ========== ========== ==========
3/1/2011 2 1 50% 1 1 100% 0 0 0% 3 2 66%
3/2/2011 1 1 100% 3 1 33% 5 3 60% 9 5 55%
Total 3 2 66% 4 2 50% 5 3 60% 12 7 58%
To create column 1 I am just doing a distinct count. To create column 2 I created a formula to the field value to 1 if it is true and 0 otherwise. I set the 3rd column to take the percent by row. The problem is in the row total at the bottom. The counts are correct, but the percentage always shows up as 100%.
I am using crystal reports 2008.

I was able to finally figue this out.
In column 1 I added a distinct count.
Column 2 I added a count based off of a formula:
if MyType = "Whatever" then
1
else
0
The third column was an average (a built in function).
No groupings are needed.

Related

How to add an iterative id column which goes up when a value in another column resets to 1 in Postgresql

I have a SQL table which has two columns called seq and sub_seq as seen below. I would like to add a third column called id, which goes up by 1 every time the sub_seq starts again at 1 as shown in the table below.
seq
sub_seq
id
1
1
1
2
2
1
3
3
1
4
4
1
5
5
1
6
1
2
7
2
2
8
3
2
9
1
3
10
2
3
11
3
3
12
4
3
13
5
3
14
6
3
15
7
3
I could write a solution using plpgsql, however I would like to know if there is a way of doing this in standard SQL. Any help would be greatly appreciated.
If sub_seq is always a running sequence then you can use the DENSE RANK function and order over the differences of two columns, assuming it will consistently uniform.
SELECT seq, sub_Seq, DENSE_RANK() OVER (ORDER BY seq-sub_Seq) AS id
FROM tableDemo
This solution is based on the sample data you have provided, I think more sample data would be helpful to check the whole scenario.

Running total using two columns

Given a table with data like:
A
B
Qty.
Running Total
5
5
5
10
5
15
I can create the running total using the formula =SUM($A$2:A2) and then drag down to get the running total after each quantity (here Qty.)
What may I do for calculating running total using two columns which may or may not be consecutive as shown below:
A
B
C
D
Qty. 1
Other
Qty. 2
RT
2
blah
2
4
2
phew
2
8
3
xyz
2
13
Place in cell D2 the formula =SUM(A2,C2,D1). Do not pay attention to the fact that the function will refer to a non-numeric cell D1 - the SUM() function will not break, unlike ordinary addition =A2+C2+D1. Now, just stretch the formula down.

remove a lesser duplicate

In KDB, I have the following table:
q)tab:flip `items`sales`prices!(`nut`bolt`cam`cog`bolt`screw;6 8 0 3 0n 0n;10 20 15 20 0n 0n)
q)tab
items sales prices
------------------
nut 6 10
bolt 8 20
cam 0 15
cog 3 20
bolt
screw
In this table, there are 2 duplicate items (bolt). However since the first 'bolt' contains more information. I would like to remove the 'lesser' bolt.
FINAL RESULT:
items sales prices
------------------
nut 6 10
bolt 8 20
cam 0 15
cog 3 20
screw
As far as I understand, If I used the 'distinct' function its not deterministic?
One way to do it is to fill forward by item, then bolt will inherit the previous values.
q)update fills sales,fills prices by items from tab
items sales prices
------------------
nut 6 10
bolt 8 20
cam 0 15
cog 3 20
bolt 8 20
screw
This can also be done in functional form where you can pass the table and by columns:
{![x;();(!). 2#enlist(),y;{x!fills,/:x}cols[x]except y]}[tab;`items]
If "more information" means "least nulls" then you could count the number of nulls in each row and only return those rows by item that contain the fewest:
q)select from #[tab;`n;:;sum each null tab] where n=(min;n)fby items
items sales prices n
--------------------
nut 6 10 0
bolt 8 20 0
cam 0 15 0
cog 3 20 0
screw 2
Although would not recommend this approach as it requires working with rows rather than columns.
Because those two rows contain different data, they are considered distinct.
It depends on how you define "more information". You would probably need to provide more examples, but some possibilities:
Delete rows with null sales value
q)delete from tab where null sales
items sales prices
------------------
nut 6 10
bolt 8 20
cam 0 15
cog 3 20
Retrieve rows with max sales value for each item
q)select from tab where (sales*prices) = (max;sales*prices) fby items
items sales prices
------------------
nut 6 10
bolt 8 20
cam 0 15
cog 3 20

Summing variable used in calculation after report is generated

I am creating a report where a query returns a list of values which are being used in my detail band. A new value shows for each record like it should. I'm trying to sum up these values and then use them in a calculation on the same line. Heres an example:
Color Fruit # of fruit Percent of total
--------------------------------------------------------
red apple 4 57%
green apple 3 43%
Total apples 7
The column being summed is the # of fruit or 3rd column. You can see the total at the end. Then the percent of total is the # of fruit for that row divided by the total. So for the first row it's 4 / 7 which equals 57%.
The problem is that I can't get the total until all the fruit has been summed. But I need that total to be used on each row for the Percent of total column.
How do I create a variable to do this?

Any other way to count group records in crystal reports?

I want to count the total no of records found under a group and according to that i need to assign reduction percentages.
In Detail,
if a user got less than 3 products he ll get 10%
if a user got 4-10 products he ll get 15%
if he got 10-20 products he ll get 20% as deduction
For example consider the following as a crystal report:
User 1
Product Deduction
Apple 15 %
Orange 15 %
Lemon 15 %
Strawberry 15 %
Grapes 15 %
Here i made a count of records in this group using running total and worte a formula. Depends on the count its ll make the deductions
But the problem in getting is, I'm getting the following output...
User 1
Product Deduction
Apple 10 % // Since count is 1
Orange 10 % // Since count is 2
Lemon 10 % // Since count is 3
Strawberry 15 % // Since count is 4
Grapes 15 % // Since count is 5
I wanna make all products deduction as 15 % if the total no of records are 4-15. Here the total no of products is 5 its in the range of 4-15 so all the product should have the deduction as 15 %.
Please help me out to solve this?
You don't need a running-total field (RT) for a simple count; use a summary field instead. The summary field can be used in the group's header and footer (a RT can only be used in the footer).
Select a field (preferably a unique field), then select Insert | Summary Field... Choose Count or Distinct Count from the picklist.
You can use this summary in a formula as well:
//{#discount}
//Assumes there is a group on {table.fruit_name}):
SELECT Count({table.key_field}, {table.fruit_name})
CASE 1 to 3: .1
CASE 4 to 5: .15
DEFAULT: 0