本帖最后由 hkp7r 于 2010-06-10 16:50:26 编辑

解决方案 »

  1.   

    with temp as(
    select '北京' city,'cd' product,2000 year,200 sales from dual
    union all
    select '北京' city,'dvd' product,2001 year,300 sales from dual
    union all
    select '北京' city,'tv' product,2001 year,300 sales from dual
    union all
    select '上海' city,'dvd' product,1999 year,300 sales from dual
    union all
    select '上海' city,'tv' product,1999 year,100 sales from dual
    union all
    select '上海' city,'cd' product,1999 year,100 sales from dual
    union all
    select '上海' city,'dvd' product,2000 year,200 sales from dual
    )
    select city,product,sales from (
    select city,product,year,sales,'a' sorts from temp
    union all
    select city,null product,null year,sum(sales) sales,'b' sorts from temp group by rollup(city)
    ) order by city,sorts
      

  2.   

    with temp as(
    select '北京' city,'cd' product,2000 year,200 sales from dual
    union all
    select '北京' city,'dvd' product,2001 year,300 sales from dual
    union all
    select '北京' city,'tv' product,2001 year,300 sales from dual
    union all
    select '上海' city,'dvd' product,1999 year,300 sales from dual
    union all
    select '上海' city,'tv' product,1999 year,100 sales from dual
    union all
    select '上海' city,'cd' product,1999 year,100 sales from dual
    union all
    select '上海' city,'dvd' product,2000 year,200 sales from dual
    )
    select city,product,sales from (
    select city,product,year,sales,'a' sorts from temp
    union all
    select city,null product,null year,sum(sales) sales,'b' sorts from temp group by rollup(city)
    ) where city is not null order by city,sorts
      

  3.   


    这样是可以可使,年就不会再显示了,我用下面也可以实现这样的效果,可使年也是不显示了
    select city,product,sum(sales)  from tmp1  group by city, rollup(product) order by city
      

  4.   

    不好意思,年未加上
    select city,product,year,sales from (
    select city,product,year,sales,'a' sorts from temp
    union all
    select city,null product,null year,sum(sales) sales,'b' sorts from temp group by rollup(city)
    ) where city is not null order by city,sorts
      

  5.   

    with temp as(
    select '北京' city,'cd' product,2000 year,200 sales from dual
    union all
    select '北京' city,'dvd' product,2001 year,300 sales from dual
    union all
    select '北京' city,'tv' product,2001 year,300 sales from dual
    union all
    select '上海' city,'dvd' product,1999 year,300 sales from dual
    union all
    select '上海' city,'tv' product,1999 year,100 sales from dual
    union all
    select '上海' city,'cd' product,1999 year,100 sales from dual
    union all
    select '上海' city,'dvd' product,2000 year,200 sales from dual
    )
    select city,product,year,sum(sales)
    from temp
    group by  city,rollup ((product,year))
      

  6.   


    请问最后((product,year))这个怎么理解?
      

  7.   

    第一层个括号表示ROLLUP对象,第二层个括号表示product,year这两个同级,如果没有第二层就会对product进行汇总,结果如下
    CITY PRODUCT YEAR SUM(SALES)
    上海 cd 1999 100
    上海 cd NULL 100
    上海 tv 1999 100
    上海 tv NULL 100
    上海 dvd 2000 200
    上海 dvd 1999 300
    上海 dvd NULL 500
    上海 NULL NULL 700
    北京 cd 2000 200
    北京 cd NUL 200
    北京 tv 2001 300
    北京 tv NULL 300
    北京 dvd 2001 300
    北京 dvd NULL 300
    北京 NULL NULL 800