MySQL Categorise data based on two columns -


   **base table:**     col 1     col2     25         184     35         200     21         75     10         206     55         114      75         190      **expected output:**               0-50  50-100  100-150  >150     0-10      0       0       0       1     10-20     0       1       0       0     20-30     0       0       0       1     >30       0       0       1       2  need write mysql query print count of values 2 columns(col1, col2) rite box in matrix format (as given above). 

query had used:

select  case  when col1 between 0 , 10 '0-10' when col1 between 10 , 20 '10-20' when col1 between 20 , 30 '20-30' else '>30' end freq, count( case  when col2 between 0 , 50 , col1 between 0 , 10 '0-10' when col2 between 0 , 50 , col1 between 10 , 20 '10-20' when col2 between 0 , 50 , col1 between 20 , 30 '20-30' when col2 between 0 , 50 , col1 >30 '>30' end) '0-50', count( case  when col2 between 50 , 100 , col1 between 0 , 10 '0-10' when col2 between 50 , 100 , col1 between 10 , 20 '10-20' when col2 between 50 , 100 , col1 between 20 , 30 '20-30' when col2 between 50 , 100 , col1 >30 '>30' end) '50-100', count( case  when col2 between 100 , 150 , col1 between 0 , 10 '0-10' when col2 between 100 , 150 , col1 between 10 , 20 '10-20' when col2 between 100 , 150 , col1 between 20 , 30 '20-30' when col2 between 100 , 150 , col1 >30 '>30' end) '100-150', count( case when col2>150 , col1 between 0 , 10 '0-10' when col2>150 , col1 between 10 , 20 '10-20' when col2>150 , col1 between 20 , 30 '20-30' when col2>150 , col1 >30 '>30' end) '>150', count(col2) table_1  group freq; 

i not getting correct count records. correct count appears first row in output. not sure why goes wrong others. appreciated.

thank you.

it blunder mistake made. while including limits had used upper limit in first case statement lower limit in next statement. thank inputs. same 2 different queries given below:

**query1:**  select  case  when f_value between 0 , 25 '0-25' when f_value between 26 , 50 '25-50' when f_value between 51 , 75 '50-75' when f_value between 76 , 100 '>75' else 'others' end freq, count( case  when r_value between 0 , 500 , f_value between 0 , 25 '0-25' when r_value between 0 , 500 , f_value between 26 , 50 '25-50' when r_value between 0 , 500 , f_value between 51 , 75 '50-75' when r_value between 0 , 500 , f_value>=76 '>75' end) '0-500', count( case  when r_value between 501 , 1000 , f_value between 0 , 25 '0-25' when r_value between 501 , 1000 , f_value between 26 , 50 '25-50' when r_value between 501 , 1000 , f_value between 51 , 75 '50-75' when r_value between 501 , 1000 , f_value>=76 '>75' end) '500-1000', count( case  when r_value between 1001 , 1500 , f_value between 0 , 25 '0-25'  when r_value between 1001 , 1500 , f_value between 26 , 50 '25-50'  when r_value between 1001 , 1500 , f_value between  51 , 75 '50-75' when r_value between 1001 , 1500 , f_value>=76 '>75' end) '1000-1500', count( case when r_value> 1501  , f_value between 0 , 25 '0-25'  when r_value> 1501 , f_value between 26 , 50 '25-50'   when r_value> 1501 , f_value between 51 , 75 '50-75'  when r_value> 1501 , f_value>=76 '>=75' end) '>1501' em_mem_prfm_cat_hist  rfm_calc_batch=4 group freq;   **query 2:** select      b.x,     max(if(y = '0-500', sss, null)) '0-500',     max(if(y = '501-1000', sss, null)) '501-1000',     max(if(y = '1001-1500', sss, null)) '1001-1500',     max(if(y = '1501-2000', sss, null)) '1501-2000'     ((select          x, y, count(1) sss             (select          case                 when f_value between 0 , 25 '0-25'                 when f_value between 26 , 50 '26-50'                 when f_value between 51 , 75 '51-75'                 when f_value >= 76 '76-100'             end x,             case                 when r_value between 0 , 500 '0-500'                 when r_value between 501 , 1000 '501-1000'                 when r_value between 1001 , 1500 '1001-1500'                 when r_value >= 1501 '1501-2000'             end y             em_mem_prfm_cat_hist             rfm_calc_batch = 4)     group x , y)) b group x order x 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -