查询结果是这样的:   CITY 年度一级天数最多的月份 一级天数
1  鞍山 Jul-06                      2
2  宝鸡 Sep-06                      8
3  北海 Jul-06                      31
4  北京 Jul-06                      5
5  常德 Aug-06                      16
6  长春 Jun-06                      5
7  长春 Sep-06                      5
8  长春 Jul-06                      5
9  长沙 Jul-06                      5
10 长沙 Nov-06                      5
11 长治 May-06                      3
12 成都 May-06                      8
13 赤峰 Jun-06                      4请问怎么城市相同的记录写到一起   CITY 年度一级天数最多的月份 一级天数
1  鞍山 Jul-06                     2
2  宝鸡 Sep-06                     8
3  北海 Jul-06                     31
4  北京 Jul-06                     5
5  常德 Aug-06                     16
6  长春 Jun-06,Sep-06,Jul-06        5
9  长沙 Jul-06,Nov-06             5
11 长治 May-06                     3
12 成都 May-06                     8
13 赤峰 Jun-06                     4
                

解决方案 »

  1.   

    10G
    select CITY ,WMSYS.WM_CONCAT(年度一级天数最多的月份) 年度一级天数最多的月份, 一级天数 
        from a
           group by CITY ,一级天数 
      

  2.   

    9I
    select CITY ,substr(max(sys_connect_by_path(年度一级天数最多的月份,',')),2) 年度一级天数最多的月份,一级天数 
            from 
               (
                  select a.*,row_number () over(partition by CITY ,一级天数  order city) rn
                        from a
                )
            group by CITY ,一级天数 
            start with rn=1 
            connect by rn-1=prior rn and city=prior city
      

  3.   

    如何对一级天数排序,但并不是要排序。只是增加一个字段,显示,一级天数的排名序号。
    select
         tt.city,
         tt.jcts,
         tt.一级天数,
    --这里增加字段(一级天数排名次序)
         round((tt.一级天数/tt.jcts)*100)||'%' 一级天数年度百分比,
         tttt.年度一级天数最多的月份,
         ttttt.历年I级天数最多的月份,
         tt.实际蓝天数,
         round((tt.实际蓝天数/tt.jcts)*100)||'%' 实际蓝天数年度百分比,
         tt.劣于II天数,
         round((tt.劣于II天数/tt.jcts)*100)||'%' 劣于II天数年度百分比,
         tt.重污染天数,
         round((tt.重污染天数/tt.jcts)*100)||'%' 重污染天数年度百分比
       from   
       (  select 
            t.city,
            count(t.city) jcts,
            sum(decode(t.grade,'Ⅰ',1,0)) 一级天数,
            sum(decode(t.grade,'Ⅰ',1,'Ⅱ',1,0)) 实际蓝天数,
            sum(decode(t.grade,'Ⅰ',0,'Ⅱ',0,1)) 劣于II天数,
            sum(decode(t.grade,'劣Ⅴ',1,'Ⅴ',1,0)) 重污染天数
          from city_day t 
          where to_char(t.oper_date,'yyyy')='2006'
          group by to_char(t.oper_date,'yyyy'),t.city)tt  
      
       left join (
      select CITY ,WMSYS.WM_CONCAT(年度一级天数最多的月份) 年度一级天数最多的月份, 一级天数 
      from  
        (
         select ttt.city,
         ttt.年度一级天数最多的月份,
         ttt.一级天数
         from
           (select tt.*,
       dense_rank () OVER (PARTITION BY tt.city ORDER BY 一级天数 DESC) rn
           from
           (SELECT t.city, TO_CHAR (t.oper_date, 'yyyy-MM') 年度一级天数最多的月份,
        SUM (DECODE (t.grade, 'Ⅰ', 1, 0)) 一级天数
           FROM city_day t
          WHERE TO_CHAR (t.oper_date, 'yyyy') = '2006'
       GROUP BY TO_CHAR (t.oper_date, 'yyyy-MM'), t.city
       )tt
       )ttt
          where rn =1    )a    
      group by CITY ,一级天数   
       )tttt
       on tt.city=tttt.city
      left join(
      select CITY ,WMSYS.WM_CONCAT(历年I级天数最多的月份) 历年I级天数最多的月份,
      一级天数 
      from  
        (
          select
          ttt.city,
          ttt.历年I级天数最多的月份,
          ttt.一级天数
          from
            (select tt.*,
                dense_rank () OVER (PARTITION BY tt.city ORDER BY 一级天数 DESC) rn
            from
            (SELECT t.city, TO_CHAR (t.oper_date, 'yyyy-MM') 历年I级天数最多的月份,
                                 SUM (DECODE (t.grade, 'Ⅰ', 1, 0)) 一级天数
                            FROM city_day t
                        GROUP BY TO_CHAR (t.oper_date, 'yyyy-MM'), t.city
                        )tt 
                        )ttt
            where ttt.rn =1   
            and ttt.一级天数!=0)b
         group by CITY ,一级天数     
      )ttttt  on tt.city=ttttt.city