2017-03-16 79 views
0

我想從下表創建臨時表。根據列中的值向臨時表添加新列

------------------------|-------- 
Date     | Length 
------------------------|-------- 
2014-08-28 00:00:00.000 | 1.5 
2014-08-28 00:00:00.000 | 2.6 
2014-08-28 00:00:00.000 | 1.5 
2014-08-28 00:00:00.000 | 3.3 
2014-08-28 00:00:00.000 | 1.1 
2014-08-28 00:00:00.000 | 8.5 
2014-08-28 00:00:00.000 | 8.6 
2014-08-28 00:00:00.000 | 11.3 

並且臨時表看起來像下面那樣。

Date     | Length | Length_Range 
------------------------|---------|-------------- 
2014-08-28 00:00:00.000 | 1.5 | 1-4 
2014-08-28 00:00:00.000 | 2.6 | 1-4 
2014-08-28 00:00:00.000 | 6.5 | 5-10 
2014-08-28 00:00:00.000 | 3.3 | 1-4 
2014-08-28 00:00:00.000 | 1.1 | 1-4 
2014-08-28 00:00:00.000 | 8.5 | 5-10 
2014-08-28 00:00:00.000 | 8.6 | 5-10 
2014-08-28 00:00:00.000 | 11.3 | 11-15 

我希望能夠定義[Length_Range]。

的Microsoft SQL Server 2016 兼容性級別:SQL Server 2005中(90)

+0

出於好奇,對於長度爲4.6的項目,結果應該如何?或者'10.4'? –

+0

@Damien_The_Unbeliever 4.6 = 1-5,10.4 = 11-15 –

回答

1

使用case

select t.*, 
     (case when length >= 1 and length < 4 then '1-4' 
      when length < 10 then '5-10' 
      when length < 15 then '11-15' 
      else '16+' 
     end) as length_range 
into #temp_t 
from t; 
1
CREATE TABLE #TABLE1 
    ([DATE] DATETIME, [LENGTH] FLOAT) 
INSERT INTO #TABLE1 
    ([DATE], [LENGTH]) 
VALUES 
    ('2014-08-28 00:00:00', 1.5), 
    ('2014-08-28 00:00:00', 2.6), 
    ('2014-08-28 00:00:00', 1.5), 
    ('2014-08-28 00:00:00', 3.3), 
    ('2014-08-28 00:00:00', 1.1), 
    ('2014-08-28 00:00:00', 8.5), 
    ('2014-08-28 00:00:00', 8.6), 
    ('2014-08-28 00:00:00', 1.3) 

SELECT *,CASE 
WHEN LENGTH BETWEEN 1 AND 4 THEN '1-4' 
WHEN LENGTH BETWEEN 5 AND 10 THEN '5-10' 
WHEN LENGTH BETWEEN 11 AND 15 THEN '11-15' END AS LENGHT_RANGE 
FROM #TABLE1 

輸出

Date     Length LENGHT_RANGE 
2014-08-28 00:00:00.000 1.5   1-4 
2014-08-28 00:00:00.000 2.6   1-4 
2014-08-28 00:00:00.000 1.5   1-4 
2014-08-28 00:00:00.000 3.3   1-4 
2014-08-28 00:00:00.000 1.1   1-4 
2014-08-28 00:00:00.000 8.5   5-10 
2014-08-28 00:00:00.000 8.6   5-10 
2014-08-28 00:00:00.000 1.3   1-4 
0

--May幫助這個

;WITH cte (
    [Date] 
    ,[Length] 
    ) 
AS (
    SELECT cast('2014-08-28 00:00:00.000' AS DATETIME) 
     ,CAST('1.5' AS DECIMAL(4, 2)) 

    UNION ALL 

    SELECT '2014-08-28 00:00:00.000' 
     ,'2.6' 

    UNION ALL 

    SELECT '2014-08-28 00:00:00.000' 
     ,'1.5' 

    UNION ALL 

    SELECT '2014-08-28 00:00:00.000' 
     ,'3.3' 

    UNION ALL 

    SELECT '2014-08-28 00:00:00.000' 
     ,'1.1' 

    UNION ALL 

    SELECT '2014-08-28 00:00:00.000' 
     ,'8.5' 

    UNION ALL 

    SELECT '2014-08-28 00:00:00.000' 
     ,'8.6' 

    UNION ALL 

    SELECT '2014-08-28 00:00:00.000' 
     ,'11.3' 
    ) 
SELECT * 
    ,CASE 
     WHEN Length < 1 
      THEN '< 1' 
     WHEN Length BETWEEN 1 
       AND 4 
      THEN '1-4' 
     WHEN Length BETWEEN 5 
       AND 10 
      THEN '5-10' 
     WHEN Length BETWEEN 11 
       AND 15 
      THEN '11-15' 
     WHEN Length > 15 
      THEN '> 15' 
     END AS Length_Range 
FROM cte 
0

我理解你的問題。但您的答案已經在下面的鏈接中可用。

Click here

所以參照這個鏈接,並獲得更多的細節。