咨詢電話:023-6276-4481
熱門文章
電 話:023-6276-4481
郵箱:broiling@qq.com
地址:重慶市南岸區(qū)亞太商谷6幢25-2
1.一道SQL語句面試題,關(guān)于group by
表內(nèi)容:
2005-05-09 勝
2005-05-09 勝
2005-05-09 負(fù)
2005-05-09 負(fù)
2005-05-10 勝
2005-05-10 負(fù)
2005-05-10 負(fù)
如果要生成下列結(jié)果, 該如何寫sql語句?
勝 負(fù)
2005-05-09 2 2
2005-05-10 1 2
------------------------------------------
select time, sum(case when shengfu='勝' then 1 else 0 end) as '勝',sum(case when shengfu='負(fù)' then 1 else 0 end) as '負(fù)' from my_table group by time
2.請教一個面試中遇到的SQL語句的查詢問題
表中有A B C三列,用SQL語句實(shí)現(xiàn):當(dāng)A列大于B列時選擇A列否則選擇B列,當(dāng)B列大于C列時選擇B列否則選擇C列。
------------------------------------------
select (case when a>b then a else b end ),(case when b>c then b esle c end) from my_table
3.面試題:一個當(dāng)天日期判斷的sql語句?
請取出tb_send表中日期(SendTime字段)為當(dāng)天的所有記錄?(SendTime字段為datetime型,包含日期與時間)
------------------------------------------
select * from tb where datediff(dd,SendTime,getdate())=0
4.有一張表,里面有3個字段:語文,數(shù)學(xué),英語。其中有3條記錄分別表示語文70分,數(shù)學(xué)80分,英語58分,請用一條sql語句查詢出這三條記錄并按以下條件顯示出來(并寫出您的思路):
大于或等于80表示優(yōu)秀,大于或等于60表示及格,小于60分表示不及格。
顯示格式:
語文 數(shù)學(xué) 英語
及格 優(yōu)秀 不及格
------------------------------------------
select
(case when 語文>=80 then '優(yōu)秀'
when 語文>=60 then '及格'
else '不及格') as 語文,
(case when 數(shù)學(xué)>=80 then '優(yōu)秀'
when 數(shù)學(xué)>=60 then '及格'
else '不及格') as 數(shù)學(xué),
(case when 英語>=80 then '優(yōu)秀'
when 英語>=60 then '及格'
else '不及格') as 英語,
from my_table
7.請用一個sql語句得出結(jié)果
從table1,table2中取出如Result所列格式數(shù)據(jù),注意提供的數(shù)據(jù)及結(jié)果不準(zhǔn)確,只是作為一個格式向大家請教。
如使用存儲過程也可以。
-------------------------------------
table1
月份mon 部門dep 業(yè)績yj
一月份 01 10
一月份 02 10
一月份 03 5
二月份 02 8
二月份 04 9
三月份 03 8
-----------------------------------
table2
部門dep 部門名稱dname
01 國內(nèi)業(yè)務(wù)一部
02 國內(nèi)業(yè)務(wù)二部
03 國內(nèi)業(yè)務(wù)三部
04 國際業(yè)務(wù)部
---------------------------------------------------
Result
部門名稱dname 一月份 二月份 三月份
國內(nèi)業(yè)務(wù)一部 10 null null
國內(nèi)業(yè)務(wù)二部 10 8 null
國內(nèi)業(yè)務(wù)二部 null 5 8
國際業(yè)務(wù)部 null null 9
---------------------------------------------------
select a.dep,
sum(case when a.mon=1 then a.yj else 0 end) as '一月份',
sum(case when a.mon=2 then a.yj else 0 end) as '二月份',
sum(case when a.mon=3 then a.yj else 0 end) as '三月份'
from table2 b left join table1 a on a.dep=b.dep
8.華為一道面試題
一個表中的Id有多個記錄,把所有這個id的記錄查出來,并顯示共有多少條記錄數(shù)。
------------------------------------------
select id, Count(*) from tb group by id having count(*)>1