Ok, this is that new post I said I was going to make.
Synopsis of situation:
I have two tables that have measurement slices. One table contains the time slices, the other contains the measurements. -
ewise_slice
-
|
-
> slice_id
-
> device_id
-
> slice_timestamp
-
-
ewise_slice_msmnt
-
|
-
> slice_msmnt_id
-
> slice_id
-
> msmnt_type_id
-
> msmnt_value
-
My cumulative BTU table has the following structure: -
ewise_cumulative_btus
-
|
-
> id
-
> device_id
-
> curbtus
-
> cumul_btus
-
> ewise_slice
-
Now, what I'm trying to do is to step through the existing measurements in ewise_slice/_msmnt tables and thereby calculate my existing readings. I have a SP that will work but what I need to know is how to loop through my existing readings. I will keep working on this but any advice is appreciated :)
Well, it appears the problem I have is in creating a loop in my SP. This makes absolutely no sense to me as my other SP has a loop and runs just fine. Every time I load the SP, I get error 1064 and its pointing at my opening loop statement which is:
Can anyone help me here? I was told by a programmer friend to declare the character set latin1 but that didn't help either.
3 2004
Probably should include what I have so far. My problem is I don't know how to make this loop to the end. Here's the code: -
###################
-
delimiter ;
-
-
DROP PROCEDURE IF EXISTS `calcbtus`;
-
DELIMITER $$
-
CREATE PROCEDURE `calcbtus`()
-
-
BEGIN
-
-
# slice1 is active slice - slice2 is the previous slice - time1 and time2 relate to these respectively
-
DECLARE slice1 bigint;
-
DECLARE slice2 bigint;
-
DECLARE time1 DATETIME;
-
DECLARE time2 DATETIME;
-
DECLARE x_device_id BIGINT;
-
DECLARE slicetimediff BIGINT;
-
-
#variables for BTUHr calc
-
DECLARE flowrate BIGINT;
-
DECLARE brinefactor BIGINT;
-
-
#BTU/Hr calc 1
-
DECLARE btuhr1 bigint;
-
DECLARE t1_1 BIGINT;
-
DECLARE t2_1 BIGINT;
-
DECLARE c1_1 BIGINT;
-
-
#BTU/Hr calc 2
-
DECLARE btuhr2 BIGINT;
-
DECLARE t1_2 BIGINT;
-
DECLARE t2_2 BIGINT;
-
DECLARE c1_2 BIGINT;
-
-
#calculation of (BTUHr1+BTUHr2)* 2 * slicetimediff/60
-
DECLARE btucalc bigint;
-
-
#Insertion values for cumulative BTUs
-
DECLARE lastbtuid BIGINT;
-
DECLARE lastbturead BIGINT;
-
DECLARE lastcurbtus BIGINT;
-
DECLARE x_id bigint;
-
-
# Create loop for slices
-
main_loop: LOOP
-
-
SET x_id = slice1;
-
-
# Idea, compare maxsliceid to x_id? so if x_id > maxsliceid leave loop...not sure...
-
-
if (x_id IS NULL) then
-
set x_id = (select slice_id from ewise_slice order by slice_id limit 1);
-
end if
-
-
select device_id from ewise_slice where slice_id = x_id;
-
select slice_id from ewise_slice where slice_id = x_id order by slice_id LIMIT 1 into slice1;
-
select slice_id from ewise_slice where slice_id = x_id order by slice_id LIMIT 1,1 into slice2;
-
-
select slice_timestamp from ewise_slice where slice_id = slice1 INTO time1;
-
select slice_timestamp from ewise_slice where slice_id = slice2 INTO time2;
-
-
SELECT TIMESTAMPDIFF(MINUTE,time2,time1) into slicetimediff;
-
if (slicetimediff IS NULL) then
-
set slicetimediff = 1;
-
end if;
-
if (slicetimediff > 60) then
-
set slicetimediff = 1;
-
end if;
-
-
select egw_con_flowrate from egw_constants where egw_con_device_id = x_device_id INTO flowrate;
-
select brine from egw_constants where egw_con_device_id = x_device_id INTO brinefactor;
-
-
select msmnt_value from ewise_slice_msmnt where slice_id = slice1 AND msmnt_type_id = '1' INTO t1_1;
-
select msmnt_value from ewise_slice_msmnt where slice_id = slice1 AND msmnt_type_id = '2' INTO t2_1;
-
select msmnt_value from ewise_slice_msmnt where slice_id = slice1 AND msmnt_type_id = '3' INTO c1_1;
-
-
select msmnt_value from ewise_slice_msmnt where slice_id = slice2 AND msmnt_type_id = '1' INTO t1_2;
-
select msmnt_value from ewise_slice_msmnt where slice_id = slice2 AND msmnt_type_id = '2' INTO t2_2;
-
select msmnt_value from ewise_slice_msmnt where slice_id = slice2 AND msmnt_type_id = '3' INTO c1_2;
-
-
SELECT ABS(t2_1 - t1_1) * flowrate * brinefactor * c1_1 INTO BTUHr1;
-
SELECT ABS(t2_2 - t1_2) * flowrate * brinefactor * c1_2 INTO BTUHr2;
-
-
if (BTUHr2 IS NULL) then
-
set BTUHR2 = 0;
-
end if;
-
-
SELECT (ABS(BTUHr1)+ABS(BTUHr2))/2*(slicetimediff/60) INTO btucalc;
-
-
select id from ewise_cumulative_btus where device_id = x_device_id AND id = slice2 INTO lastbtuid;
-
-
select ABS(cumul_btus) from ewise_cumulative_btus where id = lastbtuid INTO lastbturead;
-
-
if (lastbturead IS NULL) then
-
set lastbturead = 0;
-
end if;
-
-
if ABS(TIMESTAMPDIFF(MINUTE,time2,time1)) > 61 then
-
set btucalc = 0;
-
end if;
-
-
select (ABS(btucalc) + ABS(lastbturead)) INTO lastcurbtus;
-
-
if slicetimediff < 61 then
-
insert into ewise_cumulative_btus (device_id,curbtus,cumul_btus,slice_id) values(x_device_id,btucalc,lastcurbtus,slice1);
-
else
-
insert into ewise_cumulative_btus (device_id,curbtus,cumul_btus,slice_id) values(x_device_id,btucalc,'0',slice1);
-
end if;
-
-
# Uncomment the line below for command line debugging
-
select slice1,time1,slice2,time2,slicetimediff,BTUHr1,BTUHr2,lastbtuid,lastbturead,x_device_id,btucalc,lastcurbtus;
-
set slice1 = slice1++
-
-
END LOOP main_loop;
-
END
-
-
$$
-
DELIMITER ;
-
###################
-
Well, it appears the problem I have is in creating a loop in my SP. This makes absolutely no sense to me as my other SP has a loop and runs just fine. Every time I load the SP, I get error 1064 and its pointing at my opening loop statement which is:
Can anyone help me here? I was told by a programmer friend to declare the character set latin1 but that didn't help either.
Never mind, I figured it out on my own. My problem was I was not properly declaring the slice1 variable's value. Once I had that problem licked, the query ran like a champ.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: |
last post by:
I have an Access database used to track donor pledges. In it, there is a
table that contains three fields for each donor: Gift_Amount, Gift_Per_Year,
and Matching_Gift_Ratio.
The following...
|
by: GregM |
last post by:
Hi,
I'm hoping that someone can point me in the right direction with this.
What I would like to do is calculate the average time it takes to load
a page. I've been searching the net and reading...
|
by: Matt Larkin |
last post by:
I am pulling my hair out trying to solve this one (presumably because
I am not particularly trained or skilled at access!)
I have a query which summarises the variances that each of my sales...
|
by: Victor |
last post by:
Hi There,
I have a query witch gives me the following result:
Period NumberOfItems
1 13
2 2
3 1
4 1
5 1
|
by: cefrancke |
last post by:
Are there any ways to speed up a Cumulative sum in a query or perhaps
another faster way to have a cumulative sum column (call a vba
function?).
For example, I want to sum up all values under...
|
by: John Ortt |
last post by:
Hi There,
I'm trying to work out the dwell between stages in our process.
We have a table (Workability) which records progress of parts through the
factory as follows:
PartID Stage ...
|
by: James Hallam |
last post by:
I have searched through the news groups and found many threads close to
what I want but cannot get any of them to work...
I have a table with expenses and invoices, what I want is a bar chart...
|
by: ChaseCox |
last post by:
Hi all,
I have a problem that I have been looking at for a couple days now and I can not quite get it to work.
I would like to calculate the cumulative percent failure of a certain product in...
|
by: wisemen |
last post by:
I have a table with 2 columns, and .I want to run a query that will give me a cumulative sum of the no. of entries that have an <= and on the second column give me a cumulative sum of the no. of...
|
by: JAG |
last post by:
The following line of code worked in my .hta prior to installing
MS08-045 - Cumulative Security Update for Internet Explorer (953838)
(on both XP and W2K):
window.top.frames.location = ;
After...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |