`
jonakang
  • 浏览: 4065 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
最近访客 更多访客>>
社区版块
存档分类
最新评论

oracle9i单表分组问题,有点搞人!

阅读更多
Database: Oracle9iU2
有这么个表:employee
id,name,birthday,duty,degree,title,degree_title
报表格式如图:
  • 大小: 73.8 KB
分享到:
评论
15 楼 lumi 2008-03-10  
感觉没什么难的,我做的报表比你的复杂多了!

用4次行转列,然后再进行连接操作,一个sql搞定!
14 楼 javaTo 2008-03-10  
为什么非要在查询语句中费那么大劲呢!你这种sql,如果并发量稍大一点,造成死锁的几率就会很高。
给你说说我们的方案,一句简单的sql把需要的数据查出来,在程序中予以分类或计算,然后写入报表,如果对并发有要求,那么就来他几个线程
13 楼 gstripe 2008-03-08  
多累啊, 存储过程+临时表写下不就好了,非要搞这么负责的强劲SQL
12 楼 seele 2008-03-06  
多个单条SQL的效率 和 单条长SQL的效率 哪一个好?(得出相同的结果)
不可能没有区别的吧..

望有人解释一下...
11 楼 jonakang 2008-03-05  
把它拆成N个简单的sql,问题基本上解决了-_-!
10 楼 jonakang 2008-03-05  
有个查询结果,但是希望能把age1,age2,age3,age4合并为一个column,该如何做呢?
查询结果如图
9 楼 jonakang 2008-03-04  
employee表:
id,name,duty,birthday,degree,title,degree_title

其中duty,degree,title,degree_title是被设计在BasicData表中的,如下2个父子表:basic_data_master(id,code,name),
basic_data_detail(id,master_id,col_two)
basic_data_detail表中的col_two对应的就是duty,degree,title,degree_title的值

我到现在还没有找到一个比较简洁的语句来搞定这个问题。总之,我写出来的语句像是一个得了肥胖症的要死不活的家伙,超级郁闷ing !
8 楼 jonakang 2008-03-04  
主要是在年龄,职称,学历上有个多值判断,这个该怎么弄啊,case when出来的不是我想要的效果。decode的用法不熟,尝试了几把以失败告终,郁闷!
7 楼 LucasLee 2008-03-04  
看了你的SQL,太夸张了点吧,这么多SQL,不应该用一条Group by的SQL搞么?
另外,对这种报表,你可以看看琴棋报表
6 楼 jonakang 2008-03-04  
如果把类型,职称,学位放到另外的表中的话就是多表的交叉表查询了,除了采用临时表的解决方案以外还有别的出路吗?
5 楼 jonakang 2008-03-04  
不知道用什么方法能够使得最后的查询结果按照如图所示显示
4 楼 jonakang 2008-03-04  
修改了下
with  
  a as (select count(t1.id) as lessthan35 from jdpj_waf_employee t1 where months_between(sysdate, t1.birthday)/12 <= 35 and t1.duty = '管理人员'),--35> x的   
  b as (select count(t1.id) as morethan36 from jdpj_waf_employee t1 where months_between(sysdate, t1.birthday)/12 between 36 and 45 and t1.duty = '管理人员'),--36< x <45的   
  c as (select count(t1.id) as morethan46 from jdpj_waf_employee t1 where months_between(sysdate, t1.birthday)/12 between 46 and 55 and t1.duty = '管理人员'),--46< x <55的   
  d as (select count(t1.id) as morethan56 from jdpj_waf_employee t1 where months_between(sysdate, t1.birthday)/12 >= 56 and t1.duty = '管理人员'),--56< x的   
  e as (select count(t1.id) as doctor from jdpj_waf_employee t1 where t1.degree = '博士' and t1.duty = '管理人员'),--博士研究生   
  f as (select count(t1.id) as mastor from jdpj_waf_employee t1 where t1.degree = '硕士' and t1.duty = '管理人员'),--硕士研究生   
  g as (select count(t1.id) as bachelor from jdpj_waf_employee t1 where t1.degree = '本科' and t1.duty = '管理人员'),--本科   
  h as (select count(t1.id) as grad from jdpj_waf_employee t1 where t1.degree = '专科' and t1.duty = '管理人员'),--专科   
  i as (select count(t1.id) as vocational from jdpj_waf_employee t1 where t1.degree = '中等职业教育' and t1.duty = '管理人员'),--中等职业教育   
  j as (select count(t1.id) as hight from jdpj_waf_employee t1 where t1.degree = '高中' and t1.duty = '管理人员'),--高中   
  k as (select count(t1.id) as middle from jdpj_waf_employee t1 where t1.degree = '初中及以下' and t1.duty = '管理人员'),--初中及以下   
  l as (select count(t1.id) as senior from jdpj_waf_employee t1 where t1.degree_title = '高级' and t1.duty = '管理人员'),--高级   
  m as (select count(t1.id) as secondary from jdpj_waf_employee t1 where t1.degree_title = '中级' and t1.duty = '管理人员'),--中级   
  n as (select count(t1.id) as junior from jdpj_waf_employee t1 where t1.degree_title = '初级' and t1.duty = '管理人员'),--初级   
  o as (select count(t1.id) as seniortech from jdpj_waf_employee t1 where t1.degree_title = '高级技师' and t1.duty = '管理人员'),--高级技师   
  p as (select count(t1.id) as tech from jdpj_waf_employee t1 where t1.degree_title = '技师' and t1.duty = '管理人员'),--技师   
  q as (select count(t1.id) as worker1 from jdpj_waf_employee t1 where t1.degree_title = '高级工' and t1.duty = '管理人员'),--高级工   
  r as (select count(t1.id) as worker2 from jdpj_waf_employee t1 where t1.degree_title = '中级工' and t1.duty = '管理人员'),--中级工   
  s as (select count(t1.id) as worker3 from jdpj_waf_employee t1 where t1.degree_title = '初级及以下' and t1.duty = '管理人员')--初级及以下   
  select a.lessthan35, b.morethan36, c.morethan46, d.morethan56, 
  e.doctor, f.mastor, g.bachelor, h.grad, i.vocational, j.hight, k.middle, 
  l.senior, m.secondary, n.junior, 
  o.seniortech, p.tech, 
  q.worker1, r.worker2, s.worker3   
  from a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s   
3 楼 jonakang 2008-03-04  
这个是最早用with语句的解决方法,后来觉得语句臃肿想换个间练点的
with
  a as (select t1.duty, count(t1.id) as cnt from jdpj_waf_employee t1 where months_between(sysdate, t1.birthday)/12 <= 35 group by t1.duty),--35> x的
  b as (select count(t1.id) as cnt from jdpj_waf_employee t1 where months_between(sysdate, t1.birthday)/12 between 36 and 45),--36< x <45的
  c as (select count(t1.id) as cnt from jdpj_waf_employee t1 where months_between(sysdate, t1.birthday)/12 between 46 and 55),--46< x <55的
  d as (select count(t1.id) as cnt from jdpj_waf_employee t1 where months_between(sysdate, t1.birthday)/12 >= 56),--56< x的
  e as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree = '博士'),--博士研究生
  f as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree = '硕士'),--硕士研究生
  g as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree = '本科'),--本科
  h as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree = '专科'),--专科
  i as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree = '中等职业教育'),--中等职业教育
  j as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree = '高中'),--高中
  k as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree = '初中及以下'),--初中及以下
  l as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree_title = '高级'),--高级
  m as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree_title = '中级'),--中级
  n as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree_title = '初级'),--初级
  o as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree_title = '高级技师'),--高级技师
  p as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree_title = '技师'),--技师
  q as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree_title = '高级工'),--高级工
  r as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree_title = '中级工'),--中级工
  s as (select count(t1.id) as cnt from jdpj_waf_employee t1 where t1.degree_title = '初级及以下')--初级及以下
  select a.cnt, b.cnt, c.cnt, d.cnt, e.cnt, f.cnt, g.cnt, h.cnt, i.cnt, j.cnt, k.cnt, l.cnt, m.cnt, m.cnt, o.cnt, p.cnt, q.cnt, r.cnt, s.cnt
  from a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s
  where a.duty='管理人员'
2 楼 jonakang 2008-03-04  
先前有这么个做法,但是如何把结果摆成横的呢?
--年龄结构
select count(t.id_card_no) as num1, t.duty
from jdpj_waf_employee t
where months_between(sysdate, t.birthday)/12 < 35
and t.duty is not null
group by t.duty

union

select count(t.id_card_no) as num2, t.duty
from jdpj_waf_employee t
where months_between(sysdate, t.birthday)/12 between 36 and 45
and t.duty is not null
group by t.duty

union

select count(t.id_card_no) as num3, t.duty
from jdpj_waf_employee t
where months_between(sysdate, t.birthday)/12 between 46 and 55
and t.duty is not null
group by t.duty

union

select count(t.id_card_no) as num4, t.duty
from jdpj_waf_employee t
where months_between(sysdate, t.birthday)/12 > 56
and t.duty is not null
group by t.duty

union 
--文化结构

select count(t.id_card_no) as num6, t.duty
from jdpj_waf_employee t
where t.degree = '博士'
and t.duty is not null
group by t.duty

union

select count(t.id_card_no) as num7, t.duty
from jdpj_waf_employee t
where t.degree = '硕士'
and t.duty is not null
group by t.duty

union

select count(t.id_card_no) as num8, t.duty
from jdpj_waf_employee t
where t.degree = '本科'
and t.duty is not null
group by t.duty

union

select count(t.id_card_no) as num9, t.duty
from jdpj_waf_employee t
where t.degree = '专科'
and t.duty is not null
group by t.duty

union

select count(t.id_card_no) as num10, t.duty
from jdpj_waf_employee t
where t.degree = '中等职业教育'
and t.duty is not null
group by t.duty

union

select count(t.id_card_no) as num11, t.duty
from jdpj_waf_employee t
where t.degree = '高中'
and t.duty is not null
group by t.duty

union

select count(t.id_card_no) as num12, t.duty
from jdpj_waf_employee t
where t.degree = '初中及以下'
and t.duty is not null
group by t.duty

union

--职称结构
select count(t.id_card_no) as num13, t.duty
from jdpj_waf_employee t
where t.degree_title = '高级'
and t.duty is not null
group by t.duty

union

select count(t.id_card_no) as num14, t.duty
from jdpj_waf_employee t
where t.degree_title = '中级'
and t.duty is not null
group by t.duty

union

select count(t.id_card_no) as num15, t.duty
from jdpj_waf_employee t
where t.degree_title = '初级'
and t.duty is not null
group by t.duty

union

--技术等级结构
select count(t.id_card_no) as num16, t.duty
from jdpj_waf_employee t
where t.title = '高级技师'
and t.duty is not null
group by t.duty

union

select count(t.id_card_no) as num17, t.duty
from jdpj_waf_employee t
where t.title = '技师'
and t.duty is not null
group by t.duty

union

select count(t.id_card_no) as num18, t.duty
from jdpj_waf_employee t
where t.title = '高级工'
and t.duty is not null
group by t.duty

union 

select count(t.id_card_no) as num19, t.duty
from jdpj_waf_employee t
where t.title = '中级工'
and t.duty is not null
group by t.duty

union

select count(t.id_card_no) as num20, t.duty
from jdpj_waf_employee t
where t.title = '初级及以下'
and t.duty is not null
group by t.duty
1 楼 jonakang 2008-03-04  
中间橙色的不用管他

相关推荐

    Oracle 9i Client (Oracle 9i 客户端) 简化版 (不安装Oracle客户端,也可以使用PLSQL Developer)

    Oracle 9i Client (Oracle 9i 客户端) 简化版 (不安装Oracle客户端,也可以使用PLSQL Developer 不用安装Oracle客户端也可以使用PLSQL Developer 绿色! 安全! 轻便! 可靠! 1、本软件可作为简单的Oracle9i客户端...

    oracle 9i 全部下载链接

    根据提供的标题、描述、标签及部分内容,我们可以整理出关于Oracle 9i的多个关键知识点,主要包括不同操作系统下的版本、下载链接以及如何获取这些资源的方法。 ### Oracle 9i概述 Oracle 9i 是甲骨文公司(Oracle ...

    Oracle9i客户端精简版

    Oracle9i客户端精简版。 Oracle9i客户端精简版Oracle9i客户端精简版Oracle9i客户端精简版Oracle9i客户端精简版Oracle9i客户端精简版Oracle9i客户端精简版Oracle9i客户端精简版

    oracle9i经典测试用表

    该测试用表为Oracle9i的一个经典测试用表,雇员表;该表可以用来进行对数据库的增删查改的联系操作;请在MySQL中使用"SOURCE /路径"的方式进行使用

    oracle9i客户端精简版.zip

    Oracle 9i客户端精简版是一款专为数据库测试和数据检查设计的应用,它提供了一个轻量级的解决方案,便于快速安装和使用。这个版本优化了内存占用,节省了磁盘空间,使得用户能够迅速地进行数据库连接、查看表结构...

    oracle9i310.rar

    Oracle 9i是一款历史悠久但依然具有重要价值的关系型数据库管理系统,尤其对于初学者而言,它提供了深入了解数据库原理和操作的良好平台。"oracle9i310.rar" 是一个压缩包,其中包含了Oracle 9i的简化客户端版本,...

    windows server 2003 Oracle9i 安装

    Windows Server 2003 Oracle9i 安装指南 Windows Server 2003 是微软公司推出的服务器操作系统,而 Oracle9i 是 Oracle 公司推出的关系数据库管理系统。本文档将指导读者在 Windows Server 2003 上安装 Oracle9i ...

    wxh ORACLE9I删表空间问题

    ORACLE9I删表空间问题

    Oracle 9i 参考手册

    "游侠下载站使用说明.txt"可能是对获取和安装Oracle 9i的辅助指南,而"Oracle(CHM)"可能是一个包含Oracle 9i详细技术文档的离线帮助文件,用户可以通过查阅这个文件来学习和解决具体问题。 总的来说,Oracle 9i参考...

    oracle9i全备份导入到11g说明

    当将Oracle 9i的数据全备份导入到Oracle 11g版本时,经常会遇到由于`db_block_size`参数不一致导致的导入失败问题。这种情况下,如果直接进行导入操作,很可能会出现如下的错误提示:“ORA-02236: invalid filename...

    oracle9i教程

    Oracle9i基于SQL语言,其数据库概念包括实体(如表、视图)、数据类型(如数值、字符串、日期等)、索引、约束(如唯一性、非空约束)等。中英文对照的《Conceptes.chm》文件提供了详细的数据库概念解释,便于学习者...

    oracle9i的EXP和IMP

    Oracle 9i数据库系统是Oracle公司的一个重要版本,它提供了许多功能来支持数据管理,其中包括数据导入(IMP)和导出(EXP)工具。这两个工具是数据库管理员进行数据迁移、备份和恢复操作的关键组件。 **EXP(Export...

    oracle9i物理结构,oracle9i物理结构

    除了上述核心组件,Oracle 9i 物理结构还包括其他重要部分,如表空间(Tablespaces)和段(Segments),它们是逻辑结构与物理存储之间的桥梁。表空间由一个或多个数据文件组成,是数据库对象的逻辑容器,而段则代表...

    oracle9i下载地址

    oracle9i下载地址, 分三个IOS文件, 加压即可安装.

    Oracle9i 数据库安装

    Oracle9i 数据库安装是一个复杂的过程,涉及到多个步骤和配置选项。在开始安装之前,确保你的操作系统和硬件环境满足Oracle9i的要求。在这个例子中,操作系统是SuSE Linux 7.2,已经安装了Oracle9i Enterprise ...

    oracle9i客户端精简免安装.rar

    Oracle 9i是一款历史悠久的关系型数据库管理系统,由甲骨文公司(Oracle Corporation)开发,它在2001年发布,提供了许多先进的特性和功能,为企业的数据存储和管理提供了强大的支持。本压缩包“oracle9i客户端精简...

    oracle10g数据导入到oracle9i解决方案

    由于Oracle 9i与Oracle 10g之间存在版本差异,这可能涉及到一些兼容性问题,特别是当导出的数据包含BLOB、CLOB等大数据类型字段时。本文旨在提供一种解决方案来解决这一问题。 #### 问题描述 Oracle 9.2.0.5之前的...

    Oracle 9i初学者指南

    Oracle 9i初学者指南是一本专为对数据库管理感兴趣的初学者设计的教程,它涵盖了Oracle 9i数据库系统的基础知识、安装配置、管理和维护等多个重要方面。Oracle 9i是Oracle公司推出的一个重要版本,引入了许多创新...

    Oracle9i中文版基础培训教程

    本教程“Oracle9i中文版基础培训教程”由赵松涛制作,旨在帮助初学者掌握Oracle 9i的基本操作和概念,对于那些从其他数据库系统(如MySQL)转而学习Oracle的人来说,这是一个宝贵的资源。 Oracle数据库系统的核心...

    Oracle9i+Windows下安装

    百度云盘下载地址https://pan.baidu.com/s/1iU5zzuVbm2-uJtzQGr98zw windows下安装Oracle9i

Global site tag (gtag.js) - Google Analytics