当前位置: 代码迷 >> Sql Server >> 又栽在sql上面了,小弟我只能说tmd,真tmd的,太气人了。
  详细解决方案

又栽在sql上面了,小弟我只能说tmd,真tmd的,太气人了。

热度:66   发布时间:2016-04-24 19:56:15.0
又栽在sql上面了,我只能说tmd,真tmd的,太气人了。。
气自己。。。

冲冲忙忙赶过去面试,过去填张信息表,接着就是两套sql面试题,任选一套。。。

面试题如下:

——————————————————————————————————————
题目一:
学生表 stu
stuid     stuname

科目表K
kid        kname

成绩表C
cid     stuid    kid    score


求显示效果:如下?

姓名   语文    数学    英语    化学
张三    90       80     79      87
李四    89       87     56      45
王五    97       78     78      85
赵六    50       78     98      79
注:没参加科目分数对应显示为 NULL


题目二:
商品表  pro
proid   pname
1        帽子
2        上衣
3        裤子
4        鞋子
... ...

具体商品表  JT,字段如下
JTid    jtName    parent    clicknum
1        女士帽子   1         903
2         男士帽子  1         892
3         儿童帽子  1          981
4        女士上衣   2         349
5         男士上衣  2          980
6         女士裤子  3          87
7         女士鞋子  4          887
... ...
分别为id,具体商品名称,输入那个商品种类,点击次数

求每个商品种类下面点击率最高的"具体商品种类"名称,具体商品id,点击次数,以及所属商品id?

------解决方案--------------------
提高只能你自己去做,做到出来为止,你这两题貌似也太简单了吧.....
------解决方案--------------------
SQL行转列
------解决方案--------------------

if OBJECT_ID('stu') is not null drop table stu
create table stu(stuid int identity(1,1),stuname nvarchar(20))
insert into stu(stuname)
select '张三' union all
select '李四' union all
select '王五' union all
select '赵六'

if OBJECT_ID('subjects') is not null drop table subjects
create table subjects(kid int identity(1,1),kname nvarchar(20))
insert into subjects(kname)
select '语文' union all
select '数学' union all
select '英语' union all
select '化学'

if OBJECT_ID('score') is not null drop table score
create table score(cid int identity(1,1),stuid int,kid int,score int)
insert into score(stuid,kid,score)
select 1,1,90 union all
select 1,3,95 union all
select 2,1,85 union all
select 2,2,89 union all
select 2,4,90 union all
select 3,2,95 union all
select 3,3,78 union all
select 4,1,77 union all
select 4,3,83

;with T as
(
select st.stuname as 姓名,s.kname,c.score from score as c left join 
subjects s on c.kid=s.kid
left join stu as st on c.stuid=st.stuid
)
select * from T
Pivot (
sum(score) FOR kname IN
(语文,数学,英语,化学))t

/*
姓名 语文 数学 英语 化学
---  --- ---  --- ---
李四 85 89 NULL 90
王五 NULL 95 78 NULL
张三 90 NULL 95 NULL
赵六 77 NULL 83 NULL
*/
drop table stu
drop table subjects
drop table score


------解决方案--------------------
select * from pro a join JT b where a.proid =b.parent 
 and not exists(select 1 from JT where parent=b.parent and clicknum>b.clicknum)
------解决方案--------------------
下次面试的时候找个人做场外支持

我有个同学都被我支持了一份不错的工作
  相关解决方案