最近,通過你的努力,你獲得了一份工做,成爲了百貨公司的一位經理。
到位後,你發現你的銷售數據庫中有兩張表,一個是商店促銷時間的日曆,另外一個是在促銷期間的銷售額列表。
你須要編寫一個查詢,告訴咱們在每次促銷中哪位職員的銷售額最高,這樣能夠給那個職員發績效獎金。數據庫
-- 商店促銷時間的日曆 create table promotions ( promo_name varchar2(50) not null primary key, -- 促銷活動名稱 start_date date not null, -- 開始時間 end_date date not null, -- 終止時間 check(start_date<=end_date) ); -- 促銷期間的銷售額表 (注意:該表只是保存促銷期間的銷售額) create table sales ( ticket_nbr int not null primary key, --銷售票據編號 (自增) clerk_name varchar2(20) not null, --銷售員姓名 sale_date date not null, --銷售日期 sale_amount number(9,2) not null --銷售金額 ); insert into promotions values('spring sales',to_date('2009-2-1','yyyy/mm/dd'),to_date('2009-2-15','yyyy/mm/dd')); insert into promotions values('worker sale',to_date('2009-5-1','yyyy/mm/dd'),to_date('2009-5-4','yyyy/mm/dd')); insert into promotions values('children sale',to_date('2009-6-1','yyyy/mm/dd'),to_date('2009-6-1','yyyy/mm/dd')); insert into promotions values('national day sale',to_date('2009-10-1','yyyy/mm/dd'),to_date('2009-10-7','yyyy/mm/dd')); create sequence seq_nbr; insert into sales values(seq_nbr.nextval,'david',to_date('2009-2-1','yyyy/mm/dd'),30); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-2-1','yyyy/mm/dd'),73); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-2-1','yyyy/mm/dd'),110); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-2-2','yyyy/mm/dd'),190); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-2-2','yyyy/mm/dd'),30); insert into sales values(seq_nbr.nextval,'david',to_date('2009-2-2','yyyy/mm/dd'),92); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-2-3','yyyy/mm/dd'),130); insert into sales values(seq_nbr.nextval,'david',to_date('2009-2-3','yyyy/mm/dd'),90); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-2-3','yyyy/mm/dd'),110); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-2-4','yyyy/mm/dd'),70); insert into sales values(seq_nbr.nextval,'david',to_date('2009-2-4','yyyy/mm/dd'),9); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-2-5','yyyy/mm/dd'),88); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-2-5','yyyy/mm/dd'),70); insert into sales values(seq_nbr.nextval,'david',to_date('2009-2-13','yyyy/mm/dd'),50); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-2-13','yyyy/mm/dd'),170); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-2-14','yyyy/mm/dd'),270); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-2-15','yyyy/mm/dd'),67.5); insert into sales values(seq_nbr.nextval,'david',to_date('2009-5-1','yyyy/mm/dd'),280.5); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-5-1','yyyy/mm/dd'),190); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-5-1','yyyy/mm/dd'),113); insert into sales values(seq_nbr.nextval,'david',to_date('2009-5-2','yyyy/mm/dd'),88); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-5-2','yyyy/mm/dd'),35.5); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-5-2','yyyy/mm/dd'),125); insert into sales values(seq_nbr.nextval,'david', to_date('2009-5-3','yyyy/mm/dd'),92); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-5-3','yyyy/mm/dd'),93); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-5-3','yyyy/mm/dd'),167); insert into sales values(seq_nbr.nextval,'david', to_date('2009-5-4','yyyy/mm/dd'),123.5); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-5-4','yyyy/mm/dd'),200); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-5-4','yyyy/mm/dd'),2); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-6-1','yyyy/mm/dd'),190); insert into sales values(seq_nbr.nextval,'david',to_date('2009-6-1','yyyy/mm/dd'),110.5); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-6-1','yyyy/mm/dd'),213); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-10-1','yyyy/mm/dd'),1123); insert into sales values(seq_nbr.nextval,'david',to_date('2009-10-1','yyyy/mm/dd'),780); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-10-1','yyyy/mm/dd'),310); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-10-2','yyyy/mm/dd'),139); insert into sales values(seq_nbr.nextval,'david',to_date('2009-10-2','yyyy/mm/dd'),1110.5); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-10-2','yyyy/mm/dd'),998); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-10-3','yyyy/mm/dd'),120); insert into sales values(seq_nbr.nextval,'mary',to_date('2009-10-4','yyyy/mm/dd'),10); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-10-4','yyyy/mm/dd'),234); insert into sales values(seq_nbr.nextval,'david',to_date('2009-10-5','yyyy/mm/dd'),110.5); insert into sales values(seq_nbr.nextval,'tom',to_date('2009-10-6','yyyy/mm/dd'),23); insert into sales values(seq_nbr.nextval,'david',to_date('2009-10-7','yyyy/mm/dd'),10.5); insert into sales values(seq_nbr.nextval,'王海',to_date('2009-2-1','yyyy/mm/dd'),30); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-2-1','yyyy/mm/dd'),73); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-2-1','yyyy/mm/dd'),110); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-2-2','yyyy/mm/dd'),190); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-2-2','yyyy/mm/dd'),30); insert into sales values(seq_nbr.nextval,'王海',to_date('2009-2-2','yyyy/mm/dd'),92); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-2-3','yyyy/mm/dd'),130); insert into sales values(seq_nbr.nextval,'王海',to_date('2009-2-3','yyyy/mm/dd'),90); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-2-3','yyyy/mm/dd'),110); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-2-4','yyyy/mm/dd'),70); insert into sales values(seq_nbr.nextval,'王海',to_date('2009-2-4','yyyy/mm/dd'),9); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-2-5','yyyy/mm/dd'),88); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-2-5','yyyy/mm/dd'),70); insert into sales values(seq_nbr.nextval,'王海',to_date('2009-2-13','yyyy/mm/dd'),50); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-2-13','yyyy/mm/dd'),170); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-2-14','yyyy/mm/dd'),270); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-2-15','yyyy/mm/dd'),67.5); insert into sales values(seq_nbr.nextval,'王海',to_date('2009-5-1','yyyy/mm/dd'),280.5); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-5-1','yyyy/mm/dd'),190); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-5-1','yyyy/mm/dd'),113); insert into sales values(seq_nbr.nextval,'王海',to_date('2009-5-2','yyyy/mm/dd'),88); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-5-2','yyyy/mm/dd'),35.5); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-5-2','yyyy/mm/dd'),125); insert into sales values(seq_nbr.nextval,'王海', to_date('2009-5-3','yyyy/mm/dd'),92); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-5-3','yyyy/mm/dd'),93); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-5-3','yyyy/mm/dd'),167); insert into sales values(seq_nbr.nextval,'王海', to_date('2009-5-4','yyyy/mm/dd'),123.5); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-5-4','yyyy/mm/dd'),200); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-5-4','yyyy/mm/dd'),2); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-6-1','yyyy/mm/dd'),190); insert into sales values(seq_nbr.nextval,'王海',to_date('2009-6-1','yyyy/mm/dd'),110.5); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-6-1','yyyy/mm/dd'),213); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-10-1','yyyy/mm/dd'),1123); insert into sales values(seq_nbr.nextval,'王海',to_date('2009-10-1','yyyy/mm/dd'),780); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-10-1','yyyy/mm/dd'),310); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-10-2','yyyy/mm/dd'),139); insert into sales values(seq_nbr.nextval,'王海',to_date('2009-10-2','yyyy/mm/dd'),1110.5); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-10-2','yyyy/mm/dd'),998); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-10-3','yyyy/mm/dd'),120); insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-10-4','yyyy/mm/dd'),10); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-10-4','yyyy/mm/dd'),234); insert into sales values(seq_nbr.nextval,'王海',to_date('2009-10-5','yyyy/mm/dd'),110.5); insert into sales values(seq_nbr.nextval,'劉萬理',to_date('2009-10-6','yyyy/mm/dd'),23); insert into sales values(seq_nbr.nextval,'王海',to_date('2009-10-7','yyyy/mm/dd'),10.5);
找出在各次促銷活動中,銷售量最高的銷售員。code
請編制一條SQL來完成這個查詢。(儘可能考慮多種寫法)blog
select p.promo_name,s.clerk_name,sum(s.sale_amount) top_sale from promotions p inner join sales s on s.sale_date between p.start_date and p.end_date group by p.promo_name,s.clerk_name,p.start_date,p.end_date having sum(s.sale_amount) >= all( select sum(s2.sale_amount) from sales s2 where s.clerk_name <> s2.clerk_name and s2.sale_date between p.start_date and p.end_date group by s2.clerk_name ); SELECT s1.clerk_name,p.promo_name,p.start_date,p.end_date,SUM(s1.sale_amount) FROM sales s1 INNER JOIN promotions p ON s1.sale_date BETWEEN p.start_date AND p.end_date group by s1.clerk_name,p.promo_name,p.start_date,p.end_date HAVING SUM(s1.sale_amount)>= ALL ( SELECT SUM(s2.sale_amount) FROM sales s2 WHERE s2.sale_date BETWEEN p.start_date AND p.end_date GROUP BY s2.clerk_name );
財務諮詢顧問的收入統計問題ci
金太陽財務諮詢服務公司,聘請了一些財務顧問提供財務操做諮詢服務,這些顧問都以兼職形式爲公司服務,公司將按小時爲顧問工做計算薪酬。it
具體表信息以下:io
a. 顧問信息表 (consultant)table
create table consultant( cst_id int primary key, cst_name varchar2(30) not null ); insert into consultant values(1, 'david'); insert into consultant values(2, 'henry'); insert into consultant values(3, 'mary'); insert into consultant values(4, 'kent');
b. 顧問費率表 (billings)class
不一樣級別的顧問,在不一樣的時期,每小時的收費是不一樣的,公司會根據狀況上調或者下調顧問的小時佣金待遇。date
create table billings( cst_id int not null, bill_date date not null, bill_rate int not null, constraint PK_BILLING PRIMARY KEY(cst_id,bill_date), constraint FK_BILLING_EMP FOREIGN KEY (cst_id) references consultant(cst_id) ); insert into billings values(1, to_date('2010-1-1' ,'yyyy/mm/dd'),50); insert into billings values(2, to_date('2010-1-1' ,'yyyy/mm/dd'),60); insert into billings values(3, to_date('2010-1-1' ,'yyyy/mm/dd'),70); insert into billings values(4, to_date('2010-1-1' ,'yyyy/mm/dd'),40); insert into billings values(1, to_date('2011-1-1' ,'yyyy/mm/dd'),60); insert into billings values(4, to_date('2011-1-1' ,'yyyy/mm/dd'),45);
c. 顧問工做狀況記錄表 (hoursworked)
create table hoursworked( list_id int primary key, cst_id int not null, work_date date not null, bill_hrs decimal(5,2) not null, constraint FK_HW_EMP FOREIGN KEY (cst_id) references consultant(cst_id) ); insert into hoursworked values(1,1, to_date('2010-7-1','yyyy/mm/dd'),3); insert into hoursworked values(2,1, to_date('2010-8-1','yyyy/mm/dd'),5); insert into hoursworked values(3,2, to_date('2010-7-1','yyyy/mm/dd'),2); insert into hoursworked values(4,1, to_date('2011-7-1','yyyy/mm/dd'),4); insert into hoursworked values(5,3, to_date('2011-8-1','yyyy/mm/dd'),3.5); insert into hoursworked values(6,4, to_date('2010-9-1','yyyy/mm/dd'),10); insert into hoursworked values(7,4, to_date('2011-8-1','yyyy/mm/dd'),6);
求解:
咱們須要你編寫一個SQL查詢語句,顯示顧問的名字以及其總的顧問費用。
select c.cst_id,e.cst_name,sum(d.bill_rate*c.bill_hrs) total_fee from ( select h.cst_id,h.work_date,max(b.bill_date) bill_date, h.bill_hrs from billings b inner join hoursworked h on h.cst_id=b.cst_id and h.work_date>=b.bill_date group by h.cst_id,h.work_date,h.bill_hrs )c,billings d,consultant e where c.cst_id=d.cst_id and c.bill_date=d.bill_date and c.cst_id=e.cst_id group by c.cst_id,e.cst_name order by c.cst_id;