幾個經典sql

2021-07-16 01:56:24 字數 1856 閱讀 7351

幾個經典的sql語句

1.關於group by的sql語句

表結構:

year          month        amount

1991      1         1.1      

1991      2         1.2      

1991      3         1.3      

1992      1         2.1      

1992      2         2.2      

1992      3         2.3    

顯示結果:

year          m1               m2             m3  

1991      1.1       1.2       1.3      

1992      2.1       2.2       2.3      

sql的寫法:

select year,

(select amount from test as m where month='1' and test.year=m.year) as m1,

(select amount from test as m where month='2' and test.year=m.year) as m2,

(select amount from test as m where month='3' and test.year=m.year) as m3

from test group by year

2.請教乙個面試中遇到的sql語句的查詢問題

表中有a b c三列,用sql語句實現:當a列大於b列時選擇a列否則選擇b列,當b列大於c列時選擇b列否則選擇c列。

------------------------------------------

select (case when a>b then a else b end ),

(case when b>c then b esle c end)

from table_name

3.面試題:乙個日期判斷的sql語句?

請取出tb_send表中日期(sendtime欄位)為當天的所有記錄?(sendtime欄位為datetime型,包含日期與時間)

------------------------------------------

select * from tb where datediff(dd,sendtime,getdate())=0

4.有一張表,裡面有3個字段:語文,數學,英語。其中有3條記錄分別表示語文70分,數學80分,英語58分,請用一條sql語句查詢出這三條記錄並按以下條件顯示出來(並寫出您的思路):?

大於或等於80表示優秀,大於或等於60表示及格,小於60分表示不及格。?

顯示格式:?

語文 數學 英語?

及格 優秀 不及格?

------------------------------------------

select

(case when 語文》=80 then '優秀'

when 語文》=60 then '及格'

else '不及格') as 語文,

(case when 數學》=80 then '優秀'

when 數學》=60 then '及格'

else '不及格') as 數學,

(case when 英語》=80 then '優秀'

when 英語》=60 then '及格'

else '不及格') as 英語,

from table

幾個經典sql

幾個經典的sql語句 1.關於group by的sql語句 表結構 year month amount 1991 1 1.1 1991 2 1.2 1991 3 1.3 1992 1 2.1 1992 2 2.2 1992 3 2.3 顯示結果 year m1 m2 m3 1991 1.1 1.2 ...

幾個經典的sql語句

1.關於group by的sql語句 表結構 sql的寫法 select year,select amount from test as m where month 1 and test.year m.year as m1,select amount from test as m where mon...

幾個經典的博弈

一 巴什博奕 bash game 只有一堆 n個物品,兩個人輪流從這堆物品中取物,規定每次至少取乙個,最多取 m個。最後取光者得勝。顯然,如果 n m 1 那麼由於一次最多只能取 m個,所以,無論先取者拿走多少個,後取者都能夠一次拿走剩餘的物品,後者取勝。因此我們發現了如何取勝的法則 如果n m 1...