SQL語(yǔ)句練習(xí)實(shí)例之四 找出促銷(xiāo)活動(dòng)中銷(xiāo)售額最高的職員
---找出促銷(xiāo)活動(dòng)中銷(xiāo)售額最高的職員
---你剛在一家服裝銷(xiāo)售公司中找到了一份工作,此時(shí)經(jīng)理要求你根據(jù)數(shù)據(jù)庫(kù)中的兩張表得到促銷(xiāo)活動(dòng)銷(xiāo)售額最高的銷(xiāo)售員
---1.一張是促銷(xiāo)活動(dòng)表
---2.一張是銷(xiāo)售客列表
create table Promotions
(
activity nvarchar(30),
sdate datetime,
edate datetime
)
insert Promotions
select '五一促銷(xiāo)活動(dòng)','2011-5-1','2011-5-7'
union
select '十一促銷(xiāo)活動(dòng)','2011-10-1','2011-10-7'
union
select 'OA專(zhuān)場(chǎng)活動(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,'錢(qián)七','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,'錢(qián)七','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,'錢(qián)七','2011-10-8',3000
go
-----我們需要找出在每次的促銷(xiāo)活動(dòng)中,其銷(xiāo)售總額大于 等于
---所有其他職員銷(xiāo)售額的職員及促銷(xiāo)事件。
---說(shuō)明:謂詞a2.name<>a.name將其他職員從子查詢(xún)合計(jì)中排除出去
---------謂詞Between 中的子查詢(xún)確保我們使用了正確的促銷(xiāo)日期
--方法一:
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)
-----------------
---方法二:
---說(shuō)明: 如果促銷(xiāo)活動(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)文章
SQL Server ltrim(rtrim()) 去不掉空格的原因分析
這篇文章主要介紹了SQL Server ltrim(rtrim()) 去不掉空格的原因分析,原因主要是因?yàn)橹虚g存在回車(chē)符或者換行符,所以要先將此符號(hào)替換掉,具體示例代碼大家參考下本文2017-08-08
SQLServer 觸發(fā)器 數(shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)備份
首先,你需要建立測(cè)試數(shù)據(jù)表,一個(gè)用于插入數(shù)據(jù):test3,另外一個(gè)作為備份:test3_bak2009-07-07
Zabbix監(jiān)控SQL Server服務(wù)狀態(tài)的方法詳解
這篇文章主要給大家介紹了關(guān)于Zabbix監(jiān)控SQL Server服務(wù)狀態(tài)的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
sql server中通過(guò)查詢(xún)分析器實(shí)現(xiàn)數(shù)據(jù)庫(kù)的備份與恢復(fù)方法分享
sql server中通過(guò)查詢(xún)分析器實(shí)現(xiàn)數(shù)據(jù)庫(kù)的備份與恢復(fù)方法分享,需要備份或還原數(shù)據(jù)庫(kù)的朋友可以參考下2012-05-05
Sql學(xué)習(xí)第二天——SQL DML與CTE概述
關(guān)于DML即數(shù)據(jù)操縱語(yǔ)言;關(guān)于CTE即公用表表達(dá)式,接下來(lái)為大家介紹下CTE使用的地方以及優(yōu)點(diǎn)同時(shí)提供一個(gè)簡(jiǎn)單的CTE例題,感興趣的各位可以參考下哈2013-03-03
SqlServer中查看當(dāng)前Open的Cursor方法
這篇文章主要介紹了SqlServer中查看當(dāng)前Open的Cursor方法,本文為了解決A cursor with thename ' ' already exists總結(jié)的幾個(gè)方法,需要的朋友可以參考下2015-07-07
SQL Server日志過(guò)大會(huì)影響查詢(xún)結(jié)果
一臺(tái)老Web服務(wù)器上,使用的是ASP+SQL Server 2000的網(wǎng)站,五六年了,使用起來(lái)一直很順暢,也就沒(méi)有怎么去理它。2009-05-05
sql IDENTITY_INSERT對(duì)標(biāo)識(shí)列的作用和使用
本文主要介紹了sql IDENTITY_INSERT對(duì)標(biāo)識(shí)列的作用和使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
SQL中varchar和nvarchar的基本介紹及其區(qū)別
varchar長(zhǎng)度為 n 個(gè)字節(jié)的可變長(zhǎng)度且非 Unicode 的字符數(shù)據(jù),nvarchar包含 n 個(gè)字符的可變長(zhǎng)度 Unicode 字符數(shù)據(jù)2014-07-07

