【问题标题】:Move Data from One Table To Another Table With New Layout使用新布局将数据从一个表移动到另一个表
【发布时间】:2017-11-24 13:23:41
【问题描述】:

请注意,我之前问过这个问题,但意识到我遗漏了一些非常重要的信息,并且觉得最好删除原来的问题并发布一个新问题。我向大家道歉............

我有一个包含以下列的表格:

ID
Name
2010/Jan
2010/Jan_pct
2010/Feb
2010/Feb_pct
.....
.....
2017/Nov
2017/Nov_pct

然后是这样的列,用于迄今为止的每个月/年组合(希望这是有道理的)。但请注意:并不是每个月/年的组合都存在。可能存在差距或缺少月份/年份。例如,我知道 2017/Jan、2017/Feb 丢失了,并且可能缺少任何数字。我只是不想列出每一列,而是大致了解布局。

此外,数据库中没有一行,但一个名称/ID 可以有多行,并且 ID 不是身份,但可以是任意数字。

为了了解表格的外观,这里有一些示例数据(请注意,我只添加了两个年/月组合,但有几十个不一定每个月/年都有一个)

ID     Name    2010/Jan    2010/Jan_Pct  2010/Feb    2010/Feb_Pct
10     Gold    81          0.00123       79          0.01242
134    Silver  82          0             75          0.21291
678    Iron    987         1.53252       1056        2.9897

您可以想象,这不是最好的设计,因为您需要每个月添加两个新列。所以我创建了一个具有以下定义的新表

ID - float, 
Name - varchar(255), 
Month - varchar(3), 
Year - int, 
Value - int, 
Value_Pct - float

我正在试图弄清楚如何将现有数据从旧表移动到新表设计中。

任何帮助将不胜感激.....

【问题讨论】:

  • 如果将月份和年份合并到一个日期列中可能会更好。除此之外,之前的 unpivot 是您所需要的。不知道为什么你删除了那个,因为 99.999% 的工作已经在那个中完成了。
  • 诚实的回答是因为我以为我打错了要求,但我保存了信息。是我自己的愚蠢,没有完全理解并因此而感到困惑,但我明白你现在在说什么。我为这个错误道歉......

标签: sql-server tsql


【解决方案1】:

您可以使用unpivot 运算符来获得您需要的内容,并增加一个步骤,即合并unpivot 运算符返回的额外行。

示例数据设置:

鉴于目标表具有int 数据类型的value 列和float 数据类型的value_pct,我对现有数据表遵循相同的数据类型指南。

create table dbo.data_table
    (
        ID float not null
        , [Name] varchar(255) not null
        , [2010/Jan] int null 
        , [2010/Jan_Pct] float null
        , [2010/Feb] int null
        , [2010/Feb_Pct] float null
    )

insert into dbo.data_table
values (10, 'Gold', 81, 0.00123, 79, 0.01242)
    , (134, 'Sliver', 82, 0, 75, 0.21291)
    , (678, 'Iron', 987, 1.53252, 1056, 2.9897)

答案:

--combine what was the "value" row and the "value_pct" row
--into a single row via summation
select a.ID
, a.[Name]
, a.[Month]
, a.[Year]
, sum(a.value) as value
, sum(a.value_pct) as value_pct
from (
    --get the data out of the unpivot with one row for value
    --and one row for value_pct. 
    select post.ID
    , post.[Name]
    , substring(post.col_nm, 6, 3) as [Month]
    , cast(substring(post.col_nm, 1, 4) as int) as [Year]
    , iif(charindex('pct', post.col_nm, 0) = 0, post.value_prelim, null) as value
    , iif(charindex('pct', post.col_nm, 0) > 0, post.value_prelim, null) as value_pct
    from (
        --cast the columns that are currently INT as Float so that 
        --all data points can fit in one common data type (will separate back out later)
        select db.ID
        , db.[Name]
        , cast(db.[2010/Jan] as float) as [2010/Jan]
        , db.[2010/Jan_Pct]
        , cast(db.[2010/Feb] as float) as [2010/Feb]
        , db.[2010/Feb_Pct]
        from dbo.data_table as db
        ) as pre
    unpivot (value_prelim for col_nm in (
                                        [2010/Jan]
                                        , [2010/Jan_Pct]
                                        , [2010/Feb]
                                        , [2010/Feb_Pct]
                                        --List all the rest of the column names here
                                        )
            ) as post
    ) as a
group by a.ID
, a.[Name]
, a.[Month]
, a.[Year]

最终输出:

【讨论】:

    猜你喜欢
    • 2012-04-07
    • 1970-01-01
    • 2011-04-12
    • 2013-10-18
    • 1970-01-01
    • 2017-05-20
    • 2018-11-03
    • 1970-01-01
    相关资源
    最近更新 更多