Solved

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

Posted on 2013-05-15
8
346 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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

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 This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

23 Experts available now in Live!

Get 1:1 Help Now