将Case语句的所有结果放到一行

时间:2017-08-29 14:27:41

标签: sql oracle case

我正试图将多个月的结果分成一行:

SELECT id, 
       NAME, 
       year, 
       period, 
       CASE 
         WHEN sub_period = 'S1' THEN Sum(credits) 
         ELSE 0 
       END Tot_S1_Cred, 
       CASE 
         WHEN sub_period = 'S2' THEN Sum(credits) 
         ELSE 0 
       END Tot_S2_Cred, 
       CASE 
         WHEN sub_period NOT IN ( 'S1', 'S2' ) THEN Sum(credits) 
         ELSE 0 
       END Tot_Other_Cred 
FROM   student 
WHERE  period = '201750' 
       AND status IN ( 'RE', 'RW' ) 
GROUP  BY id, 
          NAME, 
          year, 
          period, 
          sub_period 

目前,我正在为每个有效的案例陈述找到一行:

|ID    | NAME    |YEAR   |PERIOD   |S1_CRED   |S2     |Other
|---   |----     |-------|------   |-------   |------ |------
|1     |Abbott   |2017   |201750   |0         |3      | 0
|1     |Abbott   |2017   |201750   |0         |0      |0
|2     |Costello |2017   |201750   |0         |3      |0
|3     |Popeye   |2017   |201750   |3         |0      |0
|3     |Popeye   |2017   |201750   |0         |6      |0
|3     |Popeye   |2017   |201750   |0         |0      |9

3 个答案:

答案 0 :(得分:2)

我认为你想要一个有条件的SUM,所以把CASE放在SUM()

SELECT id, 
       NAME, 
       year, 
       period, 
       SUM ( CASE WHEN sub_period = 'S1' 
                  THEN credits
                  ELSE 0 
             END
           ) as Tot_S1_Cred, 
       SUM ( CASE WHEN sub_period = 'S2' 
                  THEN credits
                  ELSE 0 
             END
           ) as Tot_S2_Cred, 
       SUM ( CASE WHEN sub_period NOT IN ('S1', 'S2')
                  THEN credits
                  ELSE 0 
             END
           ) as Tot_Other_Cred,   
FROM   student 
WHERE  period = '201750' 
       AND status IN ( 'RE', 'RW' ) 
GROUP  BY id, 
          NAME, 
          year, 
          period

答案 1 :(得分:0)

您需要使用相同的案例:

select Id
      ,name
      ,year
      ,Period
      ,case
         when Sub_Period in ('S1', 'S2')  then
          sum(Credits)
         else
          sum(Credits)
       end Tot_S1_Cred
  from Student
 where Period = '201750'
   and Status in ('RE', 'RW')
 group by Id
         ,name
         ,year
         ,Period
         ,Sub_Period

答案 2 :(得分:0)

将您的案件陈述替换为:

   Case When sub_period = 'S1'                       Then 'Tot_S1_Cred'
        When sub_period = 'S2'                       Then 'Tot_S2_Cred'
        When sub_period NOT IN ( 'S1', 'S2' )        Then 'Tot_Other_Cred'
        Else 'NA' End   As 'Credit Type'

   ,Sum(credits) As 'Amount'

完整查询:

SELECT id, 
       NAME, 
       year, 
       period, 
       Case When sub_period = 'S1'                  Then 'Tot_S1_Cred'
            When sub_period = 'S2'                  Then 'Tot_S2_Cred'
            When sub_period NOT IN ( 'S1', 'S2' )   Then 'Tot_Other_Cred'
            Else 'NA' End   As 'Credit Type'
        ,SUM(credits)

FROM   student 
WHERE  period = '201750' 
       AND status IN ( 'RE', 'RW' ) 
GROUP  BY id, 
          NAME, 
          year, 
          period,