Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1929628
  • 博文数量: 219
  • 博客积分: 8963
  • 博客等级: 中将
  • 技术积分: 2125
  • 用 户 组: 普通用户
  • 注册时间: 2005-10-19 12:48
个人简介

文章分类

全部博文(219)

文章存档

2021年(1)

2020年(3)

2015年(4)

2014年(5)

2012年(7)

2011年(37)

2010年(40)

2009年(22)

2008年(17)

2007年(48)

2006年(31)

2005年(4)

分类: Oracle

2008-08-22 15:32:08

今天写的一个语句如下:
INSERT INTO OAS.DAILY_MIGUSER
   (IMPDATE,bigareaid,PROVINCEID,CITYID,NEWUSER,TOTALUSER,NEWACTIVEUSER,TOTALACTIVEUSER,OFFUSER,TOTALOFFUSER)
select trunc(sysdate),0 bigareaid,b.provinceid, a.cityid,
    sum(case when to_date('2008-08-22', 'yyyy-mm-dd')=effectivedate then 1 else 0 end) newuser,
    count(*) totaluser,
    sum(case when to_date('2008-08-22', 'yyyy-mm-dd')=effectivedate then 1 else 0 end) newactiveuser,
    sum(case when activedate = null then 1 else 0 end) totalactiveuser,
    sum(case when to_date('2008-08-02', 'yyyy-mm-dd')=trunc(expirationdate)  then 1 else 0 end) newoffuser,
    sum(case when expirationdate=null  then 0 else 1 end) totaloffuser
     from miguser_baseinfo a, (select distinct cityid,provinceid from city) b
     where a.cityid=b.cityid
     group by b.provinceid,a.cityid
 
在一个查询语句中记录各种不同情况的出现次数可以用 case when expresion... then ... else ... end. 这种语句肯写是比挨个 select count(*) from ... where expresion... 的方式要方便得多.
阅读(5294) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~