2016-11-24 69 views
0

創建頻率分佈我有一個表delay像這樣使用T-SQL查詢

Delay 
----- 
-15 
139 
81 
-3 
-17 
159 
8 
-128 
169 
-28 
98 
-62 
-51 
261 
-172 
217 
65 
-91 
-22 
170 
50 
5 
213 
60 
-23 
27 
-45 
-23 
389 
244 
-82 
11 
92 
30 
-42 
-34 
-151 
150 
-57 
-148 
313 
-36 
1 
50 
2 
-111 
-63 

數據,我想用一個查詢

Range   Values Frequency 
------------------------------------ 
On time    0  407 
Late < 1 min  60  159 
Late by 1-2 min 120  80 
Late by 2-3 min 180  71 
Late by 3-4 min 240  35 
Late by 4-5 min 300  19 
Late by 5-6 min 360   8 
Late by 6-7 min 420   9 
Late by 7-8 min 480   4 
Late >8 minutes  14 

我搜索創建一個表是這樣的給定的問題。但有些答案與表中類似值的計數有關,但它們都沒有對數據進行分類。

請幫

+0

延遲在第二?負延遲的含義是什麼? – Susilo

+0

對於比預定時間提早到達展會的人是否爲負 – Fan

+0

什麼是價值觀?範圍內的所有元素的總和? –

回答

0

SQL DEMO

with ranges as (
    SELECT 'On time' r UNION ALL 
    SELECT 'Late < 1 min' UNION ALL 
    SELECT 'Late by 1-2 min' UNION ALL 
    SELECT 'Late by 2-3 min' UNION ALL 
    SELECT 'Late by 3-4 min' UNION ALL 
    SELECT 'Late by 4-5 min' UNION ALL 
    SELECT 'Late by 5-6 min' UNION ALL 
    SELECT 'Late by 6-7 min' UNION ALL 
    SELECT 'Late by 7-8 min' UNION ALL 
    SELECT 'Late >8 minutes' 
), setup as ( 
    SELECT CASE WHEN [Delay] = 0 THEN 'On time' 
       WHEN [Delay] < 1 THEN 'Late < 1 min' 
       WHEN [Delay] < 2 THEN 'Late by 1-2 min' 
       WHEN [Delay] < 3 THEN 'Late by 2-3 min' 
       WHEN [Delay] < 4 THEN 'Late by 3-4 min' 
       WHEN [Delay] < 5 THEN 'Late by 4-5 min' 
       WHEN [Delay] < 6 THEN 'Late by 5-6 min' 
       WHEN [Delay] < 7 THEN 'Late by 6-7 min'    
       WHEN [Delay] < 8 THEN 'Late by 7-8 min'   
       ELSE 'Late >8 minutes' 
      END as [Range], 
      [Delay] 
    FROM Table1  
) 
SELECT t.r as Range, COALESCE(SUM(s.[Delay]),0) as Value, COUNT(s.[Delay]) as Freq 
FROM ranges t 
LEFT JOIN setup s 
    ON t.r = s.[Range] 
GROUP BY t.r 

輸出

enter image description here

0

這個問題可以用CASEIIF來解決,BU TI建議爲代碼更易讀..和可擴展的使用功能..

用戶功能 - ufRange

CREATE FUNCTION dbo.ufRange (@Delay INT) 
RETURNS INT 
AS 
BEGIN 
    DECLARE @Ret INT 
    IF @Delay <= 0 SET @Ret = 0   -- not late/ early 
    ELSE IF @Delay > 320 SET @Ret = 9 -- more than 8 minutes late 
    ELSE SET @Ret = @Delay/60   -- late <=1 to <=8 minutes 
    RETURN @Ret 
END 

然後把你的range詳細的支持表LateRange,就像在Excel中,如果你熟悉使用vlookup

支撐臺 - LateRange

idrange   Range        Values 
    0    On time        0 
    1    Late < 1 min       60 
    2    Late by 1-2 min      120 
    3    Late by 2-3 min      180 
    4    Late by 3-4 min      240 
    5    Late by 4-5 min      300 
    6    Late by 5-6 min      360 
    7    Late by 6-7 min      420 
    8    Late by 7-8 min      480 
    9    Late >8 minutes 

好的。最後假定名爲LateListdelay保存表,查詢

SELECT Range, Values, 
     (SELECT COUNT(*) FROM LateList WHERE dbo.ufRange([delay]) = idrange) Frequency 
FROM LateRange 

或替代,如果你喜歡,你可以修改使用INNER JOIN和子查詢..