SQL 行转列和列转行2
DECLARE @T TABLE (columnName varchar(100) NOT NULL PRIMARY KEY);
INSERT INTO @T
SELECT columnName from QuanGuo_Credit..shixin_quanguo where isnull(columnName,'')!='' group by columnName;
DECLARE @SQL varchar(max)=N'';
SET @SQL=STUFF((SELECT N','+QUOTENAME(columnName) FROM @T
FOR XML PATH('')),1,1,N'');
--PRINT @SQL;
SET @SQL=N'select * from (select columnName, columnValue,oid from QuanGuo_Credit..shixin_quanguo where oid=1586141) as ord
pivot(max(columnValue) for columnName in('+@SQL+N'))as p';
--PRINT @SQL;
--EXEC (@SQL);
-------------------------------------------
declare @sql varchar(8000) set @sql = 'select oid,record_id'
select @sql = @sql + ',max(case columnName when ''' + columnName + ''' then columnValue else null end) [' + columnName +
']'from (
select distinct(b.columnName) from Table_Html a,Table_Columns b where a.table_name='深圳国税局-税务登记证失效公告'
and a.ID=b.tableID )as t
set @sql = @sql + 'from (
select columnName ,columnID,columnValue, oid=b.tableid,record_id from Table_ColumnValue A
left join Table_Columns B on A.columnID=B.ID where B.tableid in (select id from table_html where table_name=' +'''深圳国税
局-税务登记证失效公告'''+')
) as d group by oid,record_id order by oid'
print(@sql)
exec(@sql)
---------------------------------------------------
select oid ,max(case columnName when '案号' then columnValue else null end) [案号],max(case columnName when '被执行人的履行
情况' then columnValue else null end) [被执行人的履行情况],max(case columnName when '地域名称' then columnValue else null
end) [地域名称],max(case columnName when '发布时间' then columnValue else null end) [发布时间],max(case columnName when '立
案时间' then columnValue else null end) [立案时间],max(case columnName when '年龄' then columnValue else null end) [年
龄],max(case columnName when '企业法人/负责人姓名' then columnValue else null end) [企业法人/负责人姓名],max(case
columnName when '身份证号' then columnValue else null end) [身份证号],max(case columnName when '失信被执行人具体情形' then
columnValue else null end) [失信被执行人具体情形],max(case columnName when '未履行部分' then columnValue else null end) [未
履行部分],max(case columnName when '性别' then columnValue else null end) [性别],max(case columnName when '已履行部分' then
columnValue else null end) [已履行部分],max(case columnName when '执行法院' then columnValue else null end) [执行法院],max
(case columnName when '执行依据文号' then columnValue else null end) [执行依据文号],max(case columnName when '作出执行依据
单位' then columnValue else null end) [作出执行依据单位]
from (
select columnName, columnID, columnValue,oid=B.tableID --ROW_NUMBER()OVER(PARTITION BY columnID ORDER BY A.id)oid
from Table_ColumnValue A
left join Table_Columns B on A.columnID=B.ID
where B.tableID in(select top 5 id from Table_Html)
) as a group by oid
SQL 行转列和列转行2的更多相关文章
- 做图表统计你需要掌握SQL Server 行转列和列转行
说在前面 做一个数据统计和分析的项目,每天面对着各种数据,经过存储过程从源表计算汇总后需要写入中间结果表以提高数据使用效率,那么此时就需要用到行转列和列转行. 1.列转行 数据经过计算加工后会直接生成 ...
- SQL 行转列和列转行
SQL 行转列和列转行 行列互转,是一个经常遇到的需求.实现的方法,有case when方式和2005之后的内置pivot和unpivot方法来实现. 在读了技术内幕那一节后,虽说这些解决方案早就用过 ...
- sql中的行转列和列转行的问题
sql中的行转列和列转行的问题 这是一个常见的问题,也是一个考的问题 1.行转列的问题 简单实例 CREATE TABLE #T ( MON1 INT, MON2 INT, MON3 INT ) G ...
- sql 行专列 列转行 普通行列转换
转载:http://www.cnblogs.com/newwind521/archive/2010/11/25/1887203.html sql 行专列 列转行 普通行列转换 /* 标题:普通行列转换 ...
- (转载)重温SQL——行转列,列转行
原文地址:http://www.cnblogs.com/kerrycode/archive/2010/07/28/1786547.html 行转列,列转行是我们在开发过程中经常碰到的问题.行转列一般通 ...
- sql 行转列 PIVOT 列转行 UNPIVOT
原文:sql 行转列 PIVOT 列转行 UNPIVOT 一: 现有表一(t_table1),想转为表二(t_table2)的格式. 表一: 年 公司 收入 2013 公司1 12 2013 公司2 ...
- 【转载】SQL Server行转列,列转行
行转列,列转行是我们在开发过程中经常碰到的问题.行转列一般通过CASE WHEN 语句来实现,也可以通过 SQL SERVER 2005 新增的运算符PIVOT来实现.用传统的方法,比较好理解.层次清 ...
- 老生常谈之SQL Server (行转列,列转行)
Open the first article 在本文章中主要介绍以下内容: 1.静态行转列 2.静态列转行 3.动态行转列 4.动态列转行 1.静态行转列 --静态的行转列 --新建一个科目成绩表 - ...
- SQL行转列与列转行(转)
原文: http://blog.csdn.net/jx_870915876/article/details/52403472 add by zhj: 本文是以MySQL为例说明的,但其实它适用于所有关 ...
- SQL行转列(PIVOT)与列转行(UNPIVOT)简明方法
原文地址:https://www.cnblogs.com/linJie1930906722/p/6036714.html 在做数据统计的时候,行转列,列转行是经常碰到的问题.case when方式太麻 ...
随机推荐
- java1234初学maven
第一讲: maven maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. maven安装与下载: .确定jdk已经安装并且配置 .安装mav ...
- C#对Dictionary的按Value排序
使用List对其进行排序 using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp ...
- A Brief Review of Supervised Learning
There are a number of algorithms that are typically used for system identification, adaptive control ...
- selenium配置
1.firebug安装--火狐插件 2.firepath安装--火狐插件 3.
- VS2012+EF6+MYSQL配置
安装以下三个插件: 1. EFTools6.1.0ForVS2012.msi 2. mysql-connector-net-6.8.3.msi 3. Mysql for Visual Studio 1 ...
- 关于XML文档
很多人觉得XML很简单,但我想说其实一点也不简单,特别是在当今被各种web文档充斥的世界里有必要对XML进行一定程度的研究.下面的几篇博客将对XML技术进行简短的说明.
- 如何在tomcat中如何部署java EE项目
如何在tomcat中如何部署java EE项目 1.直接把项目复制到Tomcat安装目录的webapps目录中,这是最简单的一种Tomcat项目部署的方法,也是初学者最常用的方法.2.在tomcat安 ...
- Sliverlight 样式
UserControl 页面级样式UserControl.Resources style setter Property value. TargetType 应用的类型 使用 style static ...
- 学习SVG系列(4):SVG滤镜效果
注意:Internet Explorer和Safari不支持SVG滤镜 <defs>.<filter> 所有互联网的SVG滤镜定义在<defs>元素中,<fi ...
- linux 录制并回放终端会话
发现一个比较好玩的命令,然后这块做一下记录 以下内容复制来源于 LINUX shell 脚本攻略第二版 当你需要为别人在终端上演示某些操作或是需要准备一个命令行教程时,通常得一边手动输入命令一边演示, ...