Recordset Subtotals in Subform - ms-access

I have to update a recordset that holds the following type of data:
Order# - OrderQty - TotalQty
1000 - 10 - <blank>
2000 - 20 - <blank>
1000 - 15 - <blank>
3000 - 40 - <blank>
After code runs the recordset should look like this:
Order# - OrderQty - TotalQty
1000 - 10 - 25
2000 - 20 - 10
1000 - 15 - 25
3000 - 40 - 40
I am not sure how to approach this, since insertions and deletions and "Order#" changes have to be taken into account as well. I think the code should be in the "After Update" and "After Delete Confirm" events. Besides that I am not sure how to proceed with the code. Any help would be greatly appreciated.

Try below query.
UPDATE tblOrders SET TotalQty=DSum("OrderQty","tblOrders","Order=" & tblOrders.Order);

Related

How to add a loop in SSRS

I am using SSRS 2012 R2.
I need to create a loop in ssrs to calculate the compliance of ONB to LB
but with a condition, for the total bags count <= 100 it will be 27 mins. but for each additional 50 bags add 5 mins.
I tried the code below but it takes only 27 and 34 min. I want to replace 34 with a loop.
count(
iif(Fields!Duration_ONB_LB_.Value <
iif(Fields!Total_Bag_Count.Value <= "100", "27","34") ,1, Nothing))
/ count(Fields!TotalRows.Value) * 100 )
Can you please help me out in this?
I'm not sure if 'loop' is the correct term, I assume you want to just create an expression that adds 5 mins for every 50 bags (or part of).
I created a sample dataset so you can see the results, it assumes that up to 100 bags is 27 mins, 101-150 bags would be 32 mins, 151-200 bags would be 37 mins and so on...
here is the expression I used, you will need to replace the iif(Fields!Total_Bag_Count.Value <= "100", "27","34") part of your expression with this.
IIF
(
Fields!Total_Bag_Count.Value <= 100,
27,
(CEILING((Fields!Total_Bag_Count.Value -100) / 50) * 5) + 27
)
Here are the results

Counting rows from Where Clause and display every count in a different column

Hi I have a MySql Query as follows:
Select Count(*),status,time from table where Status in (2,3,8) group by status,time
The Output is ok i get something like
Count Status time
10 - 1 - 2014
10 - 2 - 2014
19 - 1 - 2015
11 - 2 - 2015
But what i would like to have is something like
Count of Status1 - Count of Status2 - Time
10 - 10 - 2014
19 - 11 - 2015
Is there a nice and efficient way to do since since my database has about 1 mio records. I want to use this data to show it in a table without further manipulation.
Thanks in advance
The following should work:
SELECT SUM(IF(`Status` = 1,1,0) ) CountOfStatus1,SUM(IF(`Status` = 2,1,0) ) CountOfStatus2,TIME FROM TABLE WHERE STATUS IN (1,2) GROUP BY TIME
You will need to adjust it for whatever statuses you want to see.

calculate two columns in MySQL

I have a MySQL database, where I have 3 columns:
Day_hours
Day_minutes
All_day_hours
I am gonna have some different form fields in a JSP page, where I can put in how many hours I work. On a day I work in different places, that means that I need fx to put in 5 * Day_hours, Day_minutes and All_day_hours. So the problem is that I want to calculate all the hours and minutes during a day. So if I fx worked:
1 job: 2 hours 15 minutes
2 job: 3 hours 45 minutes
3 job: 1 hours 10 minutes
4 job: 4 hours 40 minutes
5 job: 3 hours 15 minutes
So that means if I calculate the column "Day_minutes" it would give me the result 125. I would like that the 125 minutes is converted to hours, so the result would be 2 hours and 5 minutes. Afterwords the Day_hours and Day_minutes have to be addéd to the column Allday_hours. So Allday_hours is the sum of Day_hours + Day_minutes
so Fx in MySQL database there is the following information for an example day:
Day_hours Day_minutes Allday_hours
1 job 2 15 2.15
2 job: 3 45 3.45
3 job: 1 10 1.10
4 job: 4 40 4.40
5 job: 3 15 3.15
So my question is, how do I calculate the Day_hours and Day_minutes to the Allday_hours, so the result in job 1 would be 2.15?
Have a good weekend.
Best Regards
Mads
You should not save something in Allday_hours as this is redundant information.
You can retrieve the data you want always (without problems) from the data you have. For example with
SELECT *, ((Day_hours*60 + Day_minutes)/60) AS Allday_hours FROM timedata...
No need to actually save them.
By the way I think it is rather odd that 2 hours plus 15 minutes add up to something like 2.15. So my query above computes something relative... if you really want to compute your value, you might use
SELECT *, (Day_hours + (Day_minutes / 100)) as Allday_hours FROM timedata
And if you really want to save this, you can use the calculations in an update statement like
UPDATE timedata SET Allday_hours = (Day_hours + (Day_minutes / 100))

Dynamically change database due to WHERE MySQL Crystal Reports

I'm programming with .NET 4.5, VS 2012 with CrystalReports in and MySQL (the last ^^).
I have 3 or more tables named like "rp31_bla_2012_bla" or "rp31_bla_2013_bla" or ...
Imagine you have a table with this:
- Name - Age - Job - Year -
Robert - 45 - Doctor - 2001
Robert - 45 - Housemade- 2002
Robert - 45 - Geek - 2006
Robert - 45 - Cooker - 2009
Robert - 45 - (null) - 2013
Nadège - 21 - CallGirl - 2001
Nadège - 21 - (null) - 2002
Nadège - 21 - CallGirl - 2008
Nadège - 21 - Home - 2008
Now I have a WinForm with two textBox, if the user writes "Robert" and "2013" I have to make a report with the "rp31_bla_***2013***_bla" WHERE Name = Robert.
Is it possible?
Which type of report should I create?
How can I change dynamically the query?
You can use union to combine the 3 tables and filter by name
SELECT * FROM
(
SELECT ..., 2012 as Year FROM rp31_bla_2012_bla
UNION
SELECT ..., 2013 as Year FROM rp31_bla_2013_bla
UNION
SELECT ..., 2014 as Year FROM rp31_bla_2014_bla
) t
WHERE t.Year =2013 and t.Name ='Robert'
Since the datasource will never change you don't need to to switch the tables.
This approach will be slower because you will always query the 3 tables
Another option will be to use a command in Crystal reports and to create a dynamic query which will be executed using exec -then you will be able to query just one table but you will need to handle special characters in the name ( like '%?)

Add record to table only if data has changed since last record by date

I need help trying to find a efficient method of adding data into a table only if the data has changed.
I have the first table which is LatestResults and only stores the latest results ( 1 for each team number)
TeamNumber(Primary) TeamScore TeamWorkUnits RecordDateTime
1 - 500 - 600 - 2012-09-22 08:20:00
2 - 400 - 6 - 2012-09-22 08:20:00
3 - 90 - 15 - 2012-09-22 08:20:00
4 - 1 - 0 - 2012-09-22 08:20:00
The other table which is called HistoricResults stores a copy of very result in case I need it for statistics in the future.
HisotryKey(Auto-Primary) TeamNumber TeamScore TeamWorkUnits RecordDateTime
1 - 1 500 - 600 - 2012-09-22 08:20:00
2 - 2 -400 - 6 - 2012-09-22 08:20:00
3 - 3- 90 - 15 - 2012-09-22 08:20:00
4 - 4 - 1 - 0 - 2012-09-22 08:20:00
5 - 1 490 - 600 - 2012-09-21 08:20:00
6 - 2 -300 - 6 - 2012-09-21 08:20:00
7 - 3- 40 - 15 - 2012-09-21 08:20:00
8 - 4 - 0 - 0 - 2012-09-21 08:20:00
So I want to merge the first table into the second only if the data TeamScore or TeamWorkUnits has changed since the last record by RecordDateTime.
Meaning if TeamScore and TeamWorkUnits are the same you keep the oldest entry in the History table.
I would by far prefer do this totally within MySQL for speed if possible.
Currently I need to check about 100,000 records per hour
Thanks in advance for your help.
You can try this approach:
insert into HistoricResults
(teamNumber, TeamScore, TeamWorkUnits, RecordDateTime)
select
lr.teamNumber,
lr.TeamScore,
lr.TeamWorkUnits,
NOW()
from
LatestResults lr
where
exists (
select
*
from
HistoricResults hs
where
hs.teamNumber = lr.teamNumber and
(hs.TeamScore != lr.TeamScore or hs.TeamWorkUnits != lr.TeamWorkUnits)
and hs.RecordDateTime = (select max(RecordDateTime)
from HistoricResults hs1
where hs1.teamNumber = lr.teamNumber)
order by RecordDateTime desc
)
Here is how it should works: subquery within exist is looking in HistoricResults for the most recent records with TeamScore or TeamWorkUnits different from LatestResults table record. If such record is found, then external select selects appropriate records from LatestResults and such records are finally inserted into HistoricResults.