自己選擇的路,摸爬滾打也要走下去

          【SQL 筆試面試】一

           

          1.一道SQL語句面試題,關于group by
          表內容:
          2005-05-09

          2005-05-09

          2005-05-09

          2005-05-09

          2005-05-10

          2005-05-10

          2005-05-10


          如果要生成下列結果, 該如何寫sql語句?

                   
           
          2005-05-09 2 2
          2005-05-10 1 2
          ------------------------------------------
          create table #tmp(rq varchar(10),shengfu nchar(1))

          insert into #tmp values('2005-05-09','
          ')
          insert into #tmp values('2005-05-09','
          ')
          insert into #tmp values('2005-05-09','
          ')
          insert into #tmp values('2005-05-09','
          ')
          insert into #tmp values('2005-05-10','
          ')
          insert into #tmp values('2005-05-10','
          ')
          insert into #tmp values('2005-05-10','
          ')

          1)select rq, sum(case when shengfu='' then 1 else 0 end)'',sum(case when shengfu='' then 1 else 0 end)'' from #tmp group by rq
          2) select N.rq,N.
          ,M.from (
          select rq,
          =count(*) from #tmp where shengfu=''group by rq)N inner join
          (select rq,
          =count(*) from #tmp where shengfu=''group by rq)M on N.rq=M.rq
          3)select a.col001,a.a1
          ,b.b1 from
          (select col001,count(col001) a1 from temp1 where col002='
          ' group by col001) a,
          (select col001,count(col001) b1 from temp1 where col002='
          ' group by col001) b
          where a.col001=b.col001

          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

           

          7.請用一個sql語句得出結果
          table1,table2中取出如table3所列格式數據,注意提供的數據及結果不準確,只是作為一個格式向大家請教。
          如使用存儲過程也可以。

          table1

          月份mon 部門dep 業績yj
          -------------------------------
          一月份      01      10
          一月份      02      10
          一月份      03      5
          二月份      02      8
          二月份      04      9
          三月份      03      8

          table2

          部門dep      部門名稱dname
          --------------------------------
                01     
          國內業務一部
                02     
          國內業務二部
                03     
          國內業務三部
                04     
          國際業務部

          table3
          result

          部門dep 一月份      二月份      三月份
          --------------------------------------
                01      10        null      null
                02      10         8        null
                03      null       5        8
                04      null      null      9

          ------------------------------------------
          1)
          select a.
          部門名稱dname,b.業績yj as '一月份',c.業績yj as '二月份',d.業績yj as '三月份'
          from table1 a,table2 b,table2 c,table2 d
          where a.
          部門dep = b.部門dep and b.月份mon = '一月份' and
          a.
          部門dep = c.部門dep and c.月份mon = '二月份' and
          a.
          部門dep = d.部門dep and d.月份mon = '三月份' and
          2)
          select a.dep,
          sum(case when b.mon=1 then b.yj else 0 end) as '
          一月份',
          sum(case when b.mon=2 then b.yj else 0 end) as '
          二月份',
          sum(case when b.mon=3 then b.yj else 0 end) as '
          三月份',
          sum(case when b.mon=4 then b.yj else 0 end) as '
          四月份',
          sum(case when b.mon=5 then b.yj else 0 end) as '
          五月份',
          sum(case when b.mon=6 then b.yj else 0 end) as '
          六月份',
          sum(case when b.mon=7 then b.yj else 0 end) as '
          七月份',
          sum(case when b.mon=8 then b.yj else 0 end) as '
          八月份',
          sum(case when b.mon=9 then b.yj else 0 end) as '
          九月份',
          sum(case when b.mon=10 then b.yj else 0 end) as '
          十月份',
          sum(case when b.mon=11 then b.yj else 0 end) as '
          十一月份',
          sum(case when b.mon=12 then b.yj else 0 end) as '
          十二月份',
          from table2 a left join table1 b on a.dep=b.dep

           

          8.華為一道面試題
          一個表中的Id有多個記錄,把所有這個id的記錄查出來,并顯示共有多少條記錄數。
          ------------------------------------------
          select id, Count
          *) from tb group by id having count(*)>1
          select * from(select count(ID) as count from table group by ID)T where T.count>1



          一天,一個月,一年。總有一天會變得不一樣。

          posted on 2011-02-13 12:30 wokaoJune 閱讀(980) 評論(0)  編輯  收藏 所屬分類: SQL 筆試面試

          <2011年2月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272812345
          6789101112

          導航

          統計

          公告

          GO ,GO,GO
          自己選擇的路,摸爬滾打也要走下去

          常用鏈接

          留言簿

          隨筆分類(26)

          隨筆檔案(29)

          文章分類

          最新隨筆

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 镇沅| 象山县| 兴安县| 大理市| 宁津县| 永济市| 长垣县| 湟源县| 四平市| 鹤峰县| 班戈县| 元江| 石楼县| 偃师市| 怀远县| 东宁县| 穆棱市| 正蓝旗| 女性| 玉屏| 泸水县| 北海市| 阿荣旗| 阜新| 贺兰县| 彝良县| 寿光市| 泗洪县| 阿拉尔市| 扶绥县| 昌黎县| 东安县| 凌海市| 宝兴县| 陵川县| 洪江市| 南木林县| 巴楚县| 玉田县| 邢台县| 凉山|