mysql - How do I consolidate case when statements that have (SUM)? -


i want consolidate query....

select substr(prtmst.typcod, 1, 3) article_type,     case when substr(prtmst.typcod, 1, 3) 'a%' 'accessories'          else          case when substr(prtmst.typcod, 1, 3) = 'l02' 'spirit'               else               case when substr(prtmst.typcod, 1, 3) = 'l03' 'wine'                    else                    case when substr(prtmst.typcod, 1, 3) = 'l04' 'beer & soft drinks'                         else                         case when substr(prtmst.typcod, 1, 3) = 'l05' 'confectionary'                              else                              case when substr(prtmst.typcod, 1, 3) = 'l06' 'food'                                   else                                   case when substr(prtmst.typcod, 1, 3) 'p%' 'parfum/cosmetics'                                        else                                        case when substr(prtmst.typcod, 1, 3) 't%' 'tester'                                             else                                             case when substr(prtmst.typcod, 1, 3) 'x%' 'gwp'                                                  else                                                  case when substr(prtmst.typcod, 1, 3) 'z%' 'procurement'                                                       else ''                                                  end                                             end                                        end                                   end                              end                         end                    end               end          end     end "translation",     case when datediff(day, to_char(invdtl.fifdte, 'mm/dd/yyyy'), sysdate) < 90 sum(invdtl.untqty)          else ''     end "less 90",     case when datediff(day, to_char(invdtl.fifdte, 'mm/dd/yyyy'), sysdate) between 91      , 120 sum(invdtl.untqty)          else ''     end "91-120",     case when datediff(day, to_char(invdtl.fifdte, 'mm/dd/yyyy'), sysdate) between 121      , 180 sum(invdtl.untqty)          else ''     end "121-180",     case when datediff(day, to_char(invdtl.fifdte, 'mm/dd/yyyy'), sysdate) between 181      , 360 sum(invdtl.untqty)          else ''     end "181-360",     case when datediff(day, to_char(invdtl.fifdte, 'mm/dd/yyyy'), sysdate) between 361      , 500 sum(invdtl.untqty)          else ''     end "361-500",     case when datediff(day, to_char(invdtl.fifdte, 'mm/dd/yyyy'), sysdate) between 501      , 900 sum(invdtl.untqty)          else ''     end "501-900",     case when datediff(day, to_char(invdtl.fifdte, 'mm/dd/yyyy'), sysdate) > 900 sum(invdtl.untqty)          else ''     end "900+"    prtmst,     invdtl   invdtl.prtnum = prtmst.prtnum , prtmst.prt_client_id = 'hus' , prtmst.wh_id_tmpl = 'mftz' , prtmst.typcod not null , invdtl.prt_client_id = 'hus' , invdtl.ship_line_id null , invdtl.wrkref null , invdtl.lst_arecod not in ('adjs', 'cadj', 'sadj')   group substr(prtmst.typcod, 1, 3),     to_char(invdtl.fifdte, 'mm/dd/yyyy')   order substr(prtmst.typcod, 1, 3) asc 

the problem have result shows this: (413 rows)

article_type    translation less 90    91-120  121-180 181-360 361-500 501-900 900+ a71 accessories         481              l02 spirit  1296                         l02 spirit  6                        l02 spirit          96               l02 spirit          60               l02 spirit          2100                 l02 spirit          1014                 l02 spirit  252                      l02 spirit  318                      l02 spirit      36                   l02 spirit          192              l02 spirit  1848                         l02 spirit  2124                         l02 spirit  1550                         l02 spirit  7547                         l02 spirit  4206                         l03 wine            96               l03 wine            417              l03 wine            258              l03 wine    492                      l03 wine    348                      l03 wine        448                  l03 wine    552                      l03 wine    60                       l04 beer & soft drinks  1416                         l05 confectionary                   19       l05 confectionary                   45       l05 confectionary           108              l05 confectionary               546          l05 confectionary           1112                 

i want results this: (11 rows)

    translation less 90    91-120  121-180 181-360 361-500 501-900 900+ a71             481              l02     19147   36  3462                 l03     1452    448 771              l04     1416                         l05     3666    2153    3630    2691    387 1689     p81     11460   7056    13581   19070   27626   12141    p83                 525 344 253  t85     880 802 2888    3811    4732    2539     t90                 20  786 36   x       5082                    6    z10             250 130     549  

please me.... query not grouping case when statements because have sum function....therefore, showing multiple results consolidated in same column...

you should apply sum() function whole case expression , can avoid whole lot of repetition if apply datediff , substr functions in derived tables.

so after tweaking original query bit query should want:

select      article_type,     case left(article_type, 1)         when 'a' 'accessories'         when 'l'             case article_type                when 'l02' 'spirit'                when 'l03' 'wine'                when 'l04' 'beer & soft drinks'                when 'l05' 'confectionary'                when 'l06' 'food'            end         when 'p' 'parfum/cosmetics'         when 't' 'tester'         when 'x' 'gwp'         when 'z' 'procurement'         else ''     end "translation",     sum(case when diff < 90 invdtl.untqty end) "less 90",     sum(case when diff between 91  , 120 invdtl.untqty end) "91-120",     sum(case when diff between 121 , 180 invdtl.untqty end) "121-180",     sum(case when diff between 181 , 360 invdtl.untqty end) "181-360",     sum(case when diff between 361 , 500 invdtl.untqty end) "361-500",     sum(case when diff between 501 , 900 invdtl.untqty end) "501-900",     sum(case when diff > 900 invdtl.untqty end) "900+" (     select substr(prtmst.typcod, 1, 3) article_type, *     prtmst ) prtmst join (     select datediff(day, to_char(invdtl.fifdte, 'mm/dd/yyyy'), sysdate) diff, *      invdtl ) invdtl on invdtl.prtnum = prtmst.prtnum prtmst.prt_client_id = 'hus'   , prtmst.wh_id_tmpl = 'mftz'   , prtmst.typcod not null   , invdtl.prt_client_id = 'hus'   , invdtl.ship_line_id null   , invdtl.wrkref null   , invdtl.lst_arecod not in ('adjs', 'cadj', 'sadj') group article_type order article_type asc 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -