【问题标题】:Split records into multiple rows based on range provided in column using sql server使用sql server根据列中提供的范围将记录拆分为多行
【发布时间】:2018-08-22 00:59:58
【问题描述】:

我希望使用 sql server 根据列中提供的范围将记录拆分为多行。请提供帮助。

示例源数据集如下 -

account_no    range
abc_111       2001…2010

预期目标数据集如下 -

account_no         range
abc_111             2001
abc_111             2002
abc_111             2003
abc_111             2004
abc_111             2005
abc_111             2006
abc_111             2007
abc_111             2008
abc_111             2009
abc_111             2010

此外,附件图片中显示了源数据和预期目标数据。

【问题讨论】:

  • 另一个例子 - 源数据集 Account_no Range abc 2001...2010 xyz 301...315 xyz 401...403 预期目标数据集 Account_no Range abc 2001 abc 2002 abc 2003 abc 2004 abc 2005 abc 2006 abc 2007 abc 2008 abc 2009 abc 2010 xyz 301 xyz 302 xyz 303 xyz 304 xyz 305 xyz 401 xyz 402 xyz 403

标签: mysql sql split


【解决方案1】:

您需要一个类似数字的表格来拆分数据。您可以自己构建一个:

select sd.account_no, substring_index(range, '...', 1) + n.n - 1
from sourcedata sd join
     (select 1 as n union all
      select 2 union all
      . . .
      select 10
     ) n
     on n.n >= 1 and
        n.n <= substring_index(range, '...', -1) - substring_index(range, '...', 1);

【讨论】:

  • 感谢您的回复。我的每个帐户都有不同数量的范围。它可以有任何数字,例如10 或 5 或 100。
  • 源数据集 Account_no Range abc 2001...2010 xyz 301...315 xyz 401...403 预期目标数据集 Account_no Range abc 2001 abc 2002 abc 2003 abc 2004 abc 2005 abc 2006 abc 2007 abc 2008 abc 2009 abc 2010 xyz 301 xyz 302 xyz 303 xyz 304 xyz 305 xyz 401 xyz 402 xyz 403
  • @Angelia 。 . .你只需要一个足够大的数字表。
猜你喜欢
  • 2019-11-03
  • 1970-01-01
  • 2016-08-15
  • 2023-03-30
  • 2016-04-10
  • 1970-01-01
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
相关资源
最近更新 更多