- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章table 行转列的sql详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1、要求 。
1 创建数据表 。
CREATE TABLE [dbo].[StuScore]( 。
[stuid] [int] NOT NULL.
[subject] [nvarchar](30) NULL.
[score] [decimal](5, 1) NULL 。
) 。
2 插入测试数据 。
stuid subject score 。
3 chinese 76.0 。
3 math 73.0 。
4 chinese 82.0 。
5 chinese 66.0 。
5 math 93.0 。
6 chinese 67.0 。
7 math 83.0 。
8 chinese 77.0 。
8 math 84.0 。
3 行转列后的结果 。
stuid chinese math 。
3 76.0 73.0 。
4 82.0 0.0 。
5 66.0 93.0 。
6 67.0 0.0 。
7 0.0 83.0 。
8 77.0 84.0 。
二 、分析 。
1 行转列,一个重点就是怎么样知道有多少列,怎么样创建这些列?我们可以先把这个问题搁置,而假设这些列是已知的。 例如示例数据中,可以先假设subject的数据[chinese,math]是已知的,这样问题就简化了许多 。
2 当已知了chinese,math后,我们至少要先得到转换后的tabel结构 。
如下,
select stuid, 0 as chinese, 0 as math from dbo.StuScore 。
结果如下 。
stuid chinese math 。
3 0 0 。
3 0 0 。
4 0 0 。
5 0 0 。
5 0 0 。
6 0 0 。
7 0 0 。
8 0 0 。
8 0 0 。
3 接着就需要往这个数据集中去填充chinese, math的数据 。
select stuid.
case subject when 'chinese' then score else 0 end as chinese.
case subject when 'math' then score else 0 end as math 。
from dbo.StuScore 。
结果如下:
stuid chinese math 。
3 76.0 0.0 。
3 0.0 73.0 。
4 82.0 0.0 。
5 66.0 0.0 。
5 0.0 93.0 。
6 67.0 0.0 。
7 0.0 83.0 。
8 77.0 0.0 。
8 0.0 84.0 。
4 细心的读者会发现步骤3中的结果与我们想要的已经非常接近了,只需再做一个sum()处理,就OK了 。
select stuid.
sum(case subject when 'chinese' then score else 0 end ) as chinese.
sum(case subject when 'math' then score else 0 end ) as math 。
from dbo.StuScore group by stuid 。
得到的正是我们想要的结果 。
stuid chinese math 。
3 76.0 73.0 。
4 82.0 0.0 。
5 66.0 93.0 。
6 67.0 0.0 。
7 0.0 83.0 。
8 77.0 84.0 。
是不是现在就已经完成了呢?答案是否定的。前面我们已经说过,是为了简化问题,在假设已经知道了subject数据的情况下,这么处理的,实际上subject的数据是可变的,未知的,接下来就是要解决这个问题了 。
5 要获取subject的数据其实很简单 。
select distinct subject from dbo.StuScore 。
获取以后怎样得到case subject when 'chinese' then score else 0 end 这种语句?
可以根据subject的值去动态的组sql语句 。
看下面的一段代码 。
declare @sql varchar(2000) 。
set @sql='' 。
select @sql =@sql+ ',case subject when '''+subject+''' then 1 else 0 end as ' + subject 。
from (select distinct subject from dbo.StuScore) as sub 。
print @sql 。
message打印的信息如下:
,case subject when 'chinese' then 1 else 0 end as chinese,case subject when 'math' then 1 else 0 end as math 。
6 最后我们就需要将前面步骤综合起来,得到最终的sql 。
declare @sql varchar(2000) 。
set @sql='select stuid' 。
select @sql =@sql+ ',sum(case subject when '''+subject+''' then score else 0 end) as ' + subject 。
from (select distinct subject from dbo.StuScore) as sub 。
set @sql=@sql + ' from dbo.StuScore group by stuid' 。
exec(@sql) 。
stuid chinese math 。
3 76.0 73.0 。
4 82.0 0.0 。
5 66.0 93.0 。
6 67.0 0.0 。
7 0.0 83.0 。
8 77.0 84.0 。
至此,整个分析过程和结果就都出来了.
初试写文章, 多包涵,指正.
最后此篇关于table 行转列的sql详解的文章就讲到这里了,如果你想了解更多关于table 行转列的sql详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
猫f1.txt阿曼维沙尔阿杰贾伊维杰拉胡尔曼尼什肖比特批评塔夫林现在输出应该符合上面给定的条件 最佳答案 您可以在文件读取循环中设置一个计数器并打印它, 计数=0 读取行时做 让我们数一数++ if
我正在尝试查找文件 1 和文件 2 中的共同行。如果公共(public)行存在,我想写入文件 2 中的行,否则打印文件 1 中的非公共(public)行。fin1 和 fin2 是这里的文件句柄。它读
我有这个 SQL 脚本: CREATE TABLE `table_1` ( `IDTable_1` int(11) NOT NULL, PRIMARY KEY (`IDTable_1`) );
我有 512 行要插入到数据库中。我想知道提交多个插入内容是否比提交一个大插入内容有任何优势。例如 1x 512 行插入 -- INSERT INTO mydb.mytable (id, phonen
如何从用户中选择user_id,SUB(row, row - 1),其中user_id=@userid我的表用户,id 为 1、3、4、10、11、23...(不是++) --id---------u
我曾尝试四处寻找解决此问题的最佳方法,但我找不到此类问题的任何先前示例。 我正在构建一个基于超本地化的互联网购物中心,该区域分为大约 3000 个区域。每个区域包含大约 300 个项目。它们是相似的项
preg_match('|phpVersion = (.*)\n|',$wampConfFileContents,$result); $phpVersion = str_replace('"','',
我正在尝试创建一个正则表达式,使用“搜索并替换全部”删除 200 个 txt 文件的第一行和最后 10 行 我尝试 (\s*^(\h*\S.*)){10} 删除包含的前 10 行空白,但效果不佳。 最
下面的代码从数据库中获取我需要的信息,但没有打印出所有信息。首先,我知道它从表中获取了所有正确的信息,因为我已经在 sql Developer 中尝试过查询。 public static void m
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我试图在两个表中插入记录,但出现异常。您能帮我解决这个问题吗? 首先我尝试了下面的代码。 await _testRepository.InsertAsync(test); await _xyzRepo
这个基本的 bootstrap CSS 显示 1 行 4 列: Text Text Text
如果我想从表中检索前 10 行,我将使用以下代码: SELECT * FROM Persons LIMIT 10 我想知道的是如何检索前 10 个结果之后的 10 个结果。 如果我在下面执行这段代码,
今天我开始使用 JexcelApi 并遇到了这个:当您尝试从特定位置获取元素时,不是像您通常期望的那样使用sheet.getCell(row,col),而是使用sheet.getCell(col,ro
我正在尝试在我的网站上开发一个用户个人资料系统,其中包含用户之前发布的 3 个帖子。我可以让它选择前 3 条记录,但它只会显示其中一条。我是不是因为凌晨 2 点就想编码而变得愚蠢? query($q)
我在互联网上寻找答案,但找不到任何答案。 (我可能问错了?)我有一个看起来像这样的表: 我一直在使用查询: SELECT title, date, SUM(money) FROM payments W
我有以下查询,我想从数据库中获取 100 个项目,但 host_id 多次出现在 urls 表中,我想每个 host_id 从该表中最多获取 10 个唯一行。 select * from urls j
我的数据库表中有超过 500 行具有特定日期。 查询特定日期的行。 select * from msgtable where cdate='18/07/2012' 这将返回 500 行。 如何逐行查询
我想使用 sed 从某一行开始打印 n 行、跳过 n 行、打印 n 行等,直到文本文件结束。例如在第 4 行声明,打印 5-9,跳过 10-14,打印 15-19 等 来自文件 1 2 3 4 5 6
我目前正在执行验证过程来检查用户的旧密码,但问题是我无法理解为什么我的查询返回零行,而预期它有 1 行。另一件事是,即使我不将密码文本转换为 md5,哈希密码仍然得到正确的答案,但我不知道为什么会发生
我是一名优秀的程序员,十分优秀!