加入查询乘以结果

时间:2015-07-09 12:29:42

标签: php sql-server

我有这个查询并且效果很好。它给了我一些关于时间的kpis,与设备x有关,这项工作选择要评估的班次。我需要加入来自第二个查询的信息,它可以使用相同的移位ID工作,但是当我将此查询的参数添加到第一个时,结果会增加,例如,冷杉的总和值给出了24(24小时)但是当我将这个查询与第二个查询联系起来时,te结果增加了它的值,我无法意识到什么是错误的。

第一个查询:

ON DELETE ...

此查询提供的结果:

SELECT *
,(Efectivo+Demora+Reserva)/(Efectivo+Demora+Reserva+Mantencion+Averia+Accidente)*100 as [Disp_Fisica]
,IsNULL((Efectivo+Demora+Reserva)/NULLIF((Efectivo+Demora+Reserva+Mantencion+Averia),0),0)*100 as [Disp_Mecanica]
,IsNULL((Efectivo+Demora)/NULLIF((Efectivo+Demora+Reserva),0),0)*100 as [Uso_Disponib]
,IsNULL(Efectivo/NULLIF((Efectivo+Demora),0),0)*100 as [Efic_Operacional]
FROM (
SELECT hist_eqmtlist.unit,
       hist_statusevents.eqmt, 
             sum(case when hist_statusevents.category =1 then (hist_statusevents.duration/3600) else 0 end) as [Efectivo],
             sum(case when hist_statusevents.category =2 then (hist_statusevents.duration/3600) else 0 end) as [Demora],
             sum(case when hist_statusevents.category =3 then (hist_statusevents.duration/3600) else 0 end) as [Reserva],
             sum(case when hist_statusevents.category =4 then (hist_statusevents.duration/3600) else 0 end) as [Mantencion],
             sum(case when hist_statusevents.category =5 then (hist_statusevents.duration/3600) else 0 end) as [Averia],
             sum(case when hist_statusevents.category =6 then (hist_statusevents.duration/3600) else 0 end) as [Accidente]
                        FROM hist_eqmtlist ,hist_statusevents, hist_exproot
WHERE  hist_eqmtlist.unit     = 'Pala' 
   and hist_exproot.shiftindex between '29976' and '29977'
   and hist_statusevents.shiftindex = hist_eqmtlist.shiftindex 
   and hist_statusevents.eqmt = hist_eqmtlist.eqmtid 
   and  hist_exproot.shiftindex = hist_statusevents.shiftindex
GROUP BY hist_statusevents.eqmt, hist_eqmtlist.unit
) A
order by A.eqmt

第二个查询给了我设备吨数

unit    eqmt    Efectivo    Demora  Reserva Mantencion  Averia  Accidente   Disp_Fisica Disp_Mecanica   Uso_Disponib    Efic_Operacional
Camion  CAM336   16,8      2,6   4,35   0   0   0,2 99,0    100 81,8    86,4

此查询给出的结果是按装备移位的吨数

SELECT hist_exproot.shiftdate , hist_dumps.shiftindex, hist_dumps.truck, sum(hist_dumps.dumptons) AS 'Toneladas'
        FROM Powerview.dbo.hist_dumps hist_dumps, Powerview.dbo.hist_exproot hist_exproot
        WHERE hist_dumps.shiftindex = hist_exproot.shiftindex
        GROUP BY hist_exproot.shiftdate, hist_dumps.truck,  hist_dumps.shiftindex
        ORDER BY shiftdate

我需要加入两个查询,但是当我这样做时,它会增加数值,我如何建立关系?

我尝试在总和上添加选项部分:

以及在何处:

shiftdate   shiftindex  truck           Toneladas
2011-01-01  29950       CAM336          8964,00054931641

,查询变为:

hist_exproot.shiftindex

当我需要保留大量的调查结果以及查询时所给出的指标时,当这些结果是" efectivo,demora reserva和accidente"不应该超过24(24小时)

SELECT *
,(Efectivo+Demora+Reserva)/(Efectivo+Demora+Reserva+Mantencion+Averia+Accidente)*100 as [Disp_Fisica]
,IsNULL((Efectivo+Demora+Reserva)/NULLIF((Efectivo+Demora+Reserva+Mantencion+Averia),0),0)*100 as [Disp_Mecanica]
,IsNULL((Efectivo+Demora)/NULLIF((Efectivo+Demora+Reserva),0),0)*100 as [Uso_Disponib]
,IsNULL(Efectivo/NULLIF((Efectivo+Demora),0),0)*100 as [Efic_Operacional]
FROM (
SELECT hist_eqmtlist.unit,
       hist_statusevents.eqmt, 
             sum(case when hist_statusevents.category =1 then (hist_statusevents.duration/3600) else 0 end) as [Efectivo],
             sum(case when hist_statusevents.category =2 then (hist_statusevents.duration/3600) else 0 end) as [Demora],
             sum(case when hist_statusevents.category =3 then (hist_statusevents.duration/3600) else 0 end) as [Reserva],
             sum(case when hist_statusevents.category =4 then (hist_statusevents.duration/3600) else 0 end) as [Mantencion],
             sum(case when hist_statusevents.category =5 then (hist_statusevents.duration/3600) else 0 end) as [Averia],
             sum(case when hist_statusevents.category =6 then (hist_statusevents.duration/3600) else 0 end) as [Accidente],
             sum (hist_dumps.dumptons) as [Toneladas]
                        FROM hist_eqmtlist ,hist_statusevents, hist_exproot,hist_dumps
WHERE  hist_eqmtlist.unit     = 'Pala' 
   and hist_exproot.shiftindex between '29976' and '29977'
   and hist_statusevents.shiftindex = hist_eqmtlist.shiftindex 
   and hist_statusevents.eqmt = hist_eqmtlist.eqmtid 
   and  hist_exproot.shiftindex = hist_statusevents.shiftindex
   and hist_exproot.shiftindex = hist_dumps.shiftindex
GROUP BY hist_statusevents.eqmt, hist_eqmtlist.unit
) A
order by A.unit

0 个答案:

没有答案