职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 284|回复: 10

某牛企一sql面试题目~~~~~~~~

[复制链接]
找不到我 发表于 2011-8-28 09:11 | 显示全部楼层 |阅读模式
有report表:它里面 id  name   state    customerid 四个字段,id为主键 ,customerid为外键
state 0, 1,2   //  未使用  更新  删除

根据customerid写一条sql (注意是一条)生成表的结构如下:

customerid  state0   state1  state2
001          11       212      333
002          15       545      3







  

走失的猫咪 发表于 2011-8-28 09:11 | 显示全部楼层
典型的行转列问题

Sql代码  
select customid,   
    count(case status when 0 then status else null end) as status-0,   
    count(case status when 1 then status else null end) as status-1,   
    count(case status when 2 then status else null end) as status-2   
    from custom group by customid.  

select customid,
    count(case status when 0 then status else null end) as status-0,
    count(case status when 1 then status else null end) as status-1,
    count(case status when 2 then status else null end) as status-2
    from custom group by customid.  

爱车车 发表于 2011-8-28 09:11 | 显示全部楼层
select distinct r.customerid,
(select count(r0.id) from report r0 where r.customerid = customerid and r0.state = 0) state0,
(select count(r1.id) from report r1 where r.customerid = customerid and r1.state = 1) state1,
(select count(r2.id) from report r2 where r.customerid = customerid and r2.state = 2) state2
from report r

已经来了吗 发表于 2011-8-28 09:11 | 显示全部楼层
liangge0218 写道
典型的行转列问题


Sql代码  
select customid,   
    count(case status when 0 then status else null end) as status-0,   
    count(case status when 1 then status else null end) as status-1,   
    count(case status when 2 then status else null end) as status-2   
    from custom group by customid.  

select customid,
    count(case status when 0 then status else null end) as status-0,
    count(case status when 1 then status else null end) as status-1,
    count(case status when 2 then status else null end) as status-2
    from custom group by customid.
你这种做法,确实可以,我的也行

走失的猫咪 发表于 2011-8-28 09:11 | 显示全部楼层
liangge0218 写道
典型的行转列问题


Sql代码  
select customid,   
    count(case status when 0 then status else null end) as status-0,   
    count(case status when 1 then status else null end) as status-1,   
    count(case status when 2 then status else null end) as status-2   
    from custom group by customid.  

select customid,
    count(case status when 0 then status else null end) as status-0,
    count(case status when 1 then status else null end) as status-1,
    count(case status when 2 then status else null end) as status-2
    from custom group by customid.
中间部分,我习惯写成sum(case status when 0 then 1 else 0 end) as status-0
感觉灵活性比较好。。。要加权神马的比较方便

fl 发表于 2011-8-28 09:12 | 显示全部楼层
记得我工作面试 全不会做 照样入职

醉倚西风 发表于 2011-8-28 09:12 | 显示全部楼层
这个技巧叫行转列,放狗一搜一大堆!

醉倚西风 发表于 2011-8-28 09:12 | 显示全部楼层
我一直想知道行转列这种东西,是什么场景下会用到,我从未有机会用!

北大青鸟 发表于 2011-8-28 09:12 | 显示全部楼层
BloodyCoder 写道
我一直想知道行转列这种东西,是什么场景下会用到,我从未有机会用!

那你是没有被各式各样的报表折磨过

ksdal 发表于 2011-8-28 09:12 | 显示全部楼层
select d.customerid as customerid,count(a.state) as state0,count(c.state) as state1,count(c.state) as state2   

from report a ,report b,report c,report d where a.customerid = b.customerid and b.customerid = c.customerid  and c.customerid = d.customerid

and a.state = 0 and b.state = 1 and c.state = 2 group by d.customerid

芷馨 发表于 2011-8-29 09:34 | 显示全部楼层
知道了 不错~~~
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-7 20:10 , Processed in 0.124076 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表