求助_数据脚本里如何提取指定字段的唯一内容作为报表的字段头?_产供销
学习交流 » 产供销 »数据脚本里如何提取指定字段的唯一内容作为报表的字段头?
|
|
| |
作者 |
|
1934184799 [个人空间] QQ名 河内-服装
    注册  2015-06-23 发贴数 472 精华贴 1 原创贴 0 来自 状态 正常
级别 会员 |
|
|
如题目所示: 将一维表格变成二维表格,并在数据脚本的报表里检索出来 类似于Excel的透视表 |
|
|
|
|
1934184799 [个人空间] QQ名 河内-服装
    注册  2015-06-23 发贴数 472 精华贴 1 原创贴 0 来自 状态 正常
级别 会员 |
|
|
---------------------------------------------------------------- -- Author :DBA_HuangZJ(發糞塗牆) -- Date :2014-07-17 16:21:37 -- Version: -- Microsoft SQL Server 2012 - 11.0.5058.0 (X64) -- May 14 2014 18:34:29 -- Copyright (c) Microsoft Corporation -- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor) -- ---------------------------------------------------------------- --> 测试数据:[huang] if object_id('[huang]') is not null drop table [huang] go create table [huang]([商品号] varchar(2),[订单数量] int) insert [huang] select 'S1',80 union all select 'S1',180 union all select 'S1',280 union all select 'S1',380 union all select 'S1',480 union all select 'S2',80 union all select 'S2',180 union all select 'S2',380 union ALL select 'S2',280 union all select 'S2',280 union all select 'S3',80 union all select 'S1',80 union all select 'S1',180 union all select 'S1',580 --------------开始查询-------------------------- declare @s nvarchar(4000) set @s='' Select @s=@s+','+quotename([订单数量])+'=count(case when [订单数量]='+quotename([订单数量],'''')+' then 1 else null end)' from [huang] group by [订单数量] exec('select [商品号]'+@s+' from [huang] group by [商品号]') ----------------结果---------------------------- /* 商品号 80 180 280 380 480 580 ---- ----------- ----------- ----------- ----------- ----------- ----------- S1 2 2 1 1 1 1 S2 1 1 2 1 0 0 S3 1 0 0 0 0 0 */ |
|
|
|
|
1934184799 [个人空间] QQ名 河内-服装
    注册  2015-06-23 发贴数 472 精华贴 1 原创贴 0 来自 状态 正常
级别 会员 |
|
|
动态转置的问题解决了,但在数据报表里,如何动态定义表头? |
|
|
|
|
1934184799 [个人空间] QQ名 河内-服装
    注册  2015-06-23 发贴数 472 精华贴 1 原创贴 0 来自 状态 正常
级别 会员 |
|
|
老师说用 set_field_count 一个变量好了 |
|
|
|
|
|
|