今天做兩個複雜的子查詢練習,答案僅供參考。

1、求最大銷售數量(100)有那幾個月份(全部數據)

1.1、求銷售數量的最大值

select max(銷售數量) as 銷售數量 from [sheet5$]

1.2、提取最大銷售數量的月份

select 月份 from [sheet5$]

where 銷售數量=

(select max(銷售數量) as 銷售數量 from [sheet5$])

1.3、根據月份提取月份所有數據

select a.* from

[sheet5$] a,

(select 月份 from [sheet5$] where 銷售數量=(select max(銷售數量) as 銷售數量 from [sheet5$])) b

where a.月份=b.月份

2、獲取數據集的衆數

衆數是指一組數字中出現最頻繁的數字。例如{4,5,6,6,6,7,8,9,2}的衆數是6.Excel中有函數Mode返回衆數,但是SQL中沒有這個函數。

對個數字進行計數,並降序排列

select 衆數,count(*) as 計數 from [sheet7$] group by 衆數 order by count(*) desc

如果只有一個衆數或者只取第一個衆數,則用top 1提取數據。

select top 1 衆數

from

(select 衆數,count(*) as 計數 from [sheet7$] group by 衆數 order by count(*) desc)

通用的做法是提取計數的最大值,然後再提取衆數號碼。

select 衆數 from

(select 衆數,count(*) as 計數 from [sheet7$] group by 衆數)

where 計數=

(

select max(計數) from

(select 衆數,count(*) as 計數 from [sheet7$] group by 衆數)

)

更改數據源,讓號碼8也有3個。

然後刷新表格

查看原文 >>
相關文章