473,386 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Calculating existing measurements for Cumulative BTUs

dbrewerton
115 100+
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.

Expand|Select|Wrap|Line Numbers
  1. ewise_slice
  2.        |
  3.        > slice_id
  4.        > device_id
  5.        > slice_timestamp
  6.  
  7. ewise_slice_msmnt
  8.        |
  9.        > slice_msmnt_id
  10.        > slice_id
  11.        > msmnt_type_id
  12.        > msmnt_value
  13.  
My cumulative BTU table has the following structure:

Expand|Select|Wrap|Line Numbers
  1. ewise_cumulative_btus
  2.        |
  3.        > id
  4.        > device_id
  5.        > curbtus
  6.        > cumul_btus
  7.        > ewise_slice
  8.  
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 :)
Mar 22 '10 #1

✓ answered by dbrewerton

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:

Expand|Select|Wrap|Line Numbers
  1. main_loop: LOOP
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
dbrewerton
115 100+
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:

Expand|Select|Wrap|Line Numbers
  1. ###################
  2. delimiter ;
  3.  
  4. DROP PROCEDURE IF EXISTS `calcbtus`;
  5. DELIMITER $$
  6. CREATE PROCEDURE `calcbtus`()
  7.  
  8. BEGIN
  9.  
  10. # slice1 is active slice - slice2 is the previous slice - time1 and time2 relate to these respectively
  11. DECLARE slice1 bigint;
  12. DECLARE slice2 bigint;
  13. DECLARE time1 DATETIME;
  14. DECLARE time2 DATETIME;
  15. DECLARE x_device_id BIGINT;
  16. DECLARE slicetimediff BIGINT;
  17.  
  18. #variables for BTUHr calc
  19. DECLARE flowrate BIGINT;
  20. DECLARE brinefactor BIGINT;
  21.  
  22. #BTU/Hr calc 1
  23. DECLARE btuhr1 bigint;
  24. DECLARE t1_1 BIGINT;
  25. DECLARE t2_1 BIGINT;
  26. DECLARE c1_1 BIGINT;
  27.  
  28. #BTU/Hr calc 2
  29. DECLARE btuhr2 BIGINT;
  30. DECLARE t1_2 BIGINT;
  31. DECLARE t2_2 BIGINT;
  32. DECLARE c1_2 BIGINT;
  33.  
  34. #calculation of (BTUHr1+BTUHr2)* 2 * slicetimediff/60
  35. DECLARE btucalc bigint;
  36.  
  37. #Insertion values for cumulative BTUs
  38. DECLARE lastbtuid BIGINT;
  39. DECLARE lastbturead BIGINT;
  40. DECLARE lastcurbtus BIGINT;
  41. DECLARE x_id bigint;
  42.  
  43. # Create loop for slices
  44. main_loop: LOOP
  45.  
  46.         SET x_id = slice1;
  47.  
  48. # Idea, compare maxsliceid to x_id? so if x_id > maxsliceid leave loop...not sure...
  49.  
  50.         if (x_id IS NULL) then
  51.             set x_id = (select slice_id from ewise_slice order by slice_id limit 1);
  52.         end if
  53.  
  54.         select device_id from ewise_slice where slice_id = x_id;
  55.     select slice_id from ewise_slice where slice_id = x_id order by slice_id LIMIT 1 into slice1;
  56.     select slice_id from ewise_slice where slice_id = x_id order by slice_id LIMIT 1,1 into slice2;
  57.  
  58.     select slice_timestamp from ewise_slice where slice_id = slice1 INTO time1;
  59.     select slice_timestamp from ewise_slice where slice_id = slice2 INTO time2;
  60.  
  61.     SELECT TIMESTAMPDIFF(MINUTE,time2,time1) into slicetimediff;
  62.     if (slicetimediff IS NULL) then
  63.         set slicetimediff = 1;
  64.     end if;
  65.     if (slicetimediff > 60) then
  66.         set slicetimediff = 1;
  67.     end if;
  68.  
  69.     select egw_con_flowrate from egw_constants where egw_con_device_id = x_device_id INTO flowrate;
  70.     select brine from egw_constants where egw_con_device_id = x_device_id INTO brinefactor;
  71.  
  72.     select msmnt_value from ewise_slice_msmnt where slice_id = slice1 AND msmnt_type_id = '1' INTO t1_1;
  73.     select msmnt_value from ewise_slice_msmnt where slice_id = slice1 AND msmnt_type_id = '2' INTO t2_1;
  74.     select msmnt_value from ewise_slice_msmnt where slice_id = slice1 AND msmnt_type_id = '3' INTO c1_1;
  75.  
  76.     select msmnt_value from ewise_slice_msmnt where slice_id = slice2 AND msmnt_type_id = '1' INTO t1_2;
  77.     select msmnt_value from ewise_slice_msmnt where slice_id = slice2 AND msmnt_type_id = '2' INTO t2_2;
  78.     select msmnt_value from ewise_slice_msmnt where slice_id = slice2 AND msmnt_type_id = '3' INTO c1_2;
  79.  
  80.     SELECT ABS(t2_1 - t1_1) * flowrate * brinefactor * c1_1 INTO BTUHr1;
  81.     SELECT ABS(t2_2 - t1_2) * flowrate * brinefactor * c1_2 INTO BTUHr2;
  82.  
  83.     if (BTUHr2 IS NULL) then
  84.         set BTUHR2 = 0;
  85.     end if;
  86.  
  87.     SELECT (ABS(BTUHr1)+ABS(BTUHr2))/2*(slicetimediff/60) INTO btucalc;
  88.  
  89.     select id from ewise_cumulative_btus where device_id = x_device_id AND id = slice2 INTO lastbtuid;
  90.  
  91.     select ABS(cumul_btus) from ewise_cumulative_btus where id = lastbtuid INTO lastbturead;
  92.  
  93.     if (lastbturead IS NULL) then
  94.         set lastbturead = 0;
  95.     end if;
  96.  
  97.     if ABS(TIMESTAMPDIFF(MINUTE,time2,time1)) > 61 then
  98.         set btucalc = 0;
  99.     end if;
  100.  
  101.     select (ABS(btucalc) + ABS(lastbturead)) INTO lastcurbtus;
  102.  
  103.     if slicetimediff < 61 then
  104.     insert into ewise_cumulative_btus (device_id,curbtus,cumul_btus,slice_id) values(x_device_id,btucalc,lastcurbtus,slice1);
  105.     else
  106.     insert into ewise_cumulative_btus (device_id,curbtus,cumul_btus,slice_id) values(x_device_id,btucalc,'0',slice1);
  107.     end if;
  108.  
  109.     # Uncomment the line below for command line debugging
  110.     select slice1,time1,slice2,time2,slicetimediff,BTUHr1,BTUHr2,lastbtuid,lastbturead,x_device_id,btucalc,lastcurbtus;
  111.     set slice1 = slice1++
  112.  
  113. END LOOP main_loop;
  114. END
  115.  
  116. $$
  117. DELIMITER ;
  118. ###################
  119.  
Mar 22 '10 #2
dbrewerton
115 100+
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:

Expand|Select|Wrap|Line Numbers
  1. main_loop: LOOP
Can anyone help me here? I was told by a programmer friend to declare the character set latin1 but that didn't help either.
Mar 23 '10 #3
dbrewerton
115 100+
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.
Mar 24 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

13
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...
3
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...
4
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...
1
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
4
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...
1
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 ...
0
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...
3
ChaseCox
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...
1
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...
4
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...
0
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,...
0
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$) { } ...
0
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...
0
BarryA
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...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.