【问题标题】:T-SQL - Padding data pointsT-SQL - 填充数据点
【发布时间】:2018-08-08 14:20:42
【问题描述】:

我在 Azure SQL 数据库的表中有一系列数据点。绘制时,点具有以下分布:

我要做的是添加填充,以便它们看起来像连续的线 - 或者至少比现在更连续。

我尝试在每个点之间添加点,但问题在于它都是相对的。如果你仔细观察,你会发现有些点是蓝色的,有些是暗红色的。蓝点是我添加的,但线看起来是一样的。

我正在寻找有关解决此问题的逻辑的建议。我想根据最近点之间的距离在每个数据点之间添加 x 个点...如果有意义的话。

【问题讨论】:

  • 你用什么来绘制这些?最好使用该工具使其连续,而不是在查询或表中添加点。
  • 请提供示例数据

标签: tsql azure-sql-database sql-server-2016


【解决方案1】:

我认为这行得通

declare @t table (x smallmoney primary key, y smallmoney);
declare @inc smallmoney = 1;
insert into @t(x, y) values 
                     (1,  1)
                  ,  (5,  3) 
                  ,  (8,  4)
                  ,  (10, 5) 
                  ,  (11, 6);
with cte as 
( select x, x as x0, y, y as y0, cnt = cast(1 as smallmoney)
       , lead(x) over (order by x) as nextX
       , lead(y) over (order by x) as nextY
  from @t t 
  union all 
  select x + @inc, x0, y + @inc/(nextX-x0)*(nextY-y0), y0, cnt+1, nextX, nextY 
  from cte t 
  where x + @inc < nextX
)
select * 
from cte t 
order by t.x;

【讨论】:

    【解决方案2】:

    我不确定这是最好的解决方案,但我认为您可以以此为基础。这是sqlfiddle

    SELECT x + COALESCE(((nextx-x)/10)*inc, 0) as x, y + COALESCE(((nexty-y)/10)*inc, 0) as y
    FROM 
    (SELECT x, y, nextx, nexty, inc.n + 0.0 as inc FROM 
     (SELECT x, y, lead(x) over (order by x) as nextx, lead(y) over (order by x) as nexty
     FROM points) p inner join (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) inc(n)
     ON nextx is not null or inc.n = 0
     ) a ORDER BY x
    

    这将在每个点之间增加 9 个点(总共 10 个点,包括“真实”点)。

    基本思想是,我对每一行使用lead 来获取下一个 x 和下一个 y,然后将其加入到值 0 到 9 的硬编码列表中。然后对于每个值,我将 x 递增nextx 和 x 之差的 1/10,y 增加 nexty 和 y 之差的 1/10。

    加入条件nextx is not null or inc.n = 0 是让我只加入inc(0) 到最后一个x 值(而不是加入10 次)。

    您可以更改我的硬编码值列表和硬编码10s 以​​不同方式递增。同样,如果您只需要整数,您可能需要进行一些更改,但原理是一样的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多