制作SQL二维表

 图片
如果需要类似的如上图的二维表,可以用到left outer join ...on ...
具体如下
通过一下语句,可以查出相应单位的电视类别的工单数:
select l.id,l.report_location,count(*) as dianshi from (select bpi.id,bpi.report_location from itsm_bp_incident bpi,itsm_bp_category bpc where bpi.CATEGORY_ID=bpc.ID AND bpc.name='电视' ) l , itsm_bp_instance bps where l.id= bps.ID and bps.CREATED_ON BETWEEN '2013-02-01 00:00:00' and '2013-06-28 00:00:00'  group by l.report_location
查询结果类似如下:
 图片
通过 left outer join 可以加入电话的资料,具体如下:
select a.report_location as '单位',IFNULL(a.dianshi,0) as '电视',IFNULL(b.dianhua,0) as '电话' from (select l.id,l.report_location,count(*) as dianshi from (select bpi.id,bpi.report_location from itsm_bp_incident bpi,itsm_bp_category bpc where bpi.CATEGORY_ID=bpc.ID AND bpc.name='电视' ) l , itsm_bp_instance bps where l.id= bps.ID and bps.CREATED_ON BETWEEN '2013-02-01 00:00:00' and '2013-06-28 00:00:00' group by l.report_location) LEFT OUTER JOIN
(select l.id,l.report_location,count(*) as dianhua from (select bpi.id,bpi.report_location from itsm_bp_incident bpi,itsm_bp_category bpc where bpi.CATEGORY_ID=bpc.ID AND bpc.name='电话' ) l , itsm_bp_instance bps where l.id= bps.ID and bps.CREATED_ON BETWEEN '2013-02-01 00:00:00' and '2013-06-28 00:00:00'  group by l.report_location) b on a.report_location=b.report_location 
查询结果如下: 
图片
通过类似方法,可以加入更多类别,但是需要对应的是category_id,必须共有的,且作为on的参数来left outer join
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值