CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Oracle >  开发

一个有难度的sql

楼主chen_cyh(figer)2002-08-06 17:49:51 在 Oracle / 开发 提问

有3张表,  
  test1        
              id,docdate  
  test2    
              id,rowno,area,type1  
  test3  
              id,value,type2  
   
   
  其中表test1是主表,test2和test3是test1的子表  
   
  现在要创建一个view,  
   
  docdate,sum(area),sum(value)   ;  
   
  要求必须一天一条纪录;  
   
  create   view   vwtest   as   select   docdate,sum(area)   area,sum(value)   value   from   test1   a,test2   b,test3   c   where   a.id=b.id   and   a.id=c.id     and   b.type1='a'   and   c.type2='b'   group   by   docdate;  
   
  可是创建的view不止一天一条纪录,而是很多条;  
   
  用下面的方法创建可以实现一天一条,  
  create   view   vwtest   as   select   docdate,sum(area)   area,sum(value)   value   from     (select   docdate,sum(area)   area,sum(value)   value   from   test1   a,test2   b,test3   c   where   a.id=b.id   and   a.id=c.id     and   b.type1='a'   and   c.type2='b'   group   by   docdate   )   aa   group   by   docdate;  
   
  希望有个效率更高的sql  
  ================================================================  
  ★★   chen_cyh(逐波流)   ●      
  问题点数:20、回复次数:2Top

1 楼KingSunSha(弱水三千)回复于 2002-08-06 23:29:50 得分 20

第一条sql语句和第二条的执行结果应该是一模一样的!  
   
  如果只是第一条语句不合要求,每天多过一条记录的话,我还能猜测是因为docdate中包含了时间字段,同一天中有多个时间不同的值,那样用trunc()函数过滤掉时间值就可以了。如下:  
  select   trunc(docdate)   docdate,sum(area)   area,sum(value)   value    
      from   test1   a,test2   b,test3   c  
    where   a.id=b.id  
        and   a.id=c.id      
        and   b.type1='a'    
        and   c.type2='b'  
    group   by   trunc(docdate);  
   
  但是无论如何想不通为什么同样的情况下第二条可以,奇怪之极!  
  Top

2 楼chen_cyh(figer)回复于 2002-08-21 16:51:19 得分 0

多谢,我找到原因了Top

相关问题

  • 难度!!!!!!!SQL
  • 一个SQL,高难度
  • 超难度sql 语句
  • 超难度sql 语句
  • 超难度SQL语句
  • 超难度sql语句
  • 超难度sql语句
  • 求SQL语句(有难度)???
  • 求高难度SQL语句
  • 求一有难度的SQL

关键词

  • view
  • docdate
  • area
  • sum
  • trunc
  • test
  • group
  • 时间
  • where
  • select

得分解答快速导航

  • 帖主:chen_cyh
  • KingSunSha

相关链接

  • Oracle类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo