SQL Server:如何根据边框值填充时间轴记录

时间:2015-11-18 15:14:31

标签: sql sql-server sql-server-2012

我想知道在时间轴中使用过去的最后一个可用值填充记录的最简单方法是什么?直到它发生变化?

 snapshot  val
 ----------------
 201201    NULL
 201202    NULL
 201203    10
 201204    NULL
 201405    NULL
 201406    20
 201407    30
 201408    NULL
 201409    NULL
 201410    NULL
 201411    NULL
 201412    40
 201501    NULL
 ...

预期的输出将是:

 snapshot  val
 ---------------
 201201    NULL
 201202    NULL
 201203    10
 201204    10
 201405    10
 201406    20
 201407    30
 201408    30
 201409    30
 201410    30
 201411    30
 201412    40
 201501    40

1 个答案:

答案 0 :(得分:3)

<强> SqlFiddle Demo

@Override
public void columnMarginChanged(final ChangeEvent e)
{
    super.columnMarginChanged(e);

    if (isEditing())
    {
        removeEditor();
    }
    TableColumn resizingColumn = null;
    if (tableHeader != null)
    {
        resizingColumn = tableHeader.getResizingColumn();
    }
    if (resizingColumn != null)
    {
        if (autoResizeMode == AUTO_RESIZE_OFF)
        {
            resizingColumn.setPreferredWidth(resizingColumn.getWidth());
        }
        else
        { 
            SwingUtilities.invokeLater(new Runnable()
            {

                @Override
                public void run()
                {
                    doLayout();
                    repaint();
                }
            });
        }
    }
    else
    {
        resizeAndRepaint();
    }
}

<强>输出

SELECT [snapshot],
       [val],
       MAX( [val] ) OVER (PARTITION BY grp ORDER BY [snapshot]) as [val],
       grp
FROM (                          
      SELECT [snapshot], 
             [val], 
             COUNT([val]) OVER (ORDER BY [snapshot]) grp       
      FROM YourTable
 ) T