SQL語句練習例項之八 對於銷售員提成收入的計算

2021-09-08 02:16:23 字數 2333 閱讀 9325

----對於銷售員提成收入的計算

create table emplyees

(empid int not null,

empname nvarchar(20) not null

)insert emplyees

select 1,'張三'

union

select 2,'李四'

union

select 3 ,'王五'

gocreate table bills

(empid int not null,

billdate datetime not null,

billrate decimal(5,2)

)insert bills

select 1,'2009-1-1',25 union

select 2,'2010-1-1',15 union

select 3,'2011-1-1',20 union

select 1,'2011-1-1',30 union

select 2,'2011-1-1',35

gocreate table hoursworked

(jobid int not null,

empid int not null,

workdate datetime not null,

billhours decimal(5,2)

)insert hoursworked

select 4,1,'2011-2-1',3 union

select 4,1,'2011-3-1',5 union

select 4,2,'2011-4-1',2 union

select 4,1,'2011-5-1',4

go---需要乙個查詢,用來實現顯示某個**工作的**員的姓名和總提成。總提成按每個**員分別計算

----方法是工作小時數乘以適用的每小時收費標準。

---方法

一、使用檢視

create view hourraterpt

as select h1.empid,empname,workdate,billhours,

(select billrate from bills as s1

where billdate=(select max(billdate) from bills as s2

where s2.billdate<=h1.workdate and s1.empid=s2.empid

and s1.empid=h1.empid)) as billrate

from hoursworked h1,emplyees e1

where e1.empid=h1.empid

goselect empid,empname,sum(billhours*billrate) as billtotsal

from hourraterpt

group by empid,empname

---方法二,使用一條sql語句實現

---go

--select e1.empid,e1.empname,sum(billhours*

--(select billrate from bills  as b1

-- where billdate=(select max(billdate) from bills as b2

-- where b2.billdate<=h1.workdate and b1.empid=b2.empid and b1.empid=h1.empid)))

--from hoursworked h1,emplyees e1

--where h1.empid=e1.empid

--group by e1.empid,e1.empname

goselect e1.empid,e1.empname,sum(billhours*b1.billrate)

from hoursworked h1,emplyees e1 ,bills as b1

where e1.empid=b1.empid and e1.empid=h1.empid and

billdate=(select max(billdate) from bills b2 where b2.empid=e1.empid and b2.billdate<=h1.workdate)

and h1.workdate>=b1.billdate

group by e1.empid,e1.empname

godrop table emplyees;

drop table hoursworked;

drop table bills;

drop view hourraterpt

SQL語句練習

建立一張表,記錄 呼叫員的工作流水,記錄呼叫員編號,對方號碼,通話開始時間,結束時間。建表,插資料等都自己寫出sql 要求 輸出所有資料中通話時間最長的5條記錄。輸出所有資料中撥打長途號碼 對方號碼以0開頭 的總時長 輸出本月通話時長最多的前三個呼叫員的編號 輸出本月撥打 次數最多的前三個呼叫員的編...

SQL 語句練習

mysql select from persons limit 5 oracle select from persons where rownum 5 select from persons where name like l select from persons where name not l...

SQL語句練習

1 把兩張 的資料抽取出來放到另外一張 中 1 pt表 role id int pt int 2 season score表 role id int season score int 3 player表 role id int pt int season score int count int 4 ...