Solved

MySQL: Get Average of Seven Queries

Posted on 2013-05-14
6
339 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 40

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 40

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 40

Expert Comment

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

Accepted Solution

by:
Sharath earned 500 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Creating and Managing Databases with phpMyAdmin in cPanel.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now