佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > SQL語句練習實例之四 找出促銷活動中銷售額最高的職員

SQL語句練習實例之四 找出促銷活動中銷售額最高的職員

熱門標簽:地方門戶網(wǎng)站 百度競價排名 網(wǎng)站排名優(yōu)化 AI電銷 鐵路電話系統(tǒng) 服務(wù)外包 Linux服務(wù)器 呼叫中心市場需求
復制代碼 代碼如下:

---找出促銷活動中銷售額最高的職員
---你剛在一家服裝銷售公司中找到了一份工作,此時經(jīng)理要求你根據(jù)數(shù)據(jù)庫中的兩張表得到促銷活動銷售額最高的銷售員
---1.一張是促銷活動表
---2.一張是銷售客列表
create table Promotions
(
activity nvarchar(30),
sdate datetime,
edate datetime
)
insert Promotions
select '五一促銷活動','2011-5-1','2011-5-7'
union
select '十一促銷活動','2011-10-1','2011-10-7'
union
select 'OA專場活動','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
-----我們需要找出在每次的促銷活動中,其銷售總額大于 等于
---所有其他職員銷售額的職員及促銷事件。
---說明:謂詞a2.name>a.name將其他職員從子查詢合計中排除出去
---------謂詞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)
-----------------

---方法二:
---說明: 如果促銷活動時間是不重疊的,則promotions表中只有一個主鍵列,這樣在group by
--子句中使用(activity,sdate,edate)將不會改變。但是它將使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

您可能感興趣的文章:
  • SQL語句練習實例之六 人事系統(tǒng)中的缺勤(休假)統(tǒng)計
  • SQL語句練習實例之五 WMS系統(tǒng)中的關(guān)于LIFO或FIFO的問題分析
  • SQL語句練習實例之一——找出最近的兩次晉升日期與工資額
  • SQL語句練習實例之二——找出銷售冠軍
  • SQL語句練習實例之三——平均銷售等待時間
  • SQL語句練習實例之七 剔除不需要的記錄行

標簽:湘潭 湖南 蘭州 黃山 銅川 衡水 崇左 仙桃

巨人網(wǎng)絡(luò)通訊聲明:本文標題《SQL語句練習實例之四 找出促銷活動中銷售額最高的職員》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    大邑县| 盘山县| 勐海县| 安图县| 淮北市| 黎城县| 凌云县| 连云港市| 喀喇| 调兵山市| 桂平市| 岳西县| 灌阳县| 和田县| 尉氏县| 宜丰县| 德保县| 什邡市| 钟山县| 南澳县| 镇远县| 永善县| 三原县| 安宁市| 大冶市| 冀州市| 五家渠市| 从江县| 青冈县| 双峰县| 宁晋县| 上栗县| 那坡县| 新乡市| 宽甸| 诏安县| 江安县| 隆昌县| 大关县| 永康市| 鸡东县|