Solved

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

Posted on 2013-05-15
8
341 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
Comment Utility
Replace each value (Max_Bid1, Min_Bid1, etc) with coalesce(Max_Bid1,0) and so on.
0
 
LVL 16

Author Comment

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

Expert Comment

by:Sharath
Comment Utility
You do not have any data in your sqlfiddle?
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 40

Expert Comment

by:Sharath
Comment Utility
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
Comment Utility
0
 
LVL 16

Author Comment

by:hankknight
Comment Utility
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
Comment Utility
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

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

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

11 Experts available now in Live!

Get 1:1 Help Now