SQL一些總結(jié)

sql語(yǔ)句的執(zhí)行順序

1.from

2.on

3.join

4.where

5.group by

6.having?

7.select

8.distinct

9.order? by

10.limit


7。select? ? ?8。 distinct?

1。from

3。join

2。on

4。where

5。group by

6。having

9。order by

10。limit

一。通解任意連續(xù)N問(wèn)題,leetcode180:構(gòu)造輔助列

select distinct Num as ConsecutiveNums?

from (select * ,row_number()over(partition by Num order by Id) as rownum,

? ? ? ? ? ? ? ? ? ? ? ?row_number()over(order by Id) as id2

? ? ? ? ? from logs) as t

group by (id2-rownum),Num

having count(*) >= n;

二。通解求連續(xù)值問(wèn)題,leetcode1285:用rank或row_number找到連續(xù)區(qū)間開(kāi)始和結(jié)束的數(shù)? ? ? ? ? ?字

select min(log_id) as start_id,max(log_id) as end_id

from (select distinct log_id, row_number()over(order by log_id) as rn,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?log_id - row_number()over(order by log_id) as reference

? ? ? ? ? from Logs ) as t

group by reference

order by start_id;

三,移動(dòng)求和,移動(dòng)平均

https://michael.blog.csdn.net/article/details/107729626?leetcode579

sum(Salary) over(partition by Id order by Month rows 2 preceding)

rows 數(shù)字 preceding 取之前的行

rows 數(shù)字 following? 取之后的行

rows between 數(shù)字 preceding and 數(shù)字 following 取之前n行和之后n行

截至之前的“數(shù)字”行,也就是最靠近的“數(shù)字+1”行,包括當(dāng)前行

除去最近一個(gè)月,剩下每個(gè)月的近3個(gè)月,這時(shí)應(yīng)該 (2 preceding)

四,行列轉(zhuǎn)換。http://www.itdecent.cn/p/1c6fb0df9f58

? ? ? ?行轉(zhuǎn)列:用 sum case when,group by。科目字段中的語(yǔ)文,數(shù)學(xué),英語(yǔ)就被單獨(dú)拎出? ? ? ? ? ? ? ? ? ? ? ?來(lái)成了三個(gè)列,三個(gè)字段,稱(chēng)為行轉(zhuǎn)列。也可以用pivot函數(shù)。

? ? ? ? ? ? ? ? ? ? ? select * from student? ?pivot(sum(score) for subject in (語(yǔ)文,數(shù)學(xué),英語(yǔ))。? ? ? ? ? ? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ? ? ? pivot后面跟一個(gè)聚合函數(shù)來(lái)拿結(jié)果,for后面跟的科目是要轉(zhuǎn)的列,in后面就是? ? ? ? ? ? ? ? ? ? ? ? 具體科目值。

? ? ? ? ? ? ? ? ? ? ? ?select name,sum(case when subject = ''語(yǔ)文" then score else 0 end) "語(yǔ)文"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sum(case when subject = "數(shù)學(xué)" then score else 0 end) "數(shù)學(xué)"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?sum(case when subject = "英語(yǔ)" then score else 0 end) "英語(yǔ)"

? ? ? ? ? ? ? ? ? ? ? ?from student

? ? ? ? ? ? ? ? ? ? ? ?group by name

? ? ? ?列轉(zhuǎn)行:用union all。union all 和union 構(gòu)造新字段,連接行。union去重。unpivot

? ? ? ? ? ? ? ? ? ? ? select * from student unpivot(score for subject in (語(yǔ)文,數(shù)學(xué),英語(yǔ))

? ? ? ? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ? ? ?select name,"語(yǔ)文"as subject ,sum(語(yǔ)文) score from student group by name

? ? ? ? ? ? ? ? ? ? ?union all

? ? ? ? ? ? ? ? ? ? ?select name,"數(shù)學(xué)" as subject,sum(數(shù)學(xué)) score from student group by name

? ? ? ? ? ? ? ? ? ? ?union all

? ? ? ? ? ? ? ? ? ? ?select name,"英語(yǔ)" as subject,sum(英語(yǔ)) score from student group by name

? ? ? ? ? ? ? ? ? ? ? 是要把語(yǔ)文,數(shù)學(xué),英語(yǔ)這三個(gè)列,三個(gè)字段合并成一個(gè)字段,一個(gè)列,一個(gè)? ? ? ? ? ? ? ? ? ? ? ? 字段下的內(nèi)容,所以稱(chēng)為列轉(zhuǎn)行。

五,all select 求世界上人口最多的國(guó)家

? ? ? ? select name from world where population >= all (select population from world where? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? population >0)

六,窗口函數(shù)

? ? ?分布函數(shù) : cume_list? ?https://www.begtut.com/mysql/mysql-cume_dist-function.html??

? ? ? ? ? ? ? ? ? ? ? percent_rank?https://www.begtut.com/mysql/mysql-percent_rank-function.html

? ? ?頭尾函數(shù):first_value?https://www.begtut.com/mysql/mysql-first_value-function.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)置即旋轉(zhuǎn)數(shù)據(jù)表的橫縱方向,常用來(lái)改變數(shù)據(jù)布局,以便用新的角度觀察。有些轉(zhuǎn)置算法比較簡(jiǎn)單,比如行轉(zhuǎn)列、列轉(zhuǎn)行、雙向...
    心宇gxy閱讀 738評(píng)論 0 1
  • 版本:V1.0 第0題表結(jié)構(gòu):uid,subject_id,score求:找出所有科目成績(jī)都大于某一學(xué)科平均成績(jī)的...
    故里良田閱讀 3,173評(píng)論 0 0
  • 第一章 數(shù)據(jù)庫(kù)和SQL 1、什么是數(shù)據(jù)庫(kù) ?? 將大量數(shù)據(jù)保存起來(lái),通過(guò)計(jì)算機(jī)加工而成的可以進(jìn)行高效訪(fǎng)問(wèn)的數(shù)據(jù)集合...
    東北小阿衰閱讀 875評(píng)論 0 2
  • 前言 --下文為個(gè)人在學(xué)習(xí)和工作中的簡(jiǎn)單總結(jié),供個(gè)人參考用,如有問(wèn)題和建議歡迎指正和指導(dǎo)! 正文索引 一、sql優(yōu)...
    精華_Wjinghua閱讀 467評(píng)論 0 1
  • 3.1 視圖 我們先來(lái)看一個(gè)查詢(xún)語(yǔ)句(僅做示例,未提供相關(guān)數(shù)據(jù)) SELECTstu_nameFROMview_s...
    忘原_b2d5閱讀 498評(píng)論 0 0

友情鏈接更多精彩內(nèi)容