2017-08-22 40 views
0

此查詢爲我們提供了各種操作的時間。現在它按照名稱,日期和鏟子模型爲每個事件分配時間。不過,我想以某種方式將一名船員的所有名字的平均值加入一個新的專欄。作爲SQL遊戲的新手,擁有可以提供各種平均定義並將它們返回到同一個表的策略將是非常好的。輸出圖片在這個鏈接之後。找到一種方法來創建多個列,在同一個SQL查詢中按不同分組平均數據

https://i.stack.imgur.com/6V5XB.png

DECLARE @start AS DATETIME = '2017-08-15' 

DECLARE @end AS DATETIME = '2017-08-20' 

SELECT * 

FROM ( 

SELECT 

[Name], 
[Date], 
MAX([Crew]) as [Crew], 
MAX([Shovel_ID]) as [Shovel Model], 
CONVERT(DECIMAL(10,1) ,AVG([empty])) as [Empty], 
CONVERT(DECIMAL(10,1) ,AVG([HAUL])) AS [Hauling], 
CONVERT(DECIMAL(10,0) ,AVG([Wait At Dump]*60)) AS [Wait at Dump], 
CONVERT(DECIMAL(10,0) ,AVG([Dumping]*60)) as [Dumping], 
CONVERT(DECIMAL(10,0) ,AVG([Spot at LU]*60)) as [Spot at LU], 
CONVERT(DECIMAL(10,0) ,AVG([Load Time]*60)) AS [Truck Loading], 
CONVERT(DECIMAL(10,0) ,AVG([QUEUE]*60)) as [Wait at Shovel], 
CONVERT(DECIMAL(10,0) ,AVG([Hot Seat]*60)) as [Hot Seat], 
CONVERT(DECIMAL(10,0) ,AVG([Empty Hauling]*60)) as [Empty Stopped], 
CONVERT(DECIMAL(10,0) ,AVG([HAULING STOPPED]*60)) AS [Hauling Stopped] 


FROM (

Select 
haul_cycle, 
max([name]) as [Name], 
max([shovel_id]) as [Shovel_ID], 
max([date]) as [Date], 
max([crew]) as [Crew], 
sum(dumping) as [Dumping], 
sum([spot at lu]) as [Spot at LU], 
sum([wait at dump]) as [Wait at Dump], 
sum([empty]) as [Empty], 
sum([hauling]) as [Haul], 
SUM([Truck Loading]) as [Load Time], 
SUM([Queue at LU]) as [QUEUE], 
SUM([Hot Change]) as [Hot Seat], 
SUM([EMPTY sTOPPED]) AS [Empty Hauling], 
SUM([HAULing STOPPED]) AS [Hauling Stopped], 

SUM([HAULing STOPPED]+[EMPTY STOPPED]+[Queue at LU]+[Truck Loading]+ 
[Hauling]+[Empty]+[Spot at LU]+[wait at dump]+[dumping]) as [Cycle Time], 
COUNT([HAUL_CYCLE]) AS [Number of Cycles] 

FROM (

SELECT 
HAUL_CYCLE_TRANS.HAUL_CYCLE_REC_IDENT as [HAUL_CYCLE], 
CREW_IDENT as [CREW], 
badge.LAST_NAME + ' , ' + badge.FIRST_NAME as [Name], 
DUMP_END_SHIFT_DATE as date , 
DUMP_END_SHIFT_IDENT, 
CAST(EQUIPMENT_STATUS_TRANS.END_TIMESTAMP- 
EQUIPMENT_STATUS_TRANS.START_TIMESTAMP AS FLOAT)*24*60 AS DURATION, 
COALESCE(SUB_STATUS_DESC, STATUS_DESC) AS [Status], 

CASE 
    WHEN HAUL_CYCLE_TRANS.LOADING_UNIT_IDENT IN (4001, 4002, 4005, 4006) 
THEN '6060' 
    ELSE '7495' 
END AS SHOVEL_ID 

FROM HAUL_CYCLE_TRANS 

LEFT JOIN HAUL_UNIT_STATUS_TRANS_COL on 
HAUL_UNIT_STATUS_TRANS_COL.HAUL_CYCLE_REC_IDENT = 
HAUL_CYCLE_TRANS.HAUL_CYCLE_REC_IDENT 

LEFT JOIN EQUIPMENT_STATUS_TRANS on 
EQUIPMENT_STATUS_TRANS.EQUIP_STATUS_REC_IDENT = 
HAUL_UNIT_STATUS_TRANS_COL.EQUIP_STATUS_REC_IDENT 

LEFT JOIN badge on badge.BADGE_IDENT = 
haul_cycle_trans.HAULING_UNIT_BADGE_IDENT 

LEFT JOIN EQUIP_STATUS_CODE on EQUIP_STATUS_CODE.STATUS_CODE = 
EQUIPMENT_STATUS_TRANS.STATUS_CODE 


LEFT JOIN EQUIP_SUB_STATUS_CODE on EQUIP_SUB_STATUS_CODE.SUB_STATUS_CODE = 
EQUIPMENT_STATUS_TRANS.SUB_STATUS_CODE 

WHERE dump_end_shift_date >= @start AND First_Name <> '') raw_data 

PIVOT(SUM(duration) FOR [Status] IN ([Dumping], [Spot at LU],[Wait at Dump], 
[Empty], [Empty Stopped], [Hauling Stopped], [Hauling], [Queue at LU], 
[Truck Loading], [Hot Change])) DATAFIELD 

GROUP BY haul_cycle) SUM_DATA_FOR_EACH_REC 
GROUP BY [name], [date], Shovel_ID 
) AVG_OPERATOR_STATUS 

ORDER BY [NAME],[DATE] 
+0

標記您正在使用的dbms。該代碼是產品特定的! – jarlh

+0

SSMS 2016 - 我的歉意 – Fishingwest

回答

0

可以使用OVER (PARTITION BY)功能。

通過這種方式,您可以使用不同的方式對數據集進行分區,從而使用您希望平均具有的任何分割。

您可以閱讀更多關於OVER條款here

例子:

SELECT 
    DateField 
    ,YourName1 
    ,YourId 
    ,SUM(Value1) 
    ,AVG(Value1) OVER (PARTITION BY DateField) AS DateFieldAverage 
FROM 
    YourTable 
GROUP BY 
    DateField 
    ,YourName1 
    ,YourId 

假設你正在使用SQL Server(基於你的問題頂部的語法)。

相關問題