Solved

MySQL: Get Average Rage of 7 Queries that are NOT NULL

Posted on 2013-05-15
8
353 Views
Last Modified: 2013-05-16
I use the following MySQL query to find the average range of seven queries:

Thanks, Sharath_123, for the answer you posted here:
http://www.experts-exchange.com/Database/MySQL/Q_28127916.html
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` t1, 
               (SELECT `Time` 
                  FROM `records` 
                 ORDER BY `id` DESC 
                 LIMIT 1) t2 
         WHERE `Pair` = 'USDCHF') t3

Open in new window

The problem is that if the range for ANY of these periods is NULL (due to weekends or holidays) then the entire query fails.

How can this be modified so that result returned is the average range for only the periods that have actual values?  

For example if periods 2, 3 and 7 return null results then they should be excluded from the average.

I have attached a database dump containing the structure and some sample data.
pairDump.png
pairDump.sql.txt
0
Comment
Question by:hankknight
  • 6
  • 2
8 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 39168478
Replace each value (Max_Bid1, Min_Bid1, etc) with coalesce(Max_Bid1,0) and so on.
0
 
LVL 16

Author Comment

by:hankknight
ID: 39168537
I still get null as the result even with real data:
http://sqlfiddle.com/#!2/5dc83/8
0
 
LVL 40

Expert Comment

by:Sharath
ID: 39168555
You do not have any data in your sqlfiddle?
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 40

Expert Comment

by:Sharath
ID: 39168581
Can you try this?
SELECT ( COALESCE(Bid1, 0) + COALESCE(Bid2, 0) 
         + COALESCE(Bid3, 0) + COALESCE(Bid4, 0) 
         + COALESCE(Bid5, 0) + COALESCE(Bid6, 0) 
         + COALESCE(Bid7, 0) ) / ( ( CASE 
                                       WHEN Bid1 IS NULL THEN 0 
                                       ELSE 1 
                                     END ) + ( CASE 
                                                 WHEN Bid2 IS NULL THEN 0 
                                                 ELSE 1 
                                               END ) + ( CASE 
                                                           WHEN Bid3 IS NULL THEN 0 
                                                           ELSE 1 
                                                         END ) + ( CASE 
                                                                     WHEN Bid4 IS NULL THEN 0
                                                                     ELSE 1 
                                                                   END ) + ( CASE 
                                                                               WHEN Bid5 IS NULL THEN 0
                                                                               ELSE 1 
                                                                             END ) + ( CASE 
                                                                                         WHEN Bid6 IS NULL THEN 0
                                                                                         ELSE 1
                                                                                       END ) + ( CASE
                                                                                                   WHEN Bid7 IS NULL THEN 0
                                                                                                   ELSE 1
                                                                                                 END ) )
  FROM (SELECT ( Max_Bid1 - Min_Bid1 ) Bid1, 
               ( Max_Bid2 - Min_Bid2 ) Bid2, 
               ( Max_Bid3 - Min_Bid3 ) Bid3, 
               ( Max_Bid4 - Min_Bid4 ) Bid4, 
               ( Max_Bid5 - Min_Bid5 ) Bid5, 
               ( Max_Bid6 - Min_Bid6 ) Bid6, 
               ( Max_Bid7 - Min_Bid7 ) Bid7 
          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` t1, 
                       (SELECT `Time` 
                          FROM `records` 
                         ORDER BY `id` DESC 
                         LIMIT 1) t2 
                 WHERE `Pair` = 'USDCHF') t3)t4 

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 39168606
fixed again
SELECT ( Bid1 + Bid2 + Bid3 + Bid4+ + Bid5 + Bid6 + Bid7 ) / ( ( CASE 
                                                                   WHEN Bid1 = 0 THEN 0 
                                                                   ELSE 1 
                                                                 END ) + ( CASE 
                                                                             WHEN Bid2 = 0 THEN 0
                                                                             ELSE 1 
                                                                           END ) + ( CASE 
                                                                                       WHEN Bid3 = 0 THEN 0
                                                                                       ELSE 1
                                                                                     END ) + ( CASE
                                                                                                 WHEN Bid4 = 0 THEN 0
                                                                                                 ELSE 1
                                                                                               END ) + ( CASE
                                                                                                           WHEN Bid5 = 0 THEN 0
                                                                                                           ELSE 1
                                                                                                         END ) + ( CASE
                                                                                                                     WHEN Bid6 = 0 THEN 0
                                                                                                                     ELSE 1
                                                                                                                   END ) + ( CASE
                                                                                                                               WHEN Bid7 = 0 THEN 0
                                                                                                                               ELSE 1
                                                                                                                             END ) ) Avg_Bid
  FROM (SELECT ( Max_Bid1 - Min_Bid1 ) Bid1, 
               ( Max_Bid2 - Min_Bid2 ) Bid2, 
               ( Max_Bid3 - Min_Bid3 ) Bid3, 
               ( Max_Bid4 - Min_Bid4 ) Bid4, 
               ( Max_Bid5 - Min_Bid5 ) Bid5, 
               ( Max_Bid6 - Min_Bid6 ) Bid6, 
               ( Max_Bid7 - Min_Bid7 ) Bid7 
          FROM (SELECT MAX(CASE 
                             WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                                  AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 1 ) THEN `Bid`
                             ELSE 0 
                           END) Max_Bid1, 
                       MIN(CASE 
                             WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 1 ) 
                                  AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 1 ) THEN `Bid`
                             ELSE 0 
                           END) Min_Bid1, 
                       MAX(CASE 
                             WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 2 ) 
                                  AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 2 ) THEN `Bid`
                             ELSE 0 
                           END) Max_Bid2, 
                       MIN(CASE 
                             WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 2 ) 
                                  AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 2 ) THEN `Bid`
                             ELSE 0 
                           END) Min_Bid2, 
                       MAX(CASE 
                             WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 3 ) 
                                  AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 3 ) THEN `Bid`
                             ELSE 0 
                           END) Max_Bid3, 
                       MIN(CASE 
                             WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 3 ) 
                                  AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 3 ) THEN `Bid`
                             ELSE 0 
                           END) Min_Bid3, 
                       MAX(CASE 
                             WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 4 ) 
                                  AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 4 ) THEN `Bid`
                             ELSE 0 
                           END) Max_Bid4, 
                       MIN(CASE 
                             WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 4 ) 
                                  AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 4 ) THEN `Bid`
                             ELSE 0 
                           END) Min_Bid4, 
                       MAX(CASE 
                             WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 5 ) 
                                  AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 5 ) THEN `Bid`
                             ELSE 0 
                           END) Max_Bid5, 
                       MIN(CASE 
                             WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 5 ) 
                                  AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 5 ) THEN `Bid`
                             ELSE 0 
                           END) Min_Bid5, 
                       MAX(CASE 
                             WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 6 ) 
                                  AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 6 ) THEN `Bid`
                             ELSE 0 
                           END) Max_Bid6, 
                       MIN(CASE 
                             WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 6 ) 
                                  AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 6 ) THEN `Bid`
                             ELSE 0 
                           END) Min_Bid6, 
                       MAX(CASE 
                             WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 7 ) 
                                  AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 7 ) THEN `Bid`
                             ELSE 0 
                           END) Max_Bid7, 
                       MIN(CASE 
                             WHEN t1.`TIME` > t2.`Time` - 86400 - ( 86400 * 7 ) 
                                  AND t1.`TIME` < t2.`Time` - 72000 - ( 86400 * 7 ) THEN `Bid`
                             ELSE 0 
                           END) Min_Bid7 
                  FROM `records` t1, 
                       (SELECT `Time` 
                          FROM `records` 
                         ORDER BY `id` DESC 
                         LIMIT 1) t2 
                 WHERE `Pair` = 'USDCHF') t3)t4 

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 39168609
0
 
LVL 16

Author Comment

by:hankknight
ID: 39169106
Thanks, but it still needs a little tweaking.

Notice that the total range is very small (0.029959976673)
http://sqlfiddle.com/#!2/60197/10

However the code provided returns a much larger number: (0.949609994888)
http://sqlfiddle.com/#!2/60197/4

The average range should be SMALLER than the total range, so it should be a number smaller than 0.029959976673
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 39169352
I have printed all the values to better understand. Clearly, the min_Bid is zero for all 7 Bids. And only Bid4 has the max value. Hence Avg_Bid is displayed as same value of max_Bid4.

http://sqlfiddle.com/#!2/60197/18

If you just simply calculate the max and min Bids, they may not fall in your selection criteria.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP - AJAX and MySQL it works only if the value is a number 12 73
insert row field data graphically 4 28
unable to insert record into a table 2 39
Mysql Left Join Case 10 70
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

837 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