降低价值

时间:2017-08-04 17:55:06

标签: sql-server

我的结果如下(Output1):

Function ReturnRowNumber()

Dim ws As Excel.Worksheet
Set ws = ActiveSheet

Dim scanPart As Variant
Dim sPart As String
Dim FoundRow As String

scanPart = InputBox("Scan the first part number", "Part Number")  'Get Part Number

sPart = scanPart

MsgBox sPart

Dim SearchRange As Range
Dim FindRow As Range
Set SearchRange = Range("A1", Range("A65536").End(xlUp))
Set FindRow = SearchRange.Find(123456789012#, LookIn:=xlValues, lookat:=xlWhole)
MsgBox FindRow.Row

End Function

但是,我希望它看起来如下:

 ID     Year    YearWeek    Week    ID1         (No column name)

479474  2017    201718      18          479474  201718
479474  2017    201719      19          NULL    NULL
479474  2017    201720      20          NULL    NULL
479474  2017    201721      21          479474  201721
479474  2017    201722      22          NULL    NULL
479474  2017    201723      23          479474  201723
479474  2017    201724      24          479474  201724

有人可以提出一些建议来制作代码,以便(无列名称)中的值被拖下来吗?值为201718,201721,20173和201724.

2 个答案:

答案 0 :(得分:2)

以此为旋转......

IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL 
DROP TABLE #TestData;

CREATE TABLE #TestData (
    ID INT NOT NULL,
    [Year] INT NOT NULL,
    YearWeek INT NOT NULL,
    [Week] INT NOT NULL,
    ID1 int NULL 
    );

INSERT #TestData (ID, Year, YearWeek, Week, ID1) VALUES
    (479474, 2017, 201718, 18, 479474),
    (479474, 2017, 201719, 19, NULL  ),
    (479474, 2017, 201720, 20, NULL  ),
    (479474, 2017, 201721, 21, 479474),
    (479474, 2017, 201722, 22, NULL  ),
    (479474, 2017, 201723, 23, 479474),
    (479474, 2017, 201724, 24, 479474);

--  SELECT * FROM #TestData td

--============================================================

SELECT 
    td.ID, td.Year, td.YearWeek, td.Week, td.ID1,
    CAST(SUBSTRING(MAX(CAST(td.YearWeek AS BINARY(4)) + CAST(td.ID1 AS BINARY(4))) OVER (ORDER BY td.YearWeek ROWS UNBOUNDED PRECEDING), 1, 4) AS INT)
FROM
    #TestData td;

答案 1 :(得分:0)

你的问题是在CTE之后的案例陈述中缺乏一个包罗万象。

更新您的Select语句,如下所示:

SELECT 
    Y.*,  
    CASE 
        WHEN Y.[ID1] IS NOT NULL THEN Y.[YearWeek] 
        ELSE NULL   ----> ADDED TO HAVE A NULL WHEN THERE'S NO VALUE IN PREVIOUS COLUMN
    END AS Column1 
FROM Y
ORDER BY Y.[Customer ID], Y.Year, Y.[YearWeek], Y.Week;