Solved

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

Posted on 2013-05-15
8
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 41

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 41

Expert Comment

by:Sharath
ID: 39168555
You do not have any data in your sqlfiddle?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 41

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 41

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 41

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 41

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

707 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