从group-by子句中省略引用的列

时间:2015-07-12 06:38:40

标签: sql oracle

Geography  Project_name  Test_name  Actual count  Plan count
Russia     test1         samp4                 3           3
Russia     test1         samp5                66          19
Russia     test1         samp2               133          79
Russia     gen1          samp1               170          16
Vietnam    test1         samp2                35          17
Vietnam    gen1          samp1                91           4

我得到以下数据 -

Geography  Project name actual count
Russia     test1                 202
Russia     Gen1                  170
Vietnam    Test1                  35
Vietnam    Gen1                   91

我想只按地理位置和project_name进行分组:

 SELECT GEOGRAPHY ,  
      CASE
        WHEN project_name = 'test1'
        THEN 'test1'
        WHEN project_name = 'test2'
        THEN 'test2'
        WHEN project_name    = 'test3'
        AND test_NAME = 'samp1'
        THEN 'gen1'
        WHEN project_name     = 'test3'
        AND  TEST_name <> 'samp1'
        THEN 'gen2'
        ELSE project_name
      END PROJECT_NAME from table 1
    group by 
    geography ,CASE
        WHEN project_name = 'test1'
        THEN 'test1'
        WHEN project_name = 'test2'
        THEN 'test2'
        WHEN project_name    = 'test3'
        AND test_NAME = 'samp1'
        THEN 'gen1'
        WHEN project_name     = 'test3'
        AND  TEST_name <> 'samp1'
        THEN 'gen2'
        ELSE project_name
      END .

我得到了这个问题的答案......

{{1}}

这很有效......并感谢所有试图帮助我的人。

1 个答案:

答案 0 :(得分:0)

试试这个:

select geography, project_name
  from ( select geography
              , project
              , case when project_name = 'test1' then 'test1'
                     when project_name = 'test2' then 'test2'
                     when project_name = 'test3'
                       and test_name = 'samp1' then 'gen1'
                     when project_name = 'test3'
                       and test_name <> 'samp1' then 'gen2'
                     else project_name
                     end project_name
           from table1 )
 group by geography, project_name