I have written a query for calculating total sum in one field but i could get total records. Let me share what i written.
DEFINE VARIABLE I AS INTEGER NO-UNDO.
FIND FIRST shth_pus_head WHERE shth_pus_head.push_id = "P0000078" NO-LOCK
NO-ERROR.
FOR EACH shtd_pus_det OF shth_pus_head NO-LOCK:
i = i + 1.
END.
DISPLAY i.
What i need is when i calculate total sum in qty column i want sum = 1560.
(Note- qty column table field is shtd_pus_det.qty)
Check the attached image
Your code does not calculate a total sum. It counts the number of records. To sum up the shtd_pus_det.qty fields in those records you could code something like:
define variable tot_qty as integer no-undo.
for each shtd_pus_det no-lock where shtd_pus_det.push_id = "P0000078":
tot_qty = tot_qty + shtd_pus_det.qty.
end.
display tot_qty.
Related
I am reading through a file of financial data with beneficiaries. I need to count the number of beneficiaries and then calculate their allocated percentage. If there is 1 beneficiary, the allocation is 100%, if there are 2, if there are 3, 33.33%, etc. The file is sorted by investment then beneficiary, so if there is more than one beneficiary per investment they will be in order in the file. Here's an example:
input file data
the output that I want
Here is my code, but it's wrong because this way I am assigning 100% to the first beneficiary, 50% to the second beneficiary, 33.333% to the third, etc. How can I change it to do the count, then create the beneficiaries with the right count? (There is an outer loop which is a table of investments.)
iBeneficiaryCount = 0.
dTempPercentage = 100.
FOR EACH ttJointData WHERE ttJointData.inv-num EQ ttInvestment.inv-num:
IF ttJointData.Joint_Type EQ "Joint" THEN DO:
cTemp = "JT".
RUN CreateOwner (....).
END.
ELSE IF ttJointData.Joint_Type EQ "Beneficiary" THEN DO:
iBeneficiaryCount = iBeneficiaryCount + 1.
dTempPercentage = 100 / iBeneficiaryCount.
RUN AddBeneficiary(ttJointData.investment-num,ttInvestment.benficiary-id,dTempPercentage).
END.
END.
What are the best ways to capture that beneficiary percentage? I am thinking that I need to read through the data and put that value into the ttJointData table. Or is there a way to do it on the loop? Regardless, I need a neat algorithm to count up the instances from an input file and create and assign a percentage value.
You can use a query to calculate the number of beneficiaries before you loop through them.
Something like
DEFINE VARIABLE dTempPercentage AS DECIMAL NO-UNDO.
DEFINE VARIABLE iBeneficiaryCount AS INTEGER NO-UNDO.
DEFINE QUERY qryJD FOR ttJointData.
dTempPercentage = 100.
FOR EACH ttInvestment:
// calculate how many beneficiaries; must use PRESELECT here
OPEN QUERY qryJD PRESELECT EACH ttJointData WHERE ttJointData.inv-num EQ ttInvestment.inv-num.
iBeneficiaryCount = QUERY qryJD:NUM-RESULTS.
dTempPercentage = 100 / iBeneficiaryCount.
GET FIRST qryJD .
DO WHILE AVAILABLE ttJointData :
IF ttJointData.Joint_Type EQ "Joint" THEN DO:
cTemp = "JT".
RUN CreateOwner (....).
END.
ELSE IF ttJointData.Joint_Type EQ "Beneficiary" THEN DO:
RUN AddBeneficiary(ttJointData.investment-num,ttInvestment.benficiary-id,dTempPercentage).
END.
GET NEXT qryJD .
END.
CLOSE QUERY qryJD.
END.
I have written a program using progress 4GL. The syntax is correct but don't know how to reduce the codes..We are the company working for seats and using progress 4gl..we have one table called shift maintenance which has shift time for every hours.Let explain with an example.
DEFINE VARIABLE FistshiftStartHour AS INTEGER NO-UNDO.
DEFINE VARIABLE FistShiftEnddHour AS INTEGER NO-UNDO.
DEFINE VARIABLE SecshiftStartHour AS INTEGER NO-UNDO.
DEFINE VARIABLE SecShiftEnddHour AS INTEGER NO-UNDO.
FIND FIRST shift WHERE shift.shiftsequence = 1 NO-LOCK NO-ERROR.
ASSIGN
FistshiftStartHour = shift.starthour
FistShiftEnddHour = shift.endhour.
FIND FIRST shift WHERE shift.shiftsequence = 2 NO-LOCK NO-ERROR.
ASSIGN
SecshiftStartHour = shift.starthour
SecShiftEnddHour = shift.endhour.
Like this i need to write a query for every shift hours and assign to two variables for 21 shift sequence.Is there any chance for reducing so much queries?(Note-I must to assign start and end time to individual variables).
You can handle the shift time with array variable and assign it when doing loop in your table / temp-table:
DEFINE VARIABLE sSeq AS INTEGER EXTENT 21 NO-UNDO. /* start hour */
DEFINE VARIABLE eSeq AS INTEGER EXTENT 21 NO-UNDO. /* end hour */
FOR EACH shift WHERE shift.shiftsequence LE 21 NO-LOCK BY shift.shiftsequence:
sSeq[shift.shiftsequence] = shift.starthour.
eSeq[shift.shiftsequence] = shift.endhour.
DISP sSeq[shift.shiftsequence] eSeq[shift.shiftsequence].
END.
I have a SQLITE3 DB with following 3 column layout
typ (1=gas or 0=electrical power) | time (seconds since epoch) | value (float)
In there, I document events from a gas meter which fires every 10 liter of consumed gas. This is (when the gas heating is active) once every ~20 seconds. The value written together with the timestamp is 0 (zero).
I want to automatically fill an aggregaton table with the count of all records within an interval of 10 minutes.
I had success with this query to get the counts within the intervals:
select time/600*600+600 _time, count(*) _count
from data
where typ = 1 and value = 0
group by _time
order by _time
But how would I achive the following:
run this query regularely every 10 minutes (or at every INSERT with a TRIGGER?) at xx:10 / xx:20 / xx:20 / ...
write the resulting count of only the last 10 minutes to an aggregation table together with the interval end time.
I of course could do this with a program (e.g. PHP) but I'd prefer a DB-only solution if possible.
Thanks for any help.
This trigger will run for every inserted row, and tries to insert a corresponding row in an aggregate table if one does not already exist. Then it increments the counter value in the aggregate table for the timespan of the newly inserted row.
create trigger after insert on data
begin
insert or ignore into aggregateData(startTime, counter) values ((new.time / 600) * 600, 0);
update aggregateData set counter = counter + 1 where startTime = (new.time / 600) * 600;
end;
I think that I found an easier solution which in the end creates the same result:
Just turn my aggregate query into a view:
CREATE VIEW _aggregate as
select time/600*600+600 _time, count(*) _count
from data
where typ = 1 and value = 0
group by _time
order by _time
This gives me exactly my desired result if I do a:
select * from _aggregate
It's good enough to have the aggregated values at runtime and not to store them. Or do you see a substantial difference to your solution?
I want total count of repeated values in a datable.
as shown in image total count should be : 2 because repeated values are only 1 and 2.
I tried as given below:
DataView dv = new DataView(dtTemp);
int iRowCount = dv.ToTable(true, "Column1").Rows.Count;
but it returns 3 which is incorrect.
Does anyone knows how to do it.
I don't want to use loop becoz sometimes data table contains 4000-5000 rows so if we use loop it will take much more time to get the total count.
Thanks.
I was wondering if anyone can point me in the right direction please? Say I am pulling the following table, I would then like to select an ID randomly. I understand how to select a random number using a Randomize() call followed by the relevant syntax but I want to pre-define the range.
i.e. Table Data
ID | Name
4345 Mike
3456 Lee
4567 John
There will be many more names but for this example you could use 3 or 4 etc..
Please help I'm starting to itch :o|
Just to make sure I understand what you want:
Given a table, you want to randomly select one of the ID values from that table.
If so, this should do it:
Dim rand As New Random()
Dim record As Integer = rand.[Next](0, myDataTable.Rows.Count)
Dim randomID As Integer = CInt(myDataTable.Rows(record)("ID"))
We have all the information we need to randomly select a row, and by extension randomly select one of the ID values in the table.
In old Vb you would do
Dim i as integer
i = (Rnd * (maxval-minval)) + minval
Since rnd returns a random number between 0 and 1 you would scale the number to the right range.