Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL: Get Average of Seven Queries

Posted on 2013-05-14
6
Medium Priority
?
403 Views
Last Modified: 2013-05-15
I want the average value for all seven of these queries:

SELECT ((MAX(`Bid`)-MIN(`BID`))) FROM `records` WHERE `Pair` = 'USDCHF' 
AND 
`Time` > ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-86400-(86400*0)) 
AND 
`Time` < ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-72000-(86400*0)) 
LIMIT 1

SELECT ((MAX(`Bid`)-MIN(`BID`))) FROM `records` WHERE `Pair` = 'USDCHF' 
AND 
`Time` > ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-86400-(86400*1)) 
AND 
`Time` < ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-72000-(86400*1)) 
LIMIT 1

SELECT ((MAX(`Bid`)-MIN(`BID`))) FROM `records` WHERE `Pair` = 'USDCHF' 
AND 
`Time` > ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-86400-(86400*2)) 
AND 
`Time` < ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-72000-(86400*2)) 
LIMIT 1

SELECT ((MAX(`Bid`)-MIN(`BID`))) FROM `records` WHERE `Pair` = 'USDCHF' 
AND 
`Time` > ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-86400-(86400*3)) 
AND 
`Time` < ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-72000-(86400*3)) 
LIMIT 1

SELECT ((MAX(`Bid`)-MIN(`BID`))) FROM `records` WHERE `Pair` = 'USDCHF' 
AND 
`Time` > ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-86400-(86400*4)) 
AND 
`Time` < ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-72000-(86400*4)) 
LIMIT 1

SELECT ((MAX(`Bid`)-MIN(`BID`))) FROM `records` WHERE `Pair` = 'USDCHF' 
AND 
`Time` > ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-86400-(86400*5)) 
AND 
`Time` < ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-72000-(86400*5)) 
LIMIT 1

SELECT ((MAX(`Bid`)-MIN(`BID`))) FROM `records` WHERE `Pair` = 'USDCHF' 
AND 
`Time` > ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-86400-(86400*6)) 
AND 
`Time` < ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-72000-(86400*6)) 
LIMIT 1

SELECT ((MAX(`Bid`)-MIN(`BID`))) FROM `records` WHERE `Pair` = 'USDCHF' 
AND 
`Time` > ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-86400-(86400*7)) 
AND 
`Time` < ((SELECT `Time` FROM `records` ORDER BY `id` DESC LIMIT 1 )-72000-(86400*7)) 
LIMIT 1

Open in new window

0
Comment
Question by:hankknight
  • 4
  • 2
6 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 39166101
try this.
SELECT ( ( Max_Bid1 - Min_Bid1 ) + ( Max_Bid2 - Min_Bid2 ) + ( Max_Bid3 - Min_Bid3 ) + ( Max_Bid4 - Min_Bid4 ) + ( Max_Bid5 - Min_Bid5 ) + ( Max_Bid6 - Min_Bid6 ) + ( Max_Bid7 - Min_Bid7 ) ) / 7 Avg_Bid
  FROM (SELECT MAX(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 1 ) THEN `Bid` 
                   END) Max_Bid1, 
               MIN(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 1 ) THEN `Bid` 
                   END) Min_Bid1, 
               MAX(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 2 ) THEN `Bid` 
                   END) Max_Bid2, 
               MIN(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 2 ) THEN `Bid` 
                   END) Min_Bid2, 
               MAX(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 3 ) THEN `Bid` 
                   END) Max_Bid3, 
               MIN(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 3 ) THEN `Bid` 
                   END) Min_Bid3, 
               MAX(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 4 ) THEN `Bid` 
                   END) Max_Bid4, 
               MIN(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 4 ) THEN `Bid` 
                   END) Min_Bid4, 
               MAX(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 5 ) THEN `Bid` 
                   END) Max_Bid5, 
               MIN(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 5 ) THEN `Bid` 
                   END) Min_Bid5, 
               MAX(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 6 ) THEN `Bid` 
                   END) Max_Bid6, 
               MIN(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 6 ) THEN `Bid` 
                   END) Min_Bid6, 
               MAX(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 7 ) THEN `Bid` 
                   END) Max_Bid7, 
               MIN(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 7 ) THEN `Bid` 
                   END) Min_Bid7 
          FROM `records`, 
               (SELECT `Time` 
                  FROM `records` 
                 ORDER BY `id` DESC 
                 LIMIT 1) t2 
         WHERE `Pair` = 'USDCHF') t1 

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 39166111
corrected some copy-paste errors.
SELECT ( ( Max_Bid1 - Min_Bid1 ) + ( Max_Bid2 - Min_Bid2 ) + ( Max_Bid3 - Min_Bid3 ) + ( Max_Bid4 - Min_Bid4 ) + ( Max_Bid5 - Min_Bid5 ) + ( Max_Bid6 - Min_Bid6 ) + ( Max_Bid7 - Min_Bid7 ) ) / 7 Avg_Bid
  FROM (SELECT MAX(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 1 ) THEN `Bid` 
                   END) Max_Bid1, 
               MIN(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 1 ) THEN `Bid` 
                   END) Min_Bid1, 
               MAX(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 2 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 2 ) THEN `Bid` 
                   END) Max_Bid2, 
               MIN(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 2 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 2 ) THEN `Bid` 
                   END) Min_Bid2, 
               MAX(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 3 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 3 ) THEN `Bid` 
                   END) Max_Bid3, 
               MIN(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 3 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 3 ) THEN `Bid` 
                   END) Min_Bid3, 
               MAX(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 4 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 4 ) THEN `Bid` 
                   END) Max_Bid4, 
               MIN(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 4 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 4 ) THEN `Bid` 
                   END) Min_Bid4, 
               MAX(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 5 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 5 ) THEN `Bid` 
                   END) Max_Bid5, 
               MIN(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 5 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 5 ) THEN `Bid` 
                   END) Min_Bid5, 
               MAX(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 6 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 6 ) THEN `Bid` 
                   END) Max_Bid6, 
               MIN(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 6 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 6 ) THEN `Bid` 
                   END) Min_Bid6, 
               MAX(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 7 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 7 ) THEN `Bid` 
                   END) Max_Bid7, 
               MIN(CASE 
                     WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 7 ) 
                          AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 7 ) THEN `Bid` 
                   END) Min_Bid7 
          FROM `records`, 
               (SELECT `Time` 
                  FROM `records` 
                 ORDER BY `id` DESC 
                 LIMIT 1) t2 
         WHERE `Pair` = 'USDCHF') t1 

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 39166305
Thanks, but that gives me an error.

Please test it here:
http://sqlfiddle.com/#!2/5dc83/1
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 41

Expert Comment

by:Sharath
ID: 39166394
can you add some sample data to your table in the sqlfiddle?
0
 
LVL 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 39166397
Anyway, fixed that issue.

http://sqlfiddle.com/#!2/5dc83/7
0
 
LVL 16

Author Comment

by:hankknight
ID: 39167302
Thanks.  Your query works ONLY if all seven periods contain values.

If any of the values are null then whole thing fails.

I have asked a related question here:
http://www.experts-exchange.com/Database/MySQL/Q_28128843.html
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question