sq服务器,查询来自" as"的值列

时间:2015-07-06 02:00:47

标签: php sql-server

我得到了这个php,我需要从查询的第一部分提供的值中获取一些KPI,但是它们只提供之前给出的名称而没有值。我需要从select的las部分改变以获取值而不是名称?

SELECT hist_eqmtlist.unit,
       hist_statusevents.eqmt, 
       hist_exproot.shiftdate, 
       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'
       (Efectivo+Demora+Reserva)/(Efectivo+Demora+Reserva+Mantencion+Averia+Accidente) as 'Disp. Fisica'
FROM hist_eqmtlist,hist_exproot,hist_statusevents
WHERE hist_exproot.shiftindex = hist_statusevents.shiftindex And hist_statusevents.shiftindex = hist_eqmtlist.shiftindex And hist_statusevents.eqmt = hist_eqmtlist.eqmtid And hist_eqmtlist.unit='Dozer          ' AND hist_exproot.shiftdate='2011-01-02'
GROUP BY hist_statusevents.eqmt, hist_exproot.shiftdate, hist_eqmtlist.unit
order by hist_exproot.shiftdate,hist_statusevents.eqmt

1 个答案:

答案 0 :(得分:2)

在调用查询中无法访问列名Efectivo,Reserva,Mantencion ...但您可以在外部查询中使用这些列名,如下所示....

还可以使用方括号[]作为对象名称。

SELECT *
,(Efectivo+Demora+Reserva)/(Efectivo+Demora+Reserva+Mantencion+Averia+Accidente) as [Disp. Fisica]
FROM (
SELECT hist_eqmtlist.unit,
       hist_statusevents.eqmt, 
       hist_exproot.shiftdate, 
       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 tableNames ) A

您的查询

SELECT *
,(Efectivo+Demora+Reserva)/(Efectivo+Demora+Reserva+Mantencion+Averia+Accidente) as [Disp. Fisica]
FROM (
SELECT hist_eqmtlist.unit,
       hist_statusevents.eqmt, 
       hist_exproot.shiftdate, 
       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 
INNER JOIN  hist_statusevents   ON  hist_statusevents.shiftindex = hist_eqmtlist.shiftindex 
                               AND  hist_statusevents.eqmt = hist_eqmtlist.eqmtid 
INNER JOIN  hist_exproot        ON  hist_exproot.shiftindex = hist_statusevents.shiftindex
WHERE  hist_eqmtlist.unit     = 'Dozer' 
   AND hist_exproot.shiftdate = '2011-01-02'
GROUP BY hist_statusevents.eqmt, hist_exproot.shiftdate, hist_eqmtlist.unit
) A
order by A.shiftdate,A.eqmt