sql查询总记录以及 查询每条光缆下的所涉及到表的信息

/*光缆有条少条隐患
1,查询光缆表Optic_Cable,
2,根据光缆表关联的光缆巡检(轨迹巡检)的路线查标石(Optic_LinePat轨迹的路线名称),(Optic_LinePat_Sub,轨迹路线下的标石),
3,然后根据标石关联巡检信息表中这个标石的所有隐患次数(Optic_LinePat_Rec巡检产生的信息表,根据上面查询到的标石进行隐患匹配)
*/
select a.光缆条数 as glcount,a.光缆长度 as gllength,b.正常端口 as yesdk,b.损坏端口 as nodk,
b.业务已使用 as yesyew,b.业务未使用 as noyew,c.经过标石 as biaoshi,d.隐患 as yinhuan   from
(select count(*)as '光缆条数', sum(CONVERT(int,cable_length)) as '光缆长度' from Optic_Cable) a,
(select COUNT(case status  when '0' then '正常'  end) as '正常端口',
COUNT(case status  when '1' then '损坏'  end) as '损坏端口',
COUNT(case when  connect_content='' or connect_content is null    then '未使用' end)'业务未使用',
COUNT(case when  connect_content is not null    then '已使用' end)as '业务已使用'  
from Optic_Cable_Port ) b,
(select COUNT(c.F_Id) '经过标石'from Optic_Cable a /*光缆*/
left join  Optic_LinePat b/*光缆巡检线路*/
on a.F_Id=b.F_Id
left join Optic_LinePat_Sub c/*光缆线路子表(标石)*/
on b.F_Id=c.F_Olp_Id
where b.F_Id is not null) c,
(select COUNT(*)as '隐患' from Optic_Cable a /*光缆*/
left join  Optic_LinePat b/*光缆巡检线路(光缆表中有个轨迹巡检的名称两者进行关联)*/
on a.F_Id=b.F_Id
left join Optic_LinePat_Sub c/*光缆线路子表(轨迹巡检条数下有标石,在标石表中有轨迹的关联编号)*/
on b.F_Id=c.F_Olp_Id
left join Optic_LinePat_Rec d/*在线路巡检表中有标石的编号f_well_id*/
on c.F_Well_Id =d.F_well_id/*拿标石去查询产生的隐患,两者关联*/
where b.F_Id is not null and d.F_Env_State='隐患')d
 
/**查询总记录**/
select a.glcount,a.gllength,b.duankouYes,b.duankouNo ,
b.yewuYes,b.yewuNo ,c.biaoshi ,d.隐患 as yinhuan   from
(select count(*)as glcount, sum(CONVERT(int,cable_length)) as gllength from Optic_Cable) a,
(select COUNT(case status  when '0' then '正常'  end) as duankouYes,
COUNT(case status  when '1' then '损坏'  end) as duankouNo,
COUNT(case when  connect_content='' or connect_content is null    then '未使用' end)yewuNo,
COUNT(case when  connect_content is not null    then '已使用' end)as yewuYes
from Optic_Cable_Port ) b,

(select COUNT(c.F_Id) biaoshi from Optic_Cable a
left join  Optic_LinePat b
on a.F_Id=b.F_Id
left join Optic_LinePat_Sub c
on b.F_Id=c.F_Olp_Id
where b.F_Id is not null) c,

(select COUNT(*)as '隐患' from Optic_Cable a
left join  Optic_LinePat b
on a.F_Id=b.F_Id
left join Optic_LinePat_Sub c
on b.F_Id=c.F_Olp_Id
left join Optic_LinePat_Rec d
on c.F_Well_Id =d.F_well_id
where b.F_Id is not null and d.F_Env_State='隐患')d

原文地址:https://www.cnblogs.com/gxwa/p/7814301.html