2015-07-06 19 views
0

我得到了這個php,我需要從查詢的第一部分提供的值中獲得一些KPI,但它們只給出之前給出的名稱並且沒有值。我需要從選擇的las部分改變以獲取值而不是名稱?sq服務器,查詢考慮從「as」列的值

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 

回答

2

列名侮辱和不尊重,陳酒,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 
+0

我將編輯查詢,因爲它沒有工作 – Pipebritop

+0

'它沒有work'是一個非常模糊的術語,什麼也沒有確切地工作?你有錯誤信息?它沒有顯示你期望的結果?或者是其他東西 ???? –

+0

ahhh現在明白了! – Pipebritop