SQL Server - 复杂的动态数据透视列

2024-01-29

我有两个表“Controls”和“ControlChilds”

父表结构:

Create table Controls(
    ProjectID Varchar(20) NOT NULL,
    ControlID INT NOT NULL,
    ControlCode Varchar(2) NOT NULL,
    ControlPoint Decimal NULL,
    ControlScore Decimal NULL,
    ControlValue Varchar(50)
)

样本数据

ProjectID | ControlID | ControlCode | ControlPoint | ControlScore | ControlValue
P001        1           A            30.44            65           Invalid
P001        2           C            45.30            85           Valid

子表结构:

Create table ControlChilds(
    ControlID INT NOT NULL,
    ControlChildID INT NOT NULL,
    ControlChildValue Varchar(200) NULL 
)

样本数据

ControlID | ControlChildID | ControlChildValue
1           100              Yes
1           101              No
1           102              NA  
1           103              Others 
2           104              Yes
2           105              SomeValue

对于给定的 ProjectID,输出应位于单行中,首先是其所有控制值,然后是子控件值(基于 ControlCode(即)ControlCode_Child (1, 2, 3...),并且应该如下所示

另外,我尝试了这个 PIVOT 查询,我能够获取 ChildControls 表值,但我不知道如何获取 Controls 表值。

DECLARE @cols AS NVARCHAR(MAX);

DECLARE @query AS NVARCHAR(MAX);
select @cols = STUFF((SELECT 
                        distinct ',' + 
                        QUOTENAME(ControlCode + '_Child' + CAST(ROW_NUMBER() over(PARTITION BY ControlCode ORDER BY ControlChildID) AS Varchar(25)))
                      FROM Controls C
                      INNER JOIN ControlChilds CC 
                      ON C.ControlID = CC.ControlID 
                      FOR XML PATH(''), TYPE
                     ).value('.', 'NVARCHAR(MAX)') 
                        , 1, 1, '');

SELECT @query ='SELECT *
FROM
(
  SELECT   
    (ControlCode + ''_Child'' + CAST(ROW_NUMBER() over(PARTITION BY ControlCode ORDER BY ControlChildID) AS Varchar(25))) As Code,
        ControlChildValue
  FROM Controls AS C
  INNER JOIN ControlChilds AS CC ON C.ControlID = CC.ControlID
) AS t
PIVOT 
(
  MAX(ControlChildValue) 
  FOR Code IN( ' + @cols + ' )' +
' ) AS p ; ';

 execute(@query);

Output I am getting: enter image description here

谁能帮助我了解如何获取每个 ControlChilds 表值前面的 Controls 表值?


这里有点混乱,因为您有两个不同结构的表,并且您想要旋转多个列。我首先会开始编写查询的静态版本以使逻辑正确,然后完成编写动态版本的过程。

由于您想要旋转多个列,因此您需要取消旋转中的多个列Controls先表,后枢轴。您已将其标记为 SQL Server 2008,因此您可以使用CROSS APPLY取消旋转列。

我建议采取以下步骤。首先,取消旋转controls table:

select 
  ProjectId,
  col = ControlCode +'_'+col,
  val
from
(
  select 
    c.ProjectId,
    c.ControlCode,
    c.ControlPoint,
    c.ControlScore,
    c.ControlValue
  from controls c
) d
cross apply
(
  select 'ControlPoint', cast(controlpoint as varchar(10)) union all
  select 'ControlScore', cast(ControlScore as varchar(10)) union all
  select 'ControlValue', ControlValue
) c (col, val)

See SQL 摆弄演示 http://sqlfiddle.com/#!3/76778/12/0。这会将您的多行转换为多列,类似于:

| PROJECTID |            COL |     VAL |
|-----------|----------------|---------|
|      P001 | A_ControlPoint |   30.44 |
|      P001 | A_ControlScore |   65.00 |
|      P001 | A_ControlValue | Invalid |
|      P001 | C_ControlPoint |   45.30 |
|      P001 | C_ControlScore |   85.00 |
|      P001 | C_ControlValue |   Valid |

其次,获取数据ControlChilds表成类似的格式,但使用你的row_number()为每个孩子分配一个序列:

select 
  projectId,
  col = ControlCode+'_'+'Child'+cast(seq as varchar(10)),
  ControlChildValue
from
(
  select c.ProjectId,
    c.ControlCode,
    cc.ControlChildValue,
    row_number() over(partition by c.ProjectId, c.ControlCode
                      order by cc.ControlChildId) seq
  from controls c
  inner join controlchilds cc
    on c.controlid = cc.controlid
) d

See SQL 摆弄演示 http://sqlfiddle.com/#!3/76778/13/0。这将从该表中获取以下格式的数据:

| PROJECTID |      COL | CONTROLCHILDVALUE |
|-----------|----------|-------------------|
|      P001 | A_Child1 |               Yes |
|      P001 | A_Child2 |                No |
|      P001 | A_Child3 |                NA |
|      P001 | A_Child4 |            Others |
|      P001 | C_Child1 |               Yes |
|      P001 | C_Child2 |         SomeValue |

现在,您可以轻松使用UNION ALL在两个查询之间并应用 PIVOT 函数:

select ProjectId,
  A_ControlPoint, A_ControlScore, A_ControlValue,
  A_Child1, A_Child2, A_Child3, A_Child4,
  C_ControlPoint, C_ControlScore, C_ControlValue,
  C_Child1, C_Child2
from
(
  select 
    ProjectId,
    col = ControlCode +'_'+col,
    val
  from
  (
    select 
      c.ProjectId,
      c.ControlCode,
      c.ControlPoint,
      c.ControlScore,
      c.ControlValue
    from controls c
  ) d
  cross apply
  (
    select 'ControlPoint', cast(controlpoint as varchar(10)) union all
    select 'ControlScore', cast(ControlScore as varchar(10)) union all
    select 'ControlValue', ControlValue
  ) c (col, val)
  union all
  select 
    projectId,
    col = ControlCode+'_'+'Child'+cast(seq as varchar(10)),
    ControlChildValue
  from
  (
    select c.ProjectId,
      c.ControlCode,
      cc.ControlChildValue,
      row_number() over(partition by c.ProjectId, c.ControlCode
                        order by cc.ControlChildId) seq
    from controls c
    inner join controlchilds cc
      on c.controlid = cc.controlid
  ) d
) src
pivot
(
  max(val)
  for col in (A_ControlPoint, A_ControlScore, A_ControlValue,
              A_Child1, A_Child2, A_Child3, A_Child4,
              C_ControlPoint, C_ControlScore, C_ControlValue,
              C_Child1, C_Child2)
) piv;

See SQL 摆弄演示 http://sqlfiddle.com/#!3/76778/14/0.

现在您已经有了正确的逻辑,您可以将其转换为动态 SQL 版本:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(col) 
                    from 
                    (
                      select ControlCode,
                        col = ControlCode +'_'+col,
                        seq, 
                        so
                      from controls
                      cross apply
                      (
                        select 'ControlPoint', 0, 0 union all
                        select 'ControlScore', 0, 1 union all
                        select 'ControlValue', 0, 2 
                      ) c (col, seq, so)
                      union all
                      select  ControlCode,
                        col = ControlCode+'_'+'Child'+cast(seq as varchar(10)),
                        seq, 
                        3
                      from
                      (
                        select ControlCode, 
                          row_number() over(partition by c.ProjectId, c.ControlCode
                                                  order by cc.ControlChildId) seq
                        from controls c
                        inner join controlchilds cc
                          on c.controlid = cc.controlid
                      ) d
                    ) src
                    group by ControlCode, seq, col, so
                    order by ControlCode, so, seq
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @query = 'SELECT ProjectId, ' + @cols + ' 
            from 
            (
              select ProjectId,
                col = ControlCode +''_''+col,
                val
              from
              (
                select 
                  c.ProjectId,
                  c.ControlCode,
                  c.ControlPoint,
                  c.ControlScore,
                  c.ControlValue
                from controls c
              ) d
              cross apply
              (
                select ''ControlPoint'', cast(controlpoint as varchar(10)) union all
                select ''ControlScore'', cast(ControlScore as varchar(10)) union all
                select ''ControlValue'', ControlValue
              ) c (col, val)
              union all
              select 
                projectId,
                col = ControlCode+''_Child''+cast(seq as varchar(10)),
                ControlChildValue
              from
              (
                select c.ProjectId,
                  c.ControlCode,
                  cc.ControlChildValue,
                  row_number() over(partition by c.ProjectId, c.ControlCode
                                    order by cc.ControlChildId) seq
                from controls c
                inner join controlchilds cc
                  on c.controlid = cc.controlid
              ) d
            ) x
            pivot 
            (
                max(val)
                for col in (' + @cols + ')
            ) p '

exec sp_executesql @query;

See SQL 摆弄演示 http://sqlfiddle.com/#!3/3db46/37/0。我编写了动态版本,以按照您在示例中使用的顺序保留列。这可以通过使用排序顺序类型的值来完成。

最终结果为:

| PROJECTID | A_CONTROLPOINT | A_CONTROLSCORE | A_CONTROLVALUE | A_CHILD1 | A_CHILD2 | A_CHILD3 | A_CHILD4 | C_CONTROLPOINT | C_CONTROLSCORE | C_CONTROLVALUE | C_CHILD1 |  C_CHILD2 |
|-----------|----------------|----------------|----------------|----------|----------|----------|----------|----------------|----------------|----------------|----------|-----------|
|      P001 |          30.44 |          65.00 |        Invalid |      Yes |       No |       NA |   Others |          45.30 |          85.00 |          Valid |      Yes | SomeValue |
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SQL Server - 复杂的动态数据透视列 的相关文章

随机推荐

  • Elasticsearch - 我需要 JDBC 驱动程序吗?

    Aim 将我的 Elasticsearch 服务器与 SQL 数据库中的新数据和过期数据同步 Issue 我可以通过两种截然不同的方法来实现这一目标 但我不知道哪种方法更好 我也可以pull使用 JDBC River 插件直接连接到 SQL
  • 如何使用 ctypes 读取多字节值

    我想使用 ctypes 结构和联合从字节对象中读取两个连续的 24 位值 这将是 6 个字节 不幸的是 ctypes 读取每个值 32 位 还 sizeof报告结构比预期大 Using pack 1没有带来预期的结果 具有最小结构和二进制示
  • Android GPS 查询位置数据不正确

    我没有为此使用模拟位置 事实上 代码上周运行良好 我有一个应用程序 它收集 GPS 数据并使用应用程序本身生成的 X Y 坐标输出谷歌地图链接 我不是 100 确定为什么它没有按应有的方式工作 但是当我请求应用程序根据手机提供的 GPS 位
  • 有没有办法让 CGAL 的折线简化适用于内部/共享边界?

    我一直在尝试借助此方法对属于地图的多边形进行线条简化CGAL指南 https doc cgal org latest Polyline simplification 2 index html 例如韩国 这是一个韩国截图 https i st
  • Ruby on Rails:从另一个模型调用实例方法

    我有一个比赛模型和一个团队模型 我想在保存比赛后运行一个实例方法 在团队模型内编写 这就是我所拥有的 team rb def goals sum unless goal count cache goal count a goals sum
  • 如何检查系统音频是否静音?

    I found 这个答案 https stackoverflow com a 40545562 15498建议使用 核心音频 dll API https www dllme com dll files coreaudioapi dll ht
  • GWT:如何使用 UIBinder 在 Anchor 中嵌入小部件

    我想在 UIBinder 中使用以下内容 以便我可以以编程方式设置href我的代码中的链接
  • Visual Studio C++ 中的 msmpi.dll 错误消息

    我正在研究 mpi 但我一直收到此错误消息 程序无法启动 因为您的计算机缺少 msmpi dll 尝试 重新安装程序以解决此问题 所以我的问题是 卸载Visual Studio并重新安装可以解决这个问题吗 或者我应该使用电脑修复工具包程序和
  • 从表单应用程序 c# 启动停止服务

    如何从 C 表单应用程序启动和停止 Windows 服务 添加参考System ServiceProcess dll 然后您可以使用服务控制器 http msdn microsoft com en us library yb9w7ytd c
  • 如何避免在 Apollo Server V2 的错误响应中将错误集合包装在错误对象中

    我们正在将 Apollo Graphql Server v1 项目迁移到 v2 我们注意到错误响应格式发生了变化 在 v2 中 响应中的错误列表包含在错误对象中 但是 在 v1 中 情况并非如此 我们希望有一个一致的标准 并且不在 v2 中
  • 什么是 Array.map(Function.call,Number)

    var array1 1 4 9 16 map1 array1 map Function call Number 为什么map1的输出是 0 1 2 3 这个map函数是做什么的 Array prototype map https deve
  • Android 周期性 JobService 永远不会启动

    我有一个 JobService 我想定期启动它 目前 为了进行测试 我正在使用一个简单的 public class SenderService extends JobService Override public boolean onSta
  • 使用 C# 和 BizTalk 将 Excel (xlsx) 转换为 XML

    我已经查看了该论坛上有关类似问题的大多数主题 但尚未找到我正在寻找的内容 我正在尝试编写一个管道组件BizTalk 2013 R2使用 C 简单地转换传入的Excel 2010 xlsx文件到它的裸 基本 XML 表示形式 我不想针对它运行
  • 警告:mysql_num_rows() 期望参数 1 是给定的资源、对象[重复]

    这个问题在这里已经有答案了 q SELECT FROM tbl quevote WHERE que id qid and voteby uid result mysqli gt query q or die mysqli error mys
  • asp.net 下拉列表和视图状态

    我有一个下拉列表 可以触发自动回发并触发 SelectedIndexChanged 更改事件 我已将 viewstate 设置为 true 但由于某种原因 所选值在回发之间不会保留 我已经使用了下拉列表数百次 但似乎无法弄清楚为什么会发生这
  • Angular2 - 如何将字符串枚举与 *ngIf 一起使用

    我该如何通过enum当我使用时到一个函数 ngIf在角度 我有以下代码 export enum RoleType User User Admin Admin 组件功能 public hasAccess role RoleType check
  • 负面清单索引? [复制]

    这个问题在这里已经有答案了 我试图理解下面的代码 node list n for i in xrange 1 numnodes 1 tmp session newobject n append tmp link n 0 n 1 具体来说 我
  • 使用 Java 终止进程

    我想知道如何 杀死 已经启动的进程 我知道 Process API 但我不确定 是否可以使用它来 杀死 已经运行的进程 例如 firefox exe 等 如果可以使用 Process API 您能指出我吗 正确的方向 如果没有 还有哪些其他
  • 通过查询字符串将对象列表传递给 MVC 控制器

    我遇到了一种情况 需要将对象列表传递给 MVC 控制器 但我不确定如何在查询字符串中格式化它 我想要这样做的原因是因为这不是一个 Web 应用程序 它是一个通过查询字符串接受数据并将其路由到执行工作的控制器的 Web 服务 因此 给定一个名
  • SQL Server - 复杂的动态数据透视列

    我有两个表 Controls 和 ControlChilds 父表结构 Create table Controls ProjectID Varchar 20 NOT NULL ControlID INT NOT NULL ControlCo