SQL語句練習(xí)實(shí)例之四 找出促銷活動(dòng)中銷售額最高的職員
---找出促銷活動(dòng)中銷售額最高的職員
---你剛在一家服裝銷售公司中找到了一份工作,此時(shí)經(jīng)理要求你根據(jù)數(shù)據(jù)庫中的兩張表得到促銷活動(dòng)銷售額最高的銷售員
---1.一張是促銷活動(dòng)表
---2.一張是銷售客列表
create table Promotions
(
activity nvarchar(30),
sdate datetime,
edate datetime
)
insert Promotions
select '五一促銷活動(dòng)','2011-5-1','2011-5-7'
union
select '十一促銷活動(dòng)','2011-10-1','2011-10-7'
union
select 'OA專場活動(dòng)','2011-6-1','2011-6-7'
go
create table sales
(
id int not null,
name nvarchar(20),
saledate datetime,
price money
)
go
insert sales
select 1,'王五','2011-5-1',1000 union
select 1,'王五','2011-5-2',2000 union
select 1,'王五','2011-5-3',3000 union
select 1,'王五','2011-5-4',4000 union
select 1,'張三','2011-5-1',1000 union
select 1,'張三','2011-5-3',2000 union
select 1,'張三','2011-5-4',4000 union
select 1,'李四','2011-5-6',1000 union
select 1,'趙六','2011-5-5',1000 union
select 1,'錢七','2011-5-8',1000 union
select 1,'孫五','2011-6-1',1000 union
select 1,'孫五','2011-6-2',2000 union
select 1,'王五','2011-6-3',3000 union
select 1,'孫五','2011-6-4',4000 union
select 1,'張三','2011-6-1',11000 union
select 1,'張三','2011-6-3',20000 union
select 1,'張三','2011-6-4',4000 union
select 1,'李四','2011-6-6',1000 union
select 1,'趙六','2011-6-5',1000 union
select 1,'錢七','2011-6-8',1500 union
select 1,'孫五','2011-10-1',11000 union
select 1,'孫五','2011-10-2',12000 union
select 1,'王五','2011-10-3',9000 union
select 1,'孫五','2011-10-4',4000 union
select 1,'張三','2011-10-1',11000 union
select 1,'張三','2011-10-3',2000 union
select 1,'張三','2011-10-4',4000 union
select 1,'李四','2011-10-6',27000 union
select 1,'趙六','2011-10-5',9000 union
select 1,'錢七','2011-10-8',3000
go
-----我們需要找出在每次的促銷活動(dòng)中,其銷售總額大于 等于
---所有其他職員銷售額的職員及促銷事件。
---說明:謂詞a2.name<>a.name將其他職員從子查詢合計(jì)中排除出去
---------謂詞Between 中的子查詢確保我們使用了正確的促銷日期
--方法一:
select a.name,b.activity,SUM(a.price) as totalprice
from sales a ,Promotions as b
where a.saledate between b.sdate and b.edate
group by a.name,b.activity
having SUM(price)>= all(select SUM(price) from sales a2
where a2.name<>a.name and a2.saledate between
(
select sdate from Promotions as b2 where b2.activity=b.activity
)
and (select edate from Promotions b3
where b3.activity=b.activity)
group by a2.name)
-----------------
---方法二:
---說明: 如果促銷活動(dòng)時(shí)間是不重疊的,則promotions表中只有一個(gè)主鍵列,這樣在group by
--子句中使用(activity,sdate,edate)將不會(huì)改變。但是它將使having子句可以使用sdate和edate
select a.name,b.activity,SUM(a.price) as totalprice
from sales a ,Promotions as b
where a.saledate between b.sdate and b.edate
group by b.activity,b.sdate,b.edate,a.name
having SUM(price)>= all(select SUM(price) from sales a2
where a2.name<>a.name and a2.saledate between
b.sdate
and b.edate
group by a2.name)
go
--方法三:
---使用cte(sql 2005以后的版本)
with clearksTotal(name,activity,totalprice) as
(
select a.name,b.activity,SUM(price)
from sales a ,Promotions b
where a.saledate between b.sdate and b.edate
group by a.name,b.activity
)
select c1.name,c1.activity,c1.totalprice
from clearksTotal c1
where totalprice=(select MAX(c2.totalprice) from clearksTotal c2
where c1.activity=c2.activity)
go
drop table Promotions
go
drop table sales
相關(guān)文章
Activiti-Explorer使用sql server數(shù)據(jù)庫實(shí)現(xiàn)方法
本文主要介紹Activiti-Explorer使用sql server數(shù)據(jù)庫,這里整理了詳細(xì)的資料來說明Activiti-Explorer使用SQL Server的實(shí)例,有興趣的小伙伴可以參考下2016-08-08Sql Server 視圖數(shù)據(jù)的增刪改查教程
這篇文章主要介紹了Sql Server 視圖數(shù)據(jù)的增刪改查教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01MSSQL存儲(chǔ)過程學(xué)習(xí)筆記一 關(guān)于存儲(chǔ)過程
在寫筆記之前,首先需要整理好這些概念性的東西,否則的話,就會(huì)在概念上產(chǎn)生陌生或者是混淆的感覺。2011-05-05獲取SQL Server數(shù)據(jù)庫元數(shù)據(jù)的幾種方法
這篇文章主要介紹了獲取SQL Server數(shù)據(jù)庫元數(shù)據(jù)的幾種方法 ,需要的朋友可以參考下2015-08-08修改SQL-SERVER數(shù)據(jù)庫表結(jié)構(gòu)的SQL命令附sql命令行修改數(shù)據(jù)庫
本教程給大家介紹修改SQL-SERVER數(shù)據(jù)庫表結(jié)構(gòu)的SQL命令附sql命令行修改數(shù)據(jù)庫,涉及到sqlserver數(shù)據(jù)庫命令的相關(guān)知識(shí),對(duì)sqlserver數(shù)據(jù)庫命令感興趣的朋友可以參考下本篇文章2015-10-10mssql數(shù)據(jù)庫系統(tǒng)崩潰后的一般處理步驟與方法
mssql數(shù)據(jù)庫系統(tǒng)崩潰后的一般處理步驟與方法...2007-03-03生成300個(gè)不同的隨機(jī)數(shù)的SQL語句
最近在做一個(gè)項(xiàng)目,涉及到要生成N個(gè)8位的不重復(fù)的純數(shù)字隨機(jī)數(shù)作為優(yōu)惠碼,研究了一番,把結(jié)果分享給大家2014-08-08Sqlserver 存儲(chǔ)過程中結(jié)合事務(wù)的代碼
Sqlserver 存儲(chǔ)過程中使用事務(wù)2010-06-06