DB2数据库必须掌握的常用语句(一)

数据库
DB2数据库是IBM出口的一系列关系型数据库管理系统,分别在不同的操作系统平台上服务。本文将介绍DB2数据库必须掌握的常用语句。

DB2数据库常用的语句:

1、查找员工的编号、姓名、部门和出生日期,如果出生日期为空值,显示日期不详,并按部门排序输出,日期格式为yyyy-mm-dd

select emp_no,emp_name,dept,isnull(convert(char(10),birthday,120),'日期不详') birthday

from employee

order by dept

2、查找与喻自强在同一个单位的员工姓名、性别、部门和职称

select emp_no,emp_name,dept,title

from employee

where emp_name<>'喻自强' and dept in

(select dept from employee

where emp_name='喻自强')

3、按部门进行汇总,统计每个部门的总工资

select dept,sum(salary)

from employee

group by dept

 

4、查找商品名称为14寸显示器商品的销售情况,显示该商品的编号、销售数量、单价和金额

select a.prod_id,qty,unit_price,unit_price*qty totprice

from sale_item a,product b

where a.prod_id=b.prod_id and prod_name='14寸显示器'

5、在销售明细表中按产品编号进行汇总,统计每种产品的销售数量和金额

select prod_id,sum(qty) totqty,sum(qty*unit_price) totprice

from sale_item

group by prod_id

6、使用convert函数按客户编号统计每个客户1996年的订单总金额

select cust_id,sum(tot_amt) totprice

from sales

where convert(char(4),order_date,120)='1996'

group by cust_id

7、查找有销售记录的客户编号、名称和订单总额

select a.cust_id,cust_name,sum(tot_amt) totprice

from customer a,sales b

where a.cust_id=b.cust_id

group by a.cust_id,cust_name

8、查找在1997年中有销售记录的客户编号、名称和订单总额

select a.cust_id,cust_name,sum(tot_amt) totprice

from customer a,sales b

where a.cust_id=b.cust_id and convert(char(4),order_date,120)='1997'

group by a.cust_id,cust_name

9、查找一次销售***的销售记录

select order_no,cust_id,sale_id,tot_amt

from sales

where tot_amt=

(select max(tot_amt)

from sales)

10、查找至少有3次销售的业务员名单和销售日期

select emp_name,order_date

from employee a,sales b

where emp_no=sale_id and a.emp_no in

(select sale_id

from sales

group by sale_id

having count(*)>=3)

order by emp_name

这就是我要为大家介绍的DB2数据库必须掌握的常用语句十则,另外还有很多常用语句这里还没有介绍到,在以后的文章中会为大家介绍更多的知识,希望大家继续关注。

【编辑推荐】

  1. 通过JDBC连接DB2数据库技巧
  2. 讲解IBM DB2数据库的常用日期操作函数
  3. DB2数据库的安装
责任编辑:迎迎 来源: 天极网
相关推荐

2011-03-16 10:59:34

DB2数据库常用语句

2011-03-16 10:19:49

DB2数据库常用语句

2011-03-16 10:12:14

DB2数据库常用语句

2011-03-16 10:39:11

DB2数据库常用语句

2010-11-04 12:00:59

db2存储过程

2010-08-10 09:07:51

DB2数据库优化

2010-07-29 09:44:35

DB2数据库优化

2010-08-09 13:08:45

DB2数据库

2010-09-06 10:00:00

DB2数据库

2010-04-13 15:24:25

Oracle维护常用语

2010-08-25 15:13:22

DB2Oracle数据库

2011-03-11 16:02:03

DB2数据库安装

2010-07-27 08:48:52

DB2数据库优化

2011-08-31 16:33:00

DB2

2010-10-08 10:18:26

MySQL自增字段

2010-06-01 16:02:00

MySQL 常用语句

2010-11-03 16:32:10

DB2创建数据库

2010-08-31 17:34:46

DB2

2010-11-01 13:45:16

DB2数据库的优势

2010-08-20 12:56:52

IBM DB2数据库
点赞
收藏

51CTO技术栈公众号