sqlserver2005 行列转换实现方法
发布时间:2024-01-14 01:34:56
标签:sqlserver2005,行列转换
--Create Company Table
Create Table Company
(
ComID varchar(50) primary key,
ComName nvarchar(50) not null,
ComNumber varchar(50) not null,
ComAddress nvarchar(200),
ComTele varchar(50)
)
--Create Product Table
Create Table Product
(
ProductID varchar(50) primary key,
ComID varchar(50) not null,
ProName nvarchar(200) not null,
ProNumber int not null
)
select * from product
--insert into table value
insert Company select('58C0F3FD-7B98-4E74-A1A8-7B144FCB8707','CompanyOne','SH19991028','ShangHai','98765432112')
union all select('768B2E84-0AAB-4653-8F5B-5EF6165204DB','CompanyTwo','SH19991028','ShangHai','98765432113')
union all select('AAE86C36-C82B-421D-BC55-E72368B1DE00','CompanyThree','SH19991028','ShangHai','98765432114')
union all select('C672B359-C800-47DE-9BB4-6D0FC614594C','CompanyFour','SH19991028','ShangHai','98765432115')
union all select('FDBA8B3F-1851-4B73-9A20-A24AEF721AAE','CompanyFive','SH19991028','ShangHai','98765432116')
insert Product sleect('1598A60B-FCFD-4269-864B-CB999E8EA5CA','AAE86C36-C82B-421D-BC55-E72368B1DE00','SqlServer2005',500)
union all select('19D7BF2F-79FD-414E-B648-F105D4AB1EBB' ,'AAE86C36-C82B-421D-BC55-E72368B1DE00', 'Office', 400)
union all select('232B6109-C575-4316-A9BD-0C58F737BE7B' ,'FDBA8B3F-1851-4B73-9A20-A24AEF721AAE', 'SqlServer2005' ,200)
union all select('4F30E12C-7654-40CC-8245-DF1C3453FBC5' ,'768B2E84-0AAB-4653-8F5B-5EF6165204DB', 'Office', 400)
union all select('54C6E4C2-1588-43DF-B22C-0697A1E27DB0' ,'58C0F3FD-7B98-4E74-A1A8-7B144FCB8707', 'Office', 400)
union all select('551EB6CA-3619-4250-98A0-7231BB4C3D58' ,'FDBA8B3F-1851-4B73-9A20-A24AEF721AAE', 'SqlServer2000', 100)
union all select('5BAD331C-B6E4-440E-AC54-52CE13166843' ,'768B2E84-0AAB-4653-8F5B-5EF6165204DB', 'SqlServer2005', 1000)
union all select('5C039C53-2EE4-4D90-BA78-7A20CEC4935C' ,'58C0F3FD-7B98-4E74-A1A8-7B144FCB8707', 'Windows2000', 200)
union all select('673A8683-CD03-40D2-9DB1-1ADA812016E2' ,'58C0F3FD-7B98-4E74-A1A8-7B144FCB8707', 'WindowsXP', 100)
union all select('6B9F771B-46EA-4496-B1DA-F10CB53F6F62' ,'C672B359-C800-47DE-9BB4-6D0FC614594C', 'WindowsXP', 100)
union all select('770089B1-A80A-4F48-8537-E15BD00A99E7' ,'AAE86C36-C82B-421D-BC55-E72368B1DE00', 'WindowsXP', 100)
union all select('92EED635-5C61-468A-B19D-01AAC112D8A3' ,'FDBA8B3F-1851-4B73-9A20-A24AEF721AAE', 'SysBase', 100)
union all select('99195297-F7F0-4DCD-964E-CFB8A162B6D0' ,'768B2E84-0AAB-4653-8F5B-5EF6165204DB', 'Windows2008', 300)
union all select('9EF017C1-F8F0-49BC-A7BD-4DFFB6EA8037' ,'768B2E84-0AAB-4653-8F5B-5EF6165204DB', 'Windows2000', 200)
union all select('A31BCD44-7856-461F-A0FD-407DCA96E8A9' ,'C672B359-C800-47DE-9BB4-6D0FC614594C', 'SqlServer2005', 100)
union all select('A9B52E8F-129F-4113-A473-D4BDD2B3C09C' ,'768B2E84-0AAB-4653-8F5B-5EF6165204DB', 'WindowsXP' ,100)
union all select('AC228CA0-490C-4B3D-866D-154E771B2083' ,'58C0F3FD-7B98-4E74-A1A8-7B144FCB8707', 'Windows2008', 300)
union all select('BD0BA1D3-D1D2-4BC7-9EFD-78B1165060A0' ,'FDBA8B3F-1851-4B73-9A20-A24AEF721AAE', 'DB2', 200)
union all select('CAA71AEA-7130-4AB8-955E-B04EA35A178A' ,'FDBA8B3F-1851-4B73-9A20-A24AEF721AAE', 'Oracle', 100)
--This is Business pack .
--Using this function can using table's row
--to new table's column
declare @strSql varchar(1000)
declare @column varchar(50)
declare @columns varchar(200)
set @columns = ''
/*According to Cursor get new table column*/
declare varchar_cur cursor for
select distinct proname from product order by proname
open varchar_cur
fetch next from varchar_cur into @column
while @@fetch_status = 0
begin
set @columns = @columns + '[' + @column + '],'
fetch next from varchar_cur into @column
end
Close varchar_cur
Deallocate varchar_cur
/*Converted to the ranks of the use of pivot*/
set @columns = stuff(@columns,len(@columns),1,'')
set @strSql = 'select comname,' + @columns
set @strSql = @strSql + ' from '
set @strSql = @strSql + ' ('
set @strSql = @strSql + ' select comname,pronumber,proname from product'
set @strSql = @strSql + ' left join company on product.comid = company.comid '
set @strSql = @strSql + ' ) as temp'
set @strSql = @strSql + ' pivot '
set @strSql = @strSql + ' ( '
set @strSql = @strSql + ' sum(pronumber) '
set @strSql = @strSql + ' for proname in (' + @columns + ') '
set @strSql = @strSql + ' ) as Pivot_table'
exec(@strSql)


猜你喜欢
- 目录什么是引用?引用在数组和对象中的使用引用的传递引用的返回引用的取消总结什么是引用?在 PHP 中引用意味着用不同的名字访问同一个变量内容
- 如下所示:#!/usr/bin/env python#-*- coding: utf-8 -*-"""[0,
- Python not equal operator returns True if two variables are of same ty
- Keras运行迭代一定代数以后,速度越来越慢,经检查是因为在循环迭代过程中增加了新的计算节点,导致计算节点越来越多,内存被占用完,速度变慢。
- reflow是个神奇的东西,之前Realazy说到过这个reflow,我摘出其中的重点:在CSS规范中有一个渲染对象的概念,通常用一个盒子(
- 前言大家好!这个系列文章是W3CN 阿捷编写的。是一些制作过程中的心得和经验,希望对大家有点帮助。第一天开始制作符合标准的站点,第一件事情就
- 前言orztop是一款实时show full processlist的工具,我们可以实时看到数据库有哪些线程,执行哪些语句等。工具使用方便简
- 代码如下import sysfrom PyQt5.QtWidgets import QApplication, QWidgetclass E
- 本文实例讲述了python使用socket向客户端发送数据的方法。分享给大家供大家参考。具体如下:import socket, syspor
- 1.tensor张量与numpy相互转换tensor ----->numpyimport torcha=torch.ones([2,5
- CAS算法(compare and swap)CAS算法是一种有名的无锁算法。无锁编程,即不使用锁的情况下实现多线程之间的变量同步,也就是在
- 首先下载最新版本的python。www.python.org,目前版本为3.1。 接下来是安装,在windows下python的安装与其他应
- SQLPlus是进行Oracle操作的主要前台工具,用户名和密码分别为用户名和密码,连接ORACLE数据库可见,显示的比较混乱,可以通过以下
- OpenCVOpenCV 是计算机视觉领域最受欢迎的开源库,起初它由 C/C ++ 编写,现在用 Python 也能使用。OpenCV 可以
- 逐步回归的基本思想是将变量逐个引入模型,每引入一个解释变量后都要进行F检验,并对已经选入的解释变量逐个进行t检验,当原来引入的解释变量由于后
- 网上考试设计思路是怎样的?为了运行这个应用程序,我们需要在global.asa文件里进行设置数据库的连接。global.asa <&n
- 在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数。 2. 使用Function对象来进行返
- Django使用mysqlclient服务连接并写入数据库准备1.创建Django程序,安装Django服务(详情请看上上节)2.创建子应用
- 列表:list=[val1,val2]1.列表中的每一个元素都是可变的,有序的,可以被查看索引的。可变意味着可以对每个元素进行增删改查的操作
- python实现原图裁剪为固定尺寸小图的具体代码,供大家参考,具体内容如下讲解1、代码效果:实现原图裁剪为固定尺寸小图代码import nu