ORA-00937——Oracle中GROUP BY搭配CASE WHEN的一則SQL報錯

feelpurple發表於2015-08-27
需要將原始語句改成列傳行,原始語句如下:

寫了一個SQL,語句是這樣的
select to_char('countdate', 'YYYY-MM-DD') "日期",
       count(case
               when activetime > 60 * 60 then
                count(1)
             end) ">=1h",
       count(case
               when activetime > 120 * 60 then
                count(1)
             end) ">=2h",
       count(case
               when activetime > 180 * 60 then
                count(1)
             end) ">=3h",
       count(case
               when activetime > 240 * 60 then
                count(1)
             end) ">=4h",
       count(case
               when activetime > 300 * 60 then
                count(1)
             end) ">=5h",
       count(case
               when activetime > 360 * 60 then
                count(1)
     end) ">=6h"
  from ELMP_ANALYSIS_LOGINTIMEDETAIL
 where activetime > 60 * 60
   and countdate between to_date('&1', 'YYYY-MM-DD') and
       to_date('&2', 'YYYY-MM-DD')
 group by to_char('countdate', 'YYYY-MM-DD')
 order by 1;

執行的時候,一直報錯ORA-00937,仔細檢查,發現在count裡面還套了一層count,導致報錯,修改後的語句如下:

select countdate "日期" count(case

         when activetime > 60 * 60 then

          activetime

       end) ">=1h",

       count(case

               when activetime > 120 * 60 then

                activetime

             end) ">=2h",

       count(case

               when activetime > 180 * 60 then

                activetime

             end) ">=3h",

       count(case

               when activetime > 240 * 60 then

                activetime

             end) ">=4h",

       count(case

               when activetime > 300 * 60 then

                activetime

             end) ">=5h",

       count(case

               when activetime > 360 * 60 then

                activetime

             end) ">=6h"

  from ELMP_ANALYSIS_LOGINTIMEDETAIL

 where activetime > 60 * 60

   and countdate between to_date('&1', 'YYYY-MM-DD') and

       to_date('&2', 'YYYY-MM-DD')

 group by countdate

 order by 1;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-1783791/,如需轉載,請註明出處,否則將追究法律責任。

相關文章