⑴ DB2比較常用與實用sql語句總結
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,proct b where a.prod_id=b.prod_id and prod_name=ཊ寸顯示器'
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)=' 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)=' 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
11、用存在量詞查找沒有訂貨記錄的客戶名稱復制代碼 代碼如下:
select cust_name from customer a where not exists (select * from sales b where a.cust_id=b.cust_id)
12、使用左外連接查找每個客戶的客戶編號、名稱、訂貨日期、訂單金額訂貨日期不要顯示時間,日期格式為yyyy-mm-dd按客戶編號排序,同一客戶再按訂單降序排序輸出復制代碼 代碼如下:
select a.cust_id,cust_name,convert(char(10),order_date,120),tot_amt from customer a left outer join sales b on a.cust_id=b.cust_id order by a.cust_id,tot_amt desc
13、查找16M DRAM的銷售情況,要求顯示相應的銷售員的姓名、性別,銷售日期、銷售數量和金額,其中性別用男、女表示復制代碼 代碼如下:
select emp_name 姓名, 性別= case a.sex when 'm' then '男' when 'f' then '女' else '未' end, 銷售日期= isnull(convert(char(10),c.order_date,120),'日期不詳'), qty 數量, qty*unit_price as 金額 from employee a, sales b, sale_item c,proct d where d.prod_name=ཌM DRAM' and d.prod_id=c.prod_id and a.emp_no=b.sale_id and b.order_no=c.order_no
14、查找每個人的銷售記錄,要求顯示銷售員的編號、姓名、性別、產品名稱、數量、單價、金額和銷售日期復制代碼 代碼如下:
select emp_no 編號,emp_name 姓名, 性別= case a.sex when 'm' then '男' when 'f' then '女' else '未' end, prod_name 產品名稱,銷售日期= isnull(convert(char(10),c.order_date,120),'日期不詳'), qty 數量, qty*unit_price as 金額 from employee a left outer join sales b on a.emp_no=b.sale_id , sale_item c,proct d where d.prod_id=c.prod_id and b.order_no=c.order_no
15、查找銷售金額最大的客戶名稱和總貨款復制代碼 代碼如下:
select cust_name,d.cust_sum from customer a, (select cust_id,cust_sum from (select cust_id, sum(tot_amt) as cust_sum from sales group by cust_id ) b where b.cust_sum = ( select max(cust_sum) from (select cust_id, sum(tot_amt) as cust_sum from sales group by cust_id ) c ) ) d where a.cust_id=d.cust_id
16、查找銷售總額少於1000元的銷售員編號、姓名和銷售額復制代碼 代碼如下:
select emp_no,emp_name,d.sale_sum from employee a, (select sale_id,sale_sum from (select sale_id, sum(tot_amt) as sale_sum from sales group by sale_id ) b where b.sale_sum <1000 ) d where a.emp_no=d.sale_id
17、查找至少銷售了3種商品的客戶編號、客戶名稱、商品編號、商品名稱、數量和金額復制代碼 代碼如下:
select a.cust_id,cust_name,b.prod_id,prod_name,d.qty,d.qty*d.unit_price from customer a, proct b, sales c, sale_item d where a.cust_id=c.cust_id and d.prod_id=b.prod_id and c.order_no=d.order_no and a.cust_id in ( select cust_id from (select cust_id,count(distinct prod_id) prodid from (select cust_id,prod_id from sales e,sale_item f where e.order_no=f.order_no) g group by cust_id having count(distinct prod_id)>=3) h )
18、查找至少與世界技術開發公司銷售相同的客戶編號、名稱和商品編號、商品名稱、數量和金額復制代碼 代碼如下:
select a.cust_id,cust_name,d.prod_id,prod_name,qty,qty*unit_price from customer a, proct b, sales c, sale_item d where a.cust_id=c.cust_id and d.prod_id=b.prod_id and c.order_no=d.order_no and not exists (select f.* from customer x ,sales e, sale_item f where cust_name='世界技術開發公司' and x.cust_id=e.cust_id and e.order_no=f.order_no and not exists ( select g.* from sale_item g, sales h where g.prod_id = f.prod_id and g.order_no=h.order_no and h.cust_id=a.cust_id) )
19、查找表中所有姓劉的職工的工號,部門,薪水復制代碼 代碼如下:
select emp_no,emp_name,dept,salary from employee where emp_name like '劉%'
20、查找所有定單金額高於2000的所有客戶編號復制代碼 代碼如下:
select cust_id from sales where tot_amt>2000
21、統計表中員工的薪水在4000-6000之間的人數復制代碼 代碼如下:
select count(*)as 人數 from employee where salary beeen 4000 and 6000
22、查詢表中的同一部門的職工的平均工資,但只查詢"住址"是"上海市"的員工復制代碼 代碼如下:
select avg(salary) avg_sal,dept from employee where addr like '上海市%' group by dept
23、將表中住址為"上海市"的員工住址改為"北京市"復制代碼 代碼如下:
update employee set addr like '北京市' where addr like '上海市'
24、查找業務部或會計部的女員工的基本信息。.winGWiT.Com復制代碼 代碼如下:
select emp_no,emp_name,dept from employee where sex='F'and dept in ('業務','會計')
25、顯示每種產品的銷售金額總和,並依銷售金額由大到小輸出。復制代碼 代碼如下:
select prod_id ,sum(qty*unit_price) from sale_item group by prod_id order by sum(qty*unit_price) desc
26、選取編號界於'C0001'和'C0004'的客戶編號、客戶名稱、客戶地址。復制代碼 代碼如下:
select CUST_ID,cust_name,addr from customer where cust_id beeen 'C0001' AND 'C0004'
27、計算出一共銷售了幾種產品。復制代碼 代碼如下:
select count(distinct prod_id) as '共銷售產品數' from sale_item
28、將業務部員工的薪水上調3%。復制代碼 代碼如下:
update employee set salary=salary*1.03 where dept='業務'
29、由employee表中查找出薪水最低的員工信息。復制代碼 代碼如下:
select * from employee where salary= (select min(salary ) from employee )
30、使用join查詢客戶姓名為"客戶丙"所購貨物的"客戶名稱","定單金額","定貨日期","電話號碼"復制代碼 代碼如下:
select a.cust_id,b.tot_amt,b.order_date,a.tel_no from customer a join sales b on a.cust_id=b.cust_id and cust_name like '客戶丙'
31、由sales表中查找出訂單金額大於"E0013業務員在1996/10/15這天所接每一張訂單的金額"的所有訂單。復制代碼 代碼如下:
select * from sales where tot_amt>all (select tot_amt from sales where sale_id='E0013'and order_date=/10/15') order by tot_amt
32、計算'P0001'產品的平均銷售單價復制代碼 代碼如下:
select avg(unit_price) from sale_item where prod_id='P0001'
33、找出公司女員工所接的定單復制代碼 代碼如下:
select sale_id,tot_amt from sales where sale_id in (select sale_id from employee where sex='F')
34、找出同一天進入公司服務的員工復制代碼 代碼如下:
select a.emp_no,a.emp_name,a.date_hired from employee a join employee b on (a.emp_no!=b.emp_no and a.date_hired=b.date_hired) order by a.date_hired
35、找出目前業績超過232000元的員工編號和姓名。復制代碼 代碼如下:
lishixin/Article/program/DB2/201404/30294
⑵ 關於DB2的日期計算的SQL語句,大神速進
SELECT DATE(LEFT('201202', 4) || '-'||RIGHT('201202', 2)||'-1') - 1 MONTH,
DATE(LEFT('201202', 4) || '-'||RIGHT('201202', 2)||'-1') + 1 MONTH
from sysibm/sysmmy1
DATE(LEFT('201202', 4) || '-'||RIGHT('201202', 2)||'-1') - 1 MONTH這樣拿到的是日期類型,需要cast as char,然後截取就可以了
db2處理日期很垃圾的
⑶ db2 sql 怎麼樣 根據出生日期計算年齡
Transact-SQL 參考
DATEDIFF
返回跨兩個指定日期的日期和時間邊界數。
語法
DATEDIFF ( datepart , startdate , enddate )
參數
datepart
是規定了應在日期的哪一部分計算差額的參數。下表列出了 Microsoft® SQL Server™ 識別的日期部分和縮寫。
日期部分 縮寫
year yy, yyyy
quarter qq, q
Month mm, m
dayofyear dy, y
Day dd, d
Week wk, ww
Hour hh
minute mi, n
second ss, s
millisecond ms
startdate
是計算的開始日期。startdate 是返回 datetime 或 smalldatetime 值或日期格式字元串的表達式。
因為 smalldatetime 只精確到分鍾,所以當用 smalldatetime 值時,秒和毫秒總是 0。
如果您只指定年份的最後兩位數字,則小於或等於 "兩位數年份截止期 "配置選項的值的最後兩位數字的數字所在世紀與截止年所在世紀相同。大於該選項的值的最後兩位數字的數字所在世紀為截止年所在世紀的前一個世紀。例如,如果 two digit year cutoff 為 2049(默認),則 49 被解釋為 2049,2050 被解釋為 1950。為避免模糊,請使用四位數的年份。
有關時間值指定的更多信息,請參見時間格式。有關日期指定的更多信息,請參見 datetime 和 smalldatetime。
enddate
是計算的終止日期。enddate 是返回 datetime 或 smalldatetime 值或日期格式字元串的表達式。
返回類型
integer
注釋
startdate 是從 enddate 減去。如果 startdate 比 enddate 晚,返回負值。
當結果超出整數值范圍,DATEDIFF 產生錯誤。對於毫秒,最大數是 24 天 20 小時 31 分鍾零 23.647 秒。對於秒,最大數是 68 年。
計算跨分鍾、秒和毫秒這些邊界的方法,使得 DATEDIFF 給出的結果在全部數據類型中是一致的。結果是帶正負號的整數值,其等於跨第一個和第二個日期間的 datepart 邊界數。例如,在 1 月 4 日(星期日)和 1 月 11 日(星期日)之間的星期數是 1。
示例
此示例確定在 pubs 資料庫中標題發布日期和當前日期間的天數。
USE pubs
GO
SELECT DATEDIFF(day, pubdate, getdate()) AS no_of_days
FROM titles
GO
select datediff(year,EMP_BIRTHDAY,getdate()) as '年齡' from 表名
⑷ 求助db2如何執行動態SQL 並返回值
求助db2如何執行動態SQL 並返回值
大多數情況下,DB2資料庫管理程序在需要鎖時會隱式地獲取它們,事務不需要顯式地請求鎖,唯一有可能被事務顯式鎖定的資料庫對象就是表,可以使用下邊的語句。\r\nALTER TABLE [TableName] LOCKSIZE TABLE\r\n或\r\nLOCK TABLE [TableName] IN [SHARE|EXCLUSIVE] MODE