Stop Crosstab summary display string count from Resetting on each page - crystal-reports

I have a crosstab in Crystal Reports XI with a display string that displays a count for each time the condition is met:
Basically I have a summary column amountspent that I need to compare to avblcredit, and count every order where amountspent (the summary) exceeds avblcredit for each group, customer. I then have to display the total amount of orders for that customer where available credit is exceeded.
After much struggle due to the fact that I cannot use calculated members in Crystal Xi. I
created a second duplicate summary for item exditures and edited the display string of the second summary to compare itself to avbl credit then count:
global numbervar count;
if currentvalue > avblcredit
then count := count + 1;
count;
The count then increments everywhere it finds the current value (sum of items) > available credit.
This works correctly if the crosstab prints fully on the page, however if the crosstab extends to the next page the count resets back to 0.
So basically as an example page 1 looks as follows:
customer 1
orders avblcredit amountspent count itema itemb itemc
ord1 4000 6000 1 2000 3000 1000
ord2 3734 5001 2 1000 2000 2001
ord3 4123 5000 3 4000 1000 0
ord4 2321 5000 4 5000 0 0
ord5 4000 5003 5 1200 3800 3
ord6 4000 6000 6 1000 2000 3000
page 2 with customer 1 group continued:
orders avblcredit amountspent count itema itemb itemc
ord7 4000 6000 1 2000 3000 1000
ord8 3734 5001 2 1000 2000 2001
ord9 4123 5000 3 4000 1000 0
ord10 2321 5000 4 5000 0 0
My question is how can I get my count from resetting on each new page?
Thanks

If you can modify your datasource, you can do the calculation upstream. Just extend your existing dataset with a new field named something like spentExceedsCredit with a value of 0 or 1. Then you can sum them per customer, or use custom formatting in the report, or have the list of customer expenditures exceed a page, or whatever, and you're not dependent on Crystal's global variables.

Related

Crystal Reports - Where clause in formula to calculate sum of a field from second table

I am trying to get a result in my report, which I beleive, requires a where clause and did not work for me with the select expert section.
I have 2 tables. Lets call them table 1 and table 2.
Table 1 contains unique records.
Table 2 contains multiple records for the same uniqueKey as table 1.
there are 3 fields in table 2 that play a roll for each uniqueKey from table 1.
QTY_ORD
QTY_SHIPPED
ITEM_CANCEL
Lets assume for item # 1 from table 1, there are 5 records in table 2. Each record has a values for the 3 above mentioned fields. I need to display the SUM of all the records where ITEM_CANCEL = 0 of QTY_SHIPPED - QTY_ORD.
It could be that 3 of the records have ITEM_CANCEL = 1 (We can ignore these records), but for the other 2 reocrds where ITEM_CANCEL = 0, I need the SUM of QTY_SHIPPED - SUM of QTY_ORD.
the current code I have is as follows"
if {current_order1.ITEM_CANCEL} = 0 then
sum({current_order1.QTY_ORD})-sum({current_order1.QTY_SHIPPED}) else
0
but this result gives me the sum of ALL the records, including the ones where ITEM_CANCEL = 1.
If I use ITEM_CANCEL = 0 in the select expert, then it removes ALL the results that have no value in table 2. I even tried the code without using the SUM function, but this provided the result of only 1 of the records in table 2 where ITEM_CANCEL = 0, and not the total difference of the 2 records in table 2 that I require.
Any suggestions on this?
Start with a detail-level formuls (no SUM):
if {current_order1.ITEM_CANCEL} = 0 then {current_order1.QTY_ORD} - {current_order1.QTY_SHIPPED} ELSE 0
Then, SUM that formula at whatever Group or Report levels you require.

Adding a column based on other columns KDB+

I have a table ("ibmqt") with a number of columns, and I would like to add a new column, containing boolean values indicating for each row whether one column ("bid") is greater than or equal to another column ("ask").
My most successful attempt so far is this:
ibmqt: update (pricecross:select bid>=ask from ibmqt) from ibmqt
However, this results in the following:
time sym bid ask bsize asize pricecross
----------------------------------------------------
00:00:59.063 IBM 43.53 43.57 10000 9000 (,`ask)!,0b
00:01:03.070 IBM 43.54 43.59 6500 3000 (,`ask)!,0b
00:02:31.911 IBM 43.56 43.6 500 4500 (,`ask)!,0b
00:03:43.070 IBM 43.56 43.56 10000 2500 (,`ask)!,1b
00:06:01.170 IBM 43.54 43.56 8500 4500 (,`ask)!,0b
00:06:11.081 IBM 43.56 43.58 500 1500 (,`ask)!,0b
00:08:15.126 IBM 43.55 43.57 1500 9000 (,`ask)!,0b
Obviously in the "pricecross" column I just want 0, 0, 0, 1, 0 etc.
Any suggestions?
There is no need for a nested select. This will do what you need:
ibmqt:update pricecross:bid>=ask from ibmqt
Or you can update ibmqt in place:
update pricecross:bid>=ask from `ibmqt
q is an array language, therefore bid>=ask compares two columns pairwise and returns a list of booleans. This will illustrate the idea:
1 2 3 >= 0 2 4 / 110b
The list of booleans is then assigned to a new column pricecross.
since >= is overloaded to work with both atom and list, using pricecross:bid>=ask is the best solution here :
q)update pricecross:bid>=ask from ibmqt
But there is a slightly different way of getting the same results :
q)update pricecross:bid>='ask from ibmqt
q)update pricecross:>='[bid;ask] from ibmqt
This is particularly useful when the dyadic function works only with atoms :
q)update pricetolerance:tolerance'[bid;ask] from ibmqt

Remove a field from details in Crystal Reports fo SUM

Here is my crystal report PAY_DETAILS data field.Here I need to remove/suppress the CBS field and its value when I am getting the total.
Amount Earn_Type
-----------------------
100 ER006
200 ER007
300 ER009
400 ER003
500 ER004
1000 CBS
600 ARS
-----------------------
2100 Total
So when the view should be as follows.I tried it with suppressing the field with the suppressing condition
if({PAY_DETAILS.EARN_TYPE}="CBS")
But even it hides it comes to the Total
Amount Earn_Type
-----------------------
100 ER006
200 ER007
300 ER009
400 ER003
500 ER004
600 ARS
-----------------------
3100 Total
create a formula maybe call it Amount
if {PAY_DETAILS.EARN_TYPE}="CBS" then {valuefield} else 0
place that in the details and creat a total from that instead of the database field.
In your suppression area use
{#Amount} = 0

Iterate over current row values in kdb query

Consider the table:
q)trade
stock price amt time
-----------------------------
ibm 121.3 1000 09:03:06.000
bac 5.76 500 09:03:23.000
usb 8.19 800 09:04:01.000
and the list:
q)x: 10000 20000
The following query:
q)select from trade where price < x[first where (x - price) > 100f]
'length
fails as above. How can I pass the current row value of price in each iteration of the search query?
While price[0] in the square brackets above works, that's obviously not what I want. I even tried price[i] but that gives the same error.

Divide records into groups - quick solution

I need to divide with UPDATE command rows (selected from subselect) in PostgreSQL table into groups, these groups will be identified with integer value in one of its columns. These groups should be with the same size. Source table contains billions of records.
For example I need to divide 213 selected rows into groups, every group should contains 50 records. The result will be:
1 - 50. row => 1
51 - 100. row => 2
101 - 150. row => 3
151 - 200. row => 4
200 - 213. row => 5
There is no problem to do it with some loop (or use PostgreSQL window functions), but I need to do it very efficiently and quickly. I can't use sequence in id because there should be gaps in these ids.
I have an idea to use random integer number generator and set it as default value for a row. But this is not useable when I need to adjust group size.
The query below should display 213 rows with a group-number from 0-4. Just add 1 if you want 1-5
SELECT i, (row_number() OVER () - 1) / 50 AS grp
FROM generate_series(1001,1213) i
ORDER BY i;
create temporary sequence s minvalue 0 start with 0;
select *, nextval('s') / 50 grp
from t;
drop sequence s;
I think it has the potential to be faster than the row_number version #Richard. But the difference could be not relevant depending on the specifics.