When Oracle uses pivot, matters needing attention

a) When Pivot, it is actually a grouping, and statistics can be achieved through the clustering function

b) When Pivot takes the column name, if the alias has Chinese characters, it should be enclosed in double quotation marks, or double quotation marks should not be used

c) If the corresponding field value is not a number or letter, remember to use single quotation marks, such as the following 'A'

select *

  from (select case

                 when a.score >= 220 and a.score <= 330 then

                  '(220,330)'

                 when a.score >= 630 and a.score <= 710 then

                  '(630,710)'

                 when a.score < 220 then

                  'other'

                 else

                  '(' || (ceil((a.score - 330) / 20) * 20 + 330) || ',' ||

                  (ceil((a.score - 330) / 20 + 1) * 20 + 330) || ')'

              

               end fractional segment,

               b.score

          from SUBJECTREGISTER a

          left join SUBJECTREGISTER b

            on a.candidatesid = b.candidatesid

         WHERE a.tests = '181'

           and a.CAMPUSCODE LIKE '12%'

           AND a.SUBJECTCODE != 'S'

           AND a.SUBJECTCODE != 'F'

           AND (a.SCORE != '0' AND a.SCORE IS NOT NULL and

               b.score IS NOT NULL)

           AND (b.SUBJECTCODE = 'S' or b.SUBJECTCODE = 'F')) pivot(count(SCORE) FOR SCORE IN('A' "A",

                                                                                             'A+' "A+",

                                                                                             'B' "B",

                                                                                             'B+' "B+",

                                                                                             'C' "C",

                                                                                             'C+' "C+",

                                                                                             'D' "D",

                                                                                             'D+' "D+"))

 ORDER BY Fractions

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324874551&siteId=291194637