2016-04-29 85 views
-1

我們正在試圖分析我們的員工爲了趨勢和預測目的每小時工作多長時間。垂直計算列

我想通過計算垂直列中的所有值來計算人數。

這是每小時格式在SQL的輸出
(我盤旋我要總結的值)
http://i.stack.imgur.com/dJbWi.png
http://i.stack.imgur.com/rM3YV.png

只要該值大於0,我想將其計爲1,因爲這是按小時計算的人數。

這是我的查詢產生輸出的一部分。

SELECT 

--b.*, 
b.EMPLOYEENAME, 
B.DEPARTMENT, 

CONVERT(datetime, LEFT(b.SHIFTA_start,17),103) AS SHIFTA_start, 
CONVERT(datetime, LEFT(b.ShiftA_End,17),103) as ShiftA_End, 
b.StartTime_HOUR, 
b.StartTime_min, 
b.EndTime_HOUR, 
b.EndTime_min, 


CASE WHEN b.[0H_START] < b.[0H_END] THEN b.[0H_START] ELSE b.[0H_END] END AS [0], 
CASE WHEN b.[1H_START] < b.[1H_END] THEN b.[1H_START] ELSE b.[1H_END] END AS [1] 


from 
(
/*Step 2 - calculating minutes from starttime and endtime */ 

select a.*, 

/**Calculating the number of minutes worked from start_time MIN **/ 
CASE WHEN a.StartTime_HOUR = 0 and a.[0] = 1 AND a.StartTime_min !=0 THEN cast(cast((60-a.StartTime_min) as decimal(10,2))/60 as decimal(10,2)) ELSE a.[0] END AS [0H_START], 
CASE WHEN a.StartTime_HOUR = 1 and a.[1] = 1 AND a.StartTime_min !=0 THEN cast(cast((60-a.StartTime_min) as decimal(10,2))/60 as decimal(10,2)) ELSE a.[1] END AS [1H_START], 


/**Calculating the number of minutes worked from END_time MIN **/ 
CASE WHEN a.EndTime_HOUR = 0 and a.[0] = 1 AND a.EndTime_min !=0 THEN cast(cast((a.EndTime_min) as decimal(10,2))/60 as decimal(10,2)) ELSE a.[0] END AS [0H_END], 
CASE WHEN a.EndTime_HOUR = 1 and a.[1] = 1 AND a.EndTime_min !=0 THEN cast(cast((a.EndTime_min) as decimal(10,2))/60 as decimal(10,2)) ELSE a.[1] END AS [1H_END] 
from 

(--Step 1: 
/*to determine 1 or 0 using the start and end hour 
If time falls in the respective hour = 1 
if time doesnt fall in the respective hours = 0*/ 
SELECT 

[EMPLOYEENAME], 
[DEPARTMENT], 
[SHIFTA_start], 
CASE WHEN [SHIFTA_START] !='' OR SHIFTA_START != NULL THEN CONVERT(datetime, LEFT([SHIFTA_START],17),103) ELSE NULL END AS SHIFTA_START_con, 
CASE WHEN [SHIFTA_START] !='' OR SHIFTA_START != NULL THEN DATEPART(hh,CONVERT(datetime, LEFT([SHIFTA_START],17),103)) ELSE NULL END AS StartTime_HOUR, 
CASE WHEN [SHIFTA_START] !='' OR SHIFTA_START != NULL THEN DATEPART(mi,CONVERT(datetime, LEFT([SHIFTA_START],17),103)) ELSE NULL END AS StartTime_min, 

[SHIFTA_end], 
CASE WHEN [SHIFTA_END] !='' OR SHIFTA_end != NULL THEN CONVERT(datetime, LEFT([SHIFTA_END],17),103) ELSE NULL END AS SHIFTA_END_con, 
CASE WHEN [SHIFTA_END] !='' OR SHIFTA_end != NULL THEN DATEPART(hh,CONVERT(datetime, LEFT([SHIFTA_end],17),103)) ELSE NULL END AS EndTime_HOUR, 
CASE WHEN [SHIFTA_END] !='' OR SHIFTA_end != NULL THEN DATEPART(mi,CONVERT(datetime, LEFT([SHIFTA_end],17),103)) ELSE NULL END AS EndTime_min, 


CASE WHEN [SHIFTA_START] !='' AND 0 BETWEEN DATEPART(hh,CONVERT(datetime, LEFT([SHIFTA_START],17),103)) AND DATEPART (hh,CONVERT(datetime, LEFT([SHIFTA_end],17),103)) THEN 1 ELSE 0 END AS [0], 
CASE WHEN [SHIFTA_START] !='' AND 1 BETWEEN DATEPART(hh,CONVERT(datetime, LEFT([SHIFTA_START],17),103)) AND DATEPART (hh,CONVERT(datetime, LEFT([SHIFTA_end],17),103)) THEN 1 ELSE 0 END AS [1] 

from [DatabaseTable].[dbo].[ATTENDANCE] 

where ShiftA_Start != '' and ShiftA_End !='' and shiftA_start != shiftA_End 

)a 

)b 

請幫助,謝謝v多!

+0

後的樣本數據,文本,沒有圖像和提供查詢你已經嘗試了什麼。 –

+0

作爲文本發佈數據是什麼意思?你的意思是用excel格式嗎? 我已經發布了查詢,請看看。 @StanislovasKalašnikovas – Vannessa

回答

0

如果要計算此列所有值的數量,您可以簡單地去:

SELECT COUNT(T.[22]) FROM Table AS T 
WHERE T.[22] > 0 
+0

我想分別以垂直方式計算每列。 – Vannessa