国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

leetcode數(shù)據(jù)庫題第六彈

這篇具有很好參考價值的文章主要介紹了leetcode數(shù)據(jù)庫題第六彈。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

626. 換座位

https://leetcode.cn/problems/exchange-seats/

嗯,今天又看了看這個題目,發(fā)現(xiàn)用例已經(jīng)修復(fù)了。

# mysql && oracle
select floor((id - 1) / 2) * 2 + row_number() over(partition by floor((id - 1) / 2) order by id desc) id ,student
from seat
# mssql
select (id - 1) / 2 * 2 + row_number() over(partition by (id - 1) / 2 order by id desc) id ,student
from seat
# mssql && mysql
# 這個是抄評論里的內(nèi)容,可惜不直到怎么調(diào)整的能夠支持 oracle
select rank() over (order by (id - 1) ^ 1) id, student
from Seat

1280. 學(xué)生們參加各科測試的次數(shù)

https://leetcode.cn/problems/students-and-examinations/

額。。。統(tǒng)計所有人,所有科目,各進行了幾次考核。。。那么人員和科目只能最大化交叉查詢了,然后再去關(guān)聯(lián)考試次數(shù)。

其實指令是一拖三的,把空值處理換成各自的函數(shù)即可:

oracle : nvl
mysql : ifnull
mssql : isnull

# oracle
select a.*,nvl(cnt,0) attended_exams 
from (
    select * from students,subjects
) a
left join (
    select student_id,subject_name,count(0) cnt
    from examinations
    group by student_id,subject_name
) b on a.student_id=b.student_id and a.subject_name=b.subject_name
order by a.student_id,a.subject_name

CSDN 文盲老顧的博客,https://blog.csdn.net/superwfei

1321. 餐館營業(yè)額變化增長

https://leetcode.cn/problems/restaurant-growth/

額。。。。根據(jù)日期進行關(guān)聯(lián),還得限定輸出的日期范圍,難度不高,就是寫起來挺麻煩,這就已經(jīng)接近日常工作的樣子了。

主要的是 mysql 和 mssql 的 datediff 的用法,差異不小哦,需要仔細查詢各自的語法和差計算方式,哪個日期在前,哪個日子在后。

# mysql
select a.visited_on,sum(c.amount) amount,round(sum(c.amount) / 7,2) average_amount
from (
    select distinct visited_on 
    from customer c
    where exists(
        select 1 
        from customer
        where datediff(c.visited_on,visited_on)=6
    )
) a
inner join customer c on datediff(a.visited_on,c.visited_on)<=6 and datediff(a.visited_on,c.visited_on) >= 0
group by a.visited_on
order by a.visited_on
# mssql
select a.visited_on,sum(c.amount) amount,convert(decimal(16,2),sum(c.amount) * 1.0 / 7) average_amount
from (
    select distinct visited_on 
    from customer c
    where exists(
        select 1 
        from customer
        where datediff(d,visited_on,c.visited_on)=6
    )
) a
inner join customer c on datediff(d,c.visited_on,a.visited_on)<=6 and datediff(d,c.visited_on,a.visited_on) >= 0
group by a.visited_on
order by a.visited_on
# oracle
select to_char(a.visited_on,'YYYY-mm-DD') visited_on,sum(c.amount) amount,round(sum(c.amount) * 1.0 / 7,2) average_amount
from (
    select distinct visited_on 
    from customer c
    where exists(
        select 1 
        from customer
        where c.visited_on - visited_on = 6
    )
) a
inner join customer c on c.visited_on between a.visited_on - 6 and a.visited_on
group by a.visited_on
order by a.visited_on

1327. 列出指定時間段內(nèi)所有的下單產(chǎn)品

https://leetcode.cn/problems/list-the-products-ordered-in-a-period/

嗯,日期范圍,可以用 between 來描述,這樣就可以一拖三了。至于不少于100的銷售量,用having聚合篩選一下就好。

select p.product_name,sum(o.unit) unit 
from products p
inner join orders o on p.product_id=o.product_id
where o.order_date between '2020-2-1' and '2020-2-29'
group by p.product_name
having(sum(o.unit)>=100)

1341. 電影評分

https://leetcode.cn/problems/movie-rating/

額。完全兩個不同的查詢條件,聚合方式,然后得到兩個結(jié)果集,放到一起輸出。。。這個考的內(nèi)容,不具有通用性。。。。算了,隨便寫寫吧。

嗯,下邊的內(nèi)容,并不是某數(shù)據(jù)只能使用這一種方式,而是,在不同的數(shù)據(jù)里,用不同的方式進行實現(xiàn)了,小伙伴們也可以用任意數(shù)據(jù)庫按照特定方式去實現(xiàn)查詢結(jié)果。

# mysql
select * 
from (
    select name results
    from movierating m
    inner join users u on m.user_id=u.user_id
    group by name
    order by count(0) desc,name
    limit 0,1
) a
union all
select * 
from (
    select title results
    from movierating r
    inner join movies m on r.movie_id=m.movie_id
    where created_at between '2020-2-1' and '2020-2-29'
    group by title
    order by avg(rating) desc,title
    limit 0,1
) b
# oracle
select * 
from (
    select u.name results
    from movierating m
    inner join users u on m.user_id=u.user_id
    group by name
    order by count(0) desc,name
) a
where rownum=1
union all
select * 
from (
    select m.title results
    from movierating r
    inner join movies m on r.movie_id=m.movie_id
    where r.created_at between '2020-2-1' and '2020-2-29'
    group by title
    order by avg(r.rating) desc,title
) b
where rownum=1
# 一拖三,除了 mssql, * 1.0 都可以省略
select a.name results 
from (
    select a.*,row_number() over(partition by tp order by cnt desc,name) nid 
    from (
        select u.name,sum(1) over(partition by u.user_id order by r.created_at) cnt,'1' tp
        from users u
        inner join MovieRating r on u.user_id=r.user_id
        union all
        select m.title,avg(rating * 1.0),'2' tp 
        from Movies m
        inner join MovieRating r on m.movie_id=r.movie_id
        where created_at >= '2020-2-1' and created_at < '2020-3-1'
        group by title
    ) a
) a
where a.nid=1

1378. 使用唯一標(biāo)識碼替換員工ID

https://leetcode.cn/problems/replace-employee-id-with-the-unique-identifier/

啊哦,又考回到 left join 了,完全沒什么其他想法啊。

select u.unique_id,e.name 
from Employees e
left join EmployeeUNI u on e.id=u.id

1393. 股票的資本損益

https://leetcode.cn/problems/capital-gainloss/

嗯?這個題目居然被定為中等難度?沒看出來啊,不還是 group 和 sum 的問題嗎?哦,難道一個 case when 就算一個難度了?

# 一拖三
select a.stock_name,sum(p) capital_gain_loss 
from (
    select s.*,(case when operation = 'Buy' then -price else price end) p
    from stocks s
) a
group by a.stock_name

1407. 排名靠前的旅行者

https://leetcode.cn/problems/top-travellers/submissions/

額。。。還是 left join 一個聚合結(jié)果。。。

# oracle
select a.name,nvl(b.d,0) as travelled_distance 
from users a 
left join (
    select user_id,sum(distance) d
    from rides
    group by user_id
) b on a.id=b.user_id 
order by nvl(b.d,0) desc,a.name
# mssql
select name,isnull(d,0) travelled_distance
from users a
left join (
    select user_id,sum(distance) d
    from rides
    group by user_id
) b on a.id=b.user_id
order by d desc,name
# mysql
select name,ifnull(sum(distance),0) travelled_distance
from users a
left join rides r on r.user_id=a.id
group by a.id
order by travelled_distance desc,name

1484. 按日期分組銷售產(chǎn)品

https://leetcode.cn/problems/group-sold-products-by-the-date/

哦,終于用到 group_concat 或 for xml 了,可惜老顧對 oracle 不熟悉,不知道怎么在 oracle 里搞出這個結(jié)果

# mysql
select sell_date,count(distinct product) num_sold,group_concat(distinct product order by product) products
from Activities
group by sell_date
order by sell_date
# mssql
select distinct sell_date,len(products) - len(replace(products,',','')) + 1 num_sold,products 
from Activities a
cross apply (
    select stuff((
        select distinct ',' + product
        from Activities 
        where sell_date=a.sell_date
        order by ',' + product
        for xml path('')
    ),1,1,'') products
) b
order by sell_date

1517. 查找擁有有效郵箱的用戶

https://leetcode.cn/problems/find-users-with-valid-e-mails/

額。。。。這個題目,有官方正則支持的,很容易,而 mssql 因為沒有官方正則支持。。。只能驗字符串了。。。。還好 tsql 的 like 也有類似的正則用法,雖然并不是完整的正則。

# mysql
select * 
from Users
where mail regexp '^[a-zA-Z][a-zA-Z0-9_\.-]*@leetcode[\.]com$'
# oracle
select * 
from Users
where regexp_like(mail,'^[a-zA-Z][a-zA-Z0-9_\.-]*@leetcode[\.]com$')
# mssql
select *
from users 
where mail like '[a-zA-Z]%@leetcode.com'
and substring(mail,0,len(mail) - 12) not like '%[^a-zA-Z0-9_.-]%'

1527. 患某種疾病的患者

https://leetcode.cn/problems/patients-with-a-condition/

嗯嗯。這個題目,還是正則比 like 好用,尤其是 like 需要 or 的時候,效率太低了。

# mssql
select * 
from patients
where ',' + replace(conditions,' ',',') like '%,diab1%'
# mysql
select * 
from Patients
where conditions regexp '(?<![a-z0-9A-Z])DIAB1'
# oracle
select * 
from Patients
where regexp_like(conditions,'(^| )DIAB1')

小結(jié)

額,這次10個題目,算是考的比較全面了,尤其是分組字符串合并,是一個很常用的內(nèi)容,然后,就是正則這種匹配,也算是很常用的內(nèi)容了。

但是,在沒有正則的情況下,對于確定的字符串內(nèi)容,其實我們還是有變動的辦法的,嗯,這次有兩個題目就是用 like 進行變通得到正確結(jié)果的。

所以說,一定要了解清楚,到底每個指令,都有哪些功能,能進行怎樣的操作。

所有華麗的招式,都是有基礎(chǔ)的動作構(gòu)成的,只有了解了所有的基礎(chǔ)內(nèi)容,才能無招勝有招的完成所有需求。

leetcode數(shù)據(jù)庫題第六彈文章來源地址http://www.zghlxwxcb.cn/news/detail-497960.html

到了這里,關(guān)于leetcode數(shù)據(jù)庫題第六彈的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 圖書館自習(xí)室|基于SSM的圖書館自習(xí)室座位預(yù)約小程序設(shè)計與實現(xiàn)(源碼+數(shù)據(jù)庫+文檔)

    圖書館自習(xí)室|基于SSM的圖書館自習(xí)室座位預(yù)約小程序設(shè)計與實現(xiàn)(源碼+數(shù)據(jù)庫+文檔)

    圖書館自習(xí)室 目錄 基于SSM的圖書館自習(xí)室座位預(yù)約小程序設(shè)計與實現(xiàn) 一、前言 二、系統(tǒng)設(shè)計 三、系統(tǒng)功能設(shè)計? 1、小程序端: 2、后臺 四、數(shù)據(jù)庫設(shè)計 ?五、核心代碼? 六、論文參考 七、最新計算機畢設(shè)選題推薦 八、源碼獲取: 博主介紹 :??大廠碼農(nóng)|畢設(shè)布道師,

    2024年04月12日
    瀏覽(27)
  • 最經(jīng)典的解析LSA數(shù)據(jù)庫(第六課)

    最經(jīng)典的解析LSA數(shù)據(jù)庫(第六課)

    初步認識OSPF的大致內(nèi)容(第三課)_IHOPEDREAM的博客-CSDN博客 建立領(lǐng)居表 同步數(shù)據(jù)庫?? 今天來 說一說數(shù)據(jù)庫概念 計算路由表 數(shù)據(jù)庫是一個組織化的數(shù)據(jù)集合,用于存儲、管理和檢索數(shù)據(jù)。它是一個可訪問的集合,旨在存儲與特定主題或目的相關(guān)的數(shù)據(jù),并提供有效的檢索和使

    2024年02月09日
    瀏覽(21)
  • 數(shù)據(jù)庫管理-第六十九期 另一種累(20230422)

    Oracle 23c的相關(guān)內(nèi)容先緩緩,有些數(shù)據(jù)庫管理相關(guān)的還是得正式版發(fā)布后才好去做實驗。這周相較于之前那種割接較多的累還有點不一樣,這周陪著客戶交流了大把國產(chǎn)數(shù)據(jù)庫廠商,每次1-2個小時,需要全神貫注,協(xié)助客戶提問。 為啥要溝通的背景就不說了,這周以線上和線

    2023年04月23日
    瀏覽(25)
  • JAVA畢業(yè)設(shè)計132—基于Java+Springboot+Vue的自習(xí)室座位預(yù)約小程序管理系統(tǒng)(源代碼+數(shù)據(jù)庫)

    JAVA畢業(yè)設(shè)計132—基于Java+Springboot+Vue的自習(xí)室座位預(yù)約小程序管理系統(tǒng)(源代碼+數(shù)據(jù)庫)

    畢設(shè)所有選題: https://blog.csdn.net/2303_76227485/article/details/131104075 本項目前后端分離帶小程序,分為管理員、用戶兩種角色 1、用戶: 注冊、登錄、自習(xí)室介紹、推薦圖書、校園資訊、座位查詢、座位預(yù)約、預(yù)約查看、個人信息 2、管理員: 用戶管理、場地管理、座位管理、預(yù)

    2024年04月13日
    瀏覽(25)
  • 數(shù)據(jù)庫原理第六章課后題答案(第四版)

    數(shù)據(jù)庫原理第六章課后題答案(第四版)

    一、選擇題 1. B??? 2. C??? 3. C??? 4. A??? 5. C 6. B??? 7. C??? 8. B??? 9. D??? 10. C 11. D?? 12. B?? 13. B?? 14. D?? 15. B 16. B ??17. C 二、填空題 數(shù)據(jù)庫的結(jié)構(gòu)設(shè)計、數(shù)據(jù)庫的行為設(shè)計 新奧爾良法 分析和設(shè)計階段、實現(xiàn)和運行階段 需求分析 概念結(jié)構(gòu)設(shè)計 自頂向下、自底向

    2024年02月01日
    瀏覽(21)
  • 數(shù)據(jù)庫系統(tǒng)概述——第六章 關(guān)系數(shù)據(jù)理論(知識點復(fù)習(xí)+練習(xí)題)

    數(shù)據(jù)庫系統(tǒng)概述——第六章 關(guān)系數(shù)據(jù)理論(知識點復(fù)習(xí)+練習(xí)題)

    ?? 博主: 命運之光 ?? 專欄: 離散數(shù)學(xué)考前復(fù)習(xí)(知識點+題) ?? 專欄: 概率論期末速成(一套卷) ?? 專欄: 數(shù)字電路考前復(fù)習(xí) ?? 專欄: 數(shù)據(jù)庫系統(tǒng)概述 ?? 博主的其他文章: 點擊進入博主的主頁????? 前言: 身為大學(xué)生考前復(fù)習(xí)一定十分痛苦,你有沒有過

    2024年02月09日
    瀏覽(25)
  • 山東專升本計算機第六章-數(shù)據(jù)庫技術(shù)

    山東專升本計算機第六章-數(shù)據(jù)庫技術(shù)

    數(shù)據(jù)庫技術(shù) SQL數(shù)據(jù)庫與NOSQL數(shù)據(jù)庫的區(qū)別 數(shù)據(jù)庫管理系統(tǒng) 考點 6 數(shù)據(jù)庫管理系統(tǒng)的組成和功能 組成 ? 模式翻譯 ? 應(yīng)用程序的翻譯 ? 交互式查詢 ? 數(shù)據(jù)的組織和存取 ? 事務(wù)運行管理 ? 數(shù)據(jù)庫的維護 功能 ? 數(shù)據(jù)定義功能 ? 數(shù)據(jù)存取功能 ? 數(shù)據(jù)庫運行管理能力 ? 數(shù)

    2024年02月05日
    瀏覽(30)
  • MySQl數(shù)據(jù)庫第六課-------SQl命令的延續(xù)------快來看看

    MySQl數(shù)據(jù)庫第六課-------SQl命令的延續(xù)------快來看看

    ?歡迎小可愛們前來借鑒我的gtiee秦老大大 (qin-laoda) - Gitee.com ———————————————————————————————— SQl語句 ???????? 數(shù)據(jù)庫操作 ???????? 數(shù)據(jù)表操作 SQL增刪 ———————————————————————————— 1.主鍵唯一

    2024年02月16日
    瀏覽(17)
  • c++之旅——第六彈

    c++之旅——第六彈

    大家好啊,這里是c++之旅第六彈,跟隨我的步伐來開始這一篇的學(xué)習(xí)吧! 如果有知識性錯誤,歡迎各位指正??!一起加油!! 創(chuàng)作不易,希望大家多多支持哦! 一,靜態(tài)成員: 1.靜態(tài)成員是什么: 靜態(tài)成員可以在類的所有對象之間共享數(shù)據(jù),也可以提供不依賴于類的對象的

    2024年03月18日
    瀏覽(19)
  • C++第六彈---類與對象(三)

    C++第六彈---類與對象(三)

    ? 個人主頁: ? 熬夜學(xué)編程的小林 ?? 系列專欄: ? 【C語言詳解】 ? 【數(shù)據(jù)結(jié)構(gòu)詳解】 【C++詳解】 目錄 1、類的6個默認成員函數(shù) 2、構(gòu)造函數(shù) 2.1、概念 2.2、特性 3、析構(gòu)函數(shù) 3.1、概念 3.2、特性 3.3、調(diào)用順序 總結(jié) 如果一個 類中什么成員都沒有 ,簡稱為 空類 。 空類中

    2024年03月22日
    瀏覽(11)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包