SQL语句With As的用法

 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到。因为UNION ALL的每个部分可能相同,但是如果每个部分都去执行一遍的话,则成本太高,所以可以使用WITH AS短语,则只要执行一遍即可。

其实就是把一大堆重复用到的SQL语句放在with as 里面,取一个别名,后面的查询就可以用它。

with lines as
 (SELECT t.line
    FROM m_line t
   where 1 = 1 and t.enabled = 'Y'
   order by t.line),
a as
 (select t.line,round((count(t.serial_number) - sum(case when c.errcode!='PASS' then 1 else 0 end))/count(t.serial_number)*100,2) || '%' as con
                               from tableA t 
                               left join tableB c on c.serial_number=t.serial_number 
                              where t.LINE_TIME >=
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 07:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')
                                and t.LINE_TIME <
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 08:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')       
                                            group by t.line),
b as
 (select t.line,round((count(t.serial_number) - sum(case when c.errcode!='PASS' then 1 else 0 end))/count(t.serial_number)*100,2) || '%' as con
                               from tableA t 
                               left join tableB c on c.serial_number=t.serial_number 
                              where t.LINE_TIME >=
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 07:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')
                                and t.LINE_TIME <
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 09:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')            
                                            group by t.line),
c as
 (select t.line,round((count(t.serial_number) - sum(case when c.errcode!='PASS' then 1 else 0 end))/count(t.serial_number)*100,2) || '%' as con
                               from tableA t 
                               left join tableB c on c.serial_number=t.serial_number 
                              where t.LINE_TIME >=
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 07:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')
                                and t.LINE_TIME <
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 10:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')                     
                                            group by t.line),
d as
 (select t.line,round((count(t.serial_number) - sum(case when c.errcode!='PASS' then 1 else 0 end))/count(t.serial_number)*100,2) || '%' as con
                               from tableA t 
                               left join tableB c on c.serial_number=t.serial_number 
                              where t.LINE_TIME >=
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 07:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')
                                and t.LINE_TIME <
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 11:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')                             
                                            group by t.line),
e as
 (select t.line,round((count(t.serial_number) - sum(case when c.errcode!='PASS' then 1 else 0 end))/count(t.serial_number)*100,2) || '%' as con
                               from tableA t 
                               left join tableB c on c.serial_number=t.serial_number 
                              where t.LINE_TIME >=
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 07:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')
                                and t.LINE_TIME <
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 12:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')                                     
                                            group by t.line),
f as
 (select t.line,round((count(t.serial_number) - sum(case when c.errcode!='PASS' then 1 else 0 end))/count(t.serial_number)*100,2) || '%' as con
                               from tableA t 
                               left join tableB c on c.serial_number=t.serial_number 
                              where t.LINE_TIME >=
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 07:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')
                                and t.LINE_TIME <
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 13:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')                             
                                            group by t.line),
g as
 (select t.line,round((count(t.serial_number) - sum(case when c.errcode!='PASS' then 1 else 0 end))/count(t.serial_number)*100,2) || '%' as con
                               from tableA t 
                               left join tableB c on c.serial_number=t.serial_number 
                              where t.LINE_TIME >=
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 07:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')
                                and t.LINE_TIME <
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 14:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')                                
                                            group by t.line),
h as
 (select t.line,round((count(t.serial_number) - sum(case when c.errcode!='PASS' then 1 else 0 end))/count(t.serial_number)*100,2) || '%' as con
                               from tableA t 
                               left join tableB c on c.serial_number=t.serial_number 
                              where t.LINE_TIME >=
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 07:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')
                                and t.LINE_TIME <
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 15:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')                              
                                            group by t.line),
i as
 (select t.line,round((count(t.serial_number) - sum(case when c.errcode!='PASS' then 1 else 0 end))/count(t.serial_number)*100,2) || '%' as con
                               from tableA t 
                               left join tableB c on c.serial_number=t.serial_number 
                              where t.LINE_TIME >=
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 07:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')
                                and t.LINE_TIME <
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 16:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')                               
                                            group by t.line),
j as
 (select t.line,round((count(t.serial_number) - sum(case when c.errcode!='PASS' then 1 else 0 end))/count(t.serial_number)*100,2) || '%' as con
                               from tableA t 
                               left join tableB c on c.serial_number=t.serial_number 
                              where t.LINE_TIME >=
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 07:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')
                                and t.LINE_TIME <
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 17:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')                                 
                                            group by t.line),
k as
 (select t.line,round((count(t.serial_number) - sum(case when c.errcode!='PASS' then 1 else 0 end))/count(t.serial_number)*100,2) || '%' as con
                               from tableA t 
                               left join tableB c on c.serial_number=t.serial_number 
                              where t.LINE_TIME >=
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 07:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')
                                and t.LINE_TIME <
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 18:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')                              
                                            group by t.line),
l as
 (select t.line,round((count(t.serial_number) - sum(case when c.errcode!='PASS' then 1 else 0 end))/count(t.serial_number)*100,2) || '%' as con
                               from tableA t 
                               left join tableB c on c.serial_number=t.serial_number 
                              where t.LINE_TIME >=
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 07:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')
                                and t.LINE_TIME <
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 19:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')                                 
                                            group by t.line),
m as
 (select t.line,round((count(t.serial_number) - sum(case when c.errcode!='PASS' then 1 else 0 end))/count(t.serial_number)*100,2) || '%' as con
                               from tableA t 
                               left join tableB c on c.serial_number=t.serial_number 
                              where t.LINE_TIME >=
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 07:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')
                                and t.LINE_TIME <
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 20:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')                                    
                                            group by t.line),
n as
 (select t.line,round((count(t.serial_number) - sum(case when c.errcode!='PASS' then 1 else 0 end))/count(t.serial_number)*100,2) || '%' as con
                               from tableA t 
                               left join tableB c on c.serial_number=t.serial_number 
                              where t.LINE_TIME >=
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 07:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')
                                and t.LINE_TIME <
                                    TO_DATE(to_char(sysdate, 'yyyy-mm-dd') ||
                                            ' 21:30:00',
                                            'yyyy-MM-dd HH24:MI:SS')                                    
                                            group by t.line)
SELECT lines.line,
       
       a.con as "07:30~08:30",
       b.con as "07:30~09:30",
       c.con as "07:30~10:30",
       d.con as "07:30~11:30",
       e.con as "07:30~12:30",
       f.con as "07:30~13:30",
       g.con as "07:30~14:30",
       h.con as "07:30~15:30",
       i.con as "07:30~16:30",
       j.con as "07:30~17:30",
       k.con as "07:30~18:30",
       l.con as "07:30~19:30",
       m.con as "07:30~20:30",
       n.con as "SUMMARY"
  FROM lines
  left join a
    on a.line = lines.line
  left join b
    on b.line = lines.line
  left join c
    on c.line = lines.line
  left join d
    on d.line = lines.line
  left join e
    on e.line = lines.line
  left join f
    on f.line = lines.line
  left join g
    on g.line = lines.line
  left join h
    on h.line = lines.line
  left join i
    on i.line = lines.line
  left join j
    on j.line = lines.line
  left join k
    on k.line = lines.line
  left join l
    on l.line = lines.line
  left join m
    on m.line = lines.line
  left join n
    on n.line = lines.line
    where length(n.con)>0 order by lines.line asc;
     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

痕迹灬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值