- 16
- 0
- 约1.34万字
- 约 26页
- 2017-06-05 发布于河南
- 举报
sql行列转换方法整理(国外英文资料)
sql行列转换方法整理
-- line shift
Original: name subject score
Chinese 80
Three mathematics 90
Zhang SAN physics 85
Li 4 Chinese 85
Li 4 physics 82
Li 4 English 90
Li iv politics 70
Wang 5 English 90
Transformed table: name mathematical physical English language politics
Li: 4, 0, 82, 90, 85, 70
Wang 5, 0, 0, 0, 0, 0
Its 3, 3, 90, 85, 0, 80, 0
Example:
Create table cj -- create table cj
(
ID Int IDENTITY (1, 1) not null - creates the column ID and adds 1 each time the new record is added
Name Varchar (50),
The Subject Varchar (50),
The Result Int,
Primary key (ID) - defines the primary key of the table cj
);
- the Truncate table cj
Select * from cj
Insert into cj
Select three, Chinese, 80 union all
Select three, math, 90 union all
Select three, physics, 85 union all
Select li 4, Chinese, 85 union all
Select li 4, physics, 82 union all
Select li 4, English, 90 union all
Select li 4, politics, 70 union all
Select wang 5, English, 90
Line conversion
Declare @ SQL varchar (8000).
Set @sql = Select Name as the Name
The Select @sql = @sql + , sum is then Result else 0 end [ + Subject +]
From the select distinct Subject from cj, the name of all the only subjects is listed
Select @sql = @sql + from cj group by name
The Exec (@ SQL)
Line conversion -- merge
Original: class number
One, one
One, two
One, three
Two, one
Two, two
Three, one
Converted: class number
1, 1, 2, 3
2 1, 2,
3 1
Example:
Create table ClassNo -- Create a table ClassNo
(
ID Int IDENTITY (1, 1) not null - creates the column ID and adds 1 each time the new record is added
Class Varchar (50) - Class column
Number Varchar (50), - the student Number column
Primary Key (ID) - the Key that defines the ID as the table ClassNo
);
- the Truncate Table ClassNo
-- Select * from ClassNo
Insert Into ClassNo
Select 1, 1 Union all
Union all Select 1, 2
Select 1, 3 Union all
Select 2, 1 Union all
Union all Select 2, 2
Select 3, 1
Create a merged function
- Drop Function KFReturn
Create Function KFReturn (@class Varchar (50))
Retu
您可能关注的文档
- ADS原函数 ARX后函数(国外英文资料).doc
- ajax同步异步的差别(国外英文资料).doc
- AIX 卷组、逻辑卷、物理卷的操纵敕令(国外英文资料).doc
- android_makefile(国外英文资料).doc
- AIX基础操纵(国外英文资料).doc
- ansys命令流----前后处理和求解常用命令之求解与后处理(国外英文资料).doc
- AOA重点难点整顿(国外英文资料).doc
- Apache的多种缓存设置方法(国外英文资料).doc
- apache allow&deny详解(国外英文资料).doc
- AI罕见的各类小题目处理方法(国外英文资料).doc
- 2025年北京市门头沟区中考一模英语试题.docx
- 2025年北京市门头沟区中考二模英语试题.docx
- 2025年北京市丰台区中考二模英语试题.docx
- 2025年中考英语考前冲刺模拟卷 (北京专用) 解析卷.docx
- 2025年中考英语考前冲刺模拟卷 (北京专用) 原卷.docx
- 2025年肩颈按摩仪行业社媒趋势数据分析.docx
- 2025年人身险行业信用回顾与2026年展望.docx
- 合同法买卖合同培训课件.ppt
- 2025年全球食物系统与SDG研究报告-食物供给、食者健康、生态平衡的整体治理.docx
- 2025下半年四川乐山市川投峨眉铁合金(集团)有限责任公司对考前自测高频考点模拟试题最新.docx
最近下载
- 制鞋技术之成型后段技术培训教材01.pdf VIP
- 磁共振温度图像重建方法、装置、设备、介质及程序产品.pdf VIP
- 金田JTE 388系列变频器使用手册.pdf
- 人教版(PEP)2026-2027学年四年级英语下册教学计划(及进度表).docx
- 东北师范大学2021-2022学年第1学期《线性代数》期末考试试卷(A卷)及标准答案.docx
- 新生儿科消防安全知识培训.pptx VIP
- 第五章+中国的地理差异+课件-2023-2024学年八年级地理下学期人教版.pptx VIP
- 第五章+中国的地理差异+第1课时+课件-2023-2024学年八年级地理下学期人教版.pptx VIP
- 【地理】中国的地理差异第1课时课件-2025-2026学年人教版地理八年级下册.pptx VIP
- 第五章+中国的地理差异+第1课时课件2022-2023学年人教版地理八年级下册.pptx VIP
原创力文档

文档评论(0)