SQL 行转列

benben_tong 2012-02-20 05:06:36
现有表[Hong_Props],表中的字段如下:

PropID PropGameType PropArrea type PropTime Props PropsCoun

1 1 1 金币 2012-02-11 道具A 24
2 2 2 金币 2012-02-11 道具B 15
3 1 1 绑定金币 2012-02-12 道具C 14
4 2 1 金币 2012-02-12 道具D 2
5 2 2 金币 2012-02-13 道具D 50
6 1 2 绑定金币 2012-02-14 道具B 9
7 2 2 绑定金币 2012-02-15 道具E 10
8 1 1 金币 2012-02-15 道具A 20


先我要得到的查询效果为:

日期 道具A 道具B 道具C 道具D 道具E 累计
2012-02-11 24 15 0 0 0 39
2012-02-12 0 0 14 2 0 16
2012-02-13 0 0 0 50 0 50
2012-02-14 0 9 0 0 10 19
2012-02-15 20 0 0 0 10 30


急求高手帮忙!
急求高手帮忙!
急求高手帮忙!
急求高手帮忙!
...全文
138 11 打赏 收藏 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaobinbinbin 2012-02-21
  • 打赏
  • 举报
回复
declare @sql varchar(max)
set @sql='select Props'
select @sql=@sql+',sum(case convert(varchar(10),PropTime,120) when '''+date+''' then PropsCoun else 0 end)['+date+']'
from (
select convert(varchar(10),PropTime,120)as date from Hong_Props
group by convert(varchar(10),PropTime,120)
) t
select @sql=@sql+',sum(PropsCoun) as qq from Hong_Props group by props'
exec (@sql)
  • 打赏
  • 举报
回复
/*
现有表[Hong_Props],表中的字段如下:

PropID PropGameType PropArrea type PropTime Props PropsCoun

1 1 1 金币 2012-02-11 道具A 24
2 2 2 金币 2012-02-11 道具B 15
3 1 1 绑定金币 2012-02-12 道具C 14
4 2 1 金币 2012-02-12 道具D 2
5 2 2 金币 2012-02-13 道具D 50
6 1 2 绑定金币 2012-02-14 道具B 9
7 2 2 绑定金币 2012-02-15 道具E 10
8 1 1 金币 2012-02-15 道具A 20


先我要得到的查询效果为:

日期 道具A 道具B 道具C 道具D 道具E 累计
2012-02-11 24 15 0 0 0 39
2012-02-12 0 0 14 2 0 16
2012-02-13 0 0 0 50 0 50
2012-02-14 0 9 0 0 10 19
2012-02-15 20 0 0 0 10 30


*/

--生成测试数据:
go
if OBJECT_ID('Hong_Props')is not null
drop table Hong_Props
go
create table Hong_Props(
PropID int,
PropGameType int,
PropArrea int,
PropTime date,
Props varchar(20),
PropsCoun int
)
go
insert Hong_Props
select 1 ,1 ,1 ,'2012-02-11' ,'道具A' ,24 union all
select 2 ,2 ,2 ,'2012-02-11' ,'道具B' ,15 union all
select 3 ,1 ,1 ,'2012-02-12' ,'道具C' ,14 union all
select 4 ,2 ,1 ,'2012-02-12' ,'道具D' ,2 union all
select 5 ,2 ,2 ,'2012-02-13' ,'道具D' ,50 union all
select 6 ,1 ,2 ,'2012-02-14' ,'道具B' ,9 union all
select 7 ,2 ,2 ,'2012-02-15' ,'道具E' ,10 union all
select 8 ,1 ,1 ,'2012-02-15' ,'道具A' ,20

declare @str varchar(1000)
set @str=''
select @str=@str+','+Props+
'=max(case when Props='+quotename(Props,'''')+' then PropsCoun else 0 end)'
from Hong_Props
group by Props
--print @str
select @str='select PropTime'+@str+',sum(PropsCoun) as 累计 from Hong_Props group by PropTime'
--print @str
exec (@str)

/*
PropTime 道具A 道具B 道具C 道具D 道具E 累计
2012-02-11 24 15 0 0 0 39
2012-02-12 0 0 14 2 0 16
2012-02-13 0 0 0 50 0 50
2012-02-14 0 9 0 0 0 9
2012-02-15 20 0 0 0 10 30
*/

--你这个查询也就是很标准的行列转换问题,动态实现的基本方法就是拼接查询语句
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 szstephenzhou 的回复:]

SQL code
create table Hong_Props(
PropID int,PropGameType int,PropArrea int,PropTime datetime,Props varchar(20),PropsCoun int
)
insert into Hong_Props
select 1 ,1 ,1 ,'2012-02-11' ,'道具A' ,24 union a……
[/Quote]

这个帖子做过了,难道是楼主从发的??其实你可以拿之前那个简单的修改一下就好了
中国风 2012-02-20
  • 打赏
  • 举报
回复
勿勿 2012-02-20
  • 打赏
  • 举报
回复
create table Hong_Props(
PropID int,PropGameType int,PropArrea int,PropTime datetime,Props varchar(20),PropsCoun int
)
insert into Hong_Props
select 1 ,1 ,1 ,'2012-02-11' ,'道具A' ,24 union all
select 2 ,2 ,2 ,'2012-02-11' ,'道具B' ,15 union all
select 3 ,1 ,1 ,'2012-02-12' ,'道具C' ,14 union all
select 4 ,2 ,1 ,'2012-02-12' ,'道具D' ,2 union all
select 5 ,2 ,2 ,'2012-02-13' ,'道具D' ,50 union all
select 6 ,1 ,2 ,'2012-02-14' ,'道具B' ,9 union all
select 7 ,2 ,2 ,'2012-02-15' ,'道具E' ,10 union all
select 8 ,1 ,1 ,'2012-02-15' ,'道具A' ,20
go


declare @str varchar(max)
set @str = 'select convert(varchar(10),PropTime,120) PropTime'
select @str = @str + ',sum(case when Props='''+Props+''' then PropsCoun else 0 end) ['+Props+']'
from(select Props from Hong_Props group by Props ) t
select @str = @str +',SUM(PropsCoun) as 累计'+ ' from Hong_Props group by PropTime'
print (@str)
exec(@str)




select convert(varchar(10),PropTime,120) PropTime,
sum(case when Props='道具A' then PropsCoun else 0 end) [道具A],
sum(case when Props='道具B' then PropsCoun else 0 end) [道具B],
sum(case when Props='道具C' then PropsCoun else 0 end) [道具C],
sum(case when Props='道具D' then PropsCoun else 0 end) [道具D],
sum(case when Props='道具E' then PropsCoun else 0 end) [道具E],
SUM(PropsCoun) as 累计 from Hong_Props group by PropTime



PropTime 道具A 道具B 道具C 道具D 道具E 累计
---------- ----------- ----------- ----------- ----------- ----------- -----------
2012-02-11 24 15 0 0 0 39
2012-02-12 0 0 14 2 0 16
2012-02-13 0 0 0 50 0 50
2012-02-14 0 9 0 0 0 9
2012-02-15 20 0 0 0 10 30

(5 行受影响)

勿勿 2012-02-20
  • 打赏
  • 举报
回复
declare @str varchar(max)
set @str = 'select convert(varchar(10),PropTime,120) PropTime'
select @str = @str + ',sum(case when Props='''+Props+''' then PropsCoun else 0 end) ['+Props+']'
from(select Props from Hong_Props group by Props ) t
select @str = @str +',SUM(PropsCoun) as 累计'+ ' from Hong_Props group by PropTime'
print (@str)
exec(@str)



PropTime 道具A 道具B 道具C 道具D 道具E 累计
---------- ----------- ----------- ----------- ----------- ----------- -----------
2012-02-11 24 15 0 0 0 39
2012-02-12 0 0 14 2 0 16
2012-02-13 0 0 0 50 0 50
2012-02-14 0 9 0 0 0 9
2012-02-15 20 0 0 0 10 30

(5 行受影响)


---or

select convert(varchar(10),PropTime,120) PropTime,sum(case when Props='道具A' then PropsCoun else 0 end) [道具A],sum(case when Props='道具B' then PropsCoun else 0 end) [道具B],sum(case when Props='道具C' then PropsCoun else 0 end) [道具C],sum(case when Props='道具D' then PropsCoun else 0 end) [道具D],sum(case when Props='道具E' then PropsCoun else 0 end) [道具E],SUM(PropsCoun) as 累计 from Hong_Props group by PropTime


PropTime 道具A 道具B 道具C 道具D 道具E 累计
---------- ----------- ----------- ----------- ----------- ----------- -----------
2012-02-11 24 15 0 0 0 39
2012-02-12 0 0 14 2 0 16
2012-02-13 0 0 0 50 0 50
2012-02-14 0 9 0 0 0 9
2012-02-15 20 0 0 0 10 30

(5 行受影响)






叶子 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 benben_tong 的回复:]

引用 1 楼 acherat 的回复:

SQL code

--PropTime Props PropsCoun
declare @sql varchar(8000)
set @sql = 'select convert(varchar(10),PropTime,120) PropTime'
select @sql = @sql + ',sum(case when convert……
[/Quote]
按时间分组的行转列,小三写得应该没有问题呀。
勿勿 2012-02-20
  • 打赏
  • 举报
回复
create table Hong_Props(
PropID int,PropGameType int,PropArrea int,PropTime datetime,Props varchar(20),PropsCoun int
)
insert into Hong_Props
select 1 ,1 ,1 ,'2012-02-11' ,'道具A' ,24 union all
select 2 ,2 ,2 ,'2012-02-11' ,'道具B' ,15 union all
select 3 ,1 ,1 ,'2012-02-12' ,'道具C' ,14 union all
select 4 ,2 ,1 ,'2012-02-12' ,'道具D' ,2 union all
select 5 ,2 ,2 ,'2012-02-13' ,'道具D' ,50 union all
select 6 ,1 ,2 ,'2012-02-14' ,'道具B' ,9 union all
select 7 ,2 ,2 ,'2012-02-15' ,'道具E' ,10 union all
select 8 ,1 ,1 ,'2012-02-15' ,'道具A' ,20
go


declare @str varchar(max)
set @str = 'select convert(varchar(10),PropTime,120) PropTime'
select @str = @str + ',sum(case when Props='''+Props+''' then PropsCoun else 0 end) ['+Props+']'
from(select Props from Hong_Props group by Props ) t
select @str = @str + ' from Hong_Props group by PropTime'
exec(@str)


PropTime 道具A 道具B 道具C 道具D 道具E
---------- ----------- ----------- ----------- ----------- -----------
2012-02-11 24 15 0 0 0
2012-02-12 0 0 14 2 0
2012-02-13 0 0 0 50 0
2012-02-14 0 9 0 0 0
2012-02-15 20 0 0 0 10

(5 行受影响)


benben_tong 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 acherat 的回复:]

SQL code

--PropTime Props PropsCoun
declare @sql varchar(8000)
set @sql = 'select convert(varchar(10),PropTime,120) PropTime'
select @sql = @sql + ',sum(case when convert(varchar(10),PropTime,120)=……
[/Quote]

嗯,你看问题要求了、、 新手求解、
勿勿 2012-02-20
  • 打赏
  • 举报
回复
AcHerat 2012-02-20
  • 打赏
  • 举报
回复

--PropTime Props PropsCoun
declare @sql varchar(8000)
set @sql = 'select convert(varchar(10),PropTime,120) PropTime'
select @sql = @sql + ',sum(case when convert(varchar(10),PropTime,120)='''+date+''' then PropsCoun else 0 end) ['+date+']'
from(select convert(varchar(10),PropTime,120) date from Hong_Props group by convert(varchar(10),PropTime,120)) t
select @sql = @sql + ' from Hong_Props group by convert(varchar(10),PropTime,120)'
exec(@sql)
内容概要:本文介绍了一个基于Python的滚动轴承故障诊断项目,采用迁移学习(TL)结合SqueezeNet网络(TL-SqueezeNet)实现智能诊断。项目通过将一维振动信号经短时傅里叶变换转化为二维时频图,输入改进的SqueezeNet模型进行训练与分类。模型利用预训练权重冻结骨干网络、替换分类头,并结合类别权重、数据增强、标签平滑和分阶段微调策略,提升小样本与类别不均衡条件下的诊断性能。系统实现了从原始信号读取、滑动窗口分段、时频图生成、数据集划分、模型训练评估到单文件推理的全流程自动化,支持CSV、TXT、NPY等多种格式,输出包括准确率、宏平均F1、混淆矩阵及各类别概率,增强了诊断结果的可解释性与工程实用性。; 适合人群:具备Python编程基础,熟悉PyTorch框架,从事工业设备故障诊断、信号处理或深度学习应用研究的研发人员、研究生及工程师(工作或研究年限1-3年)。; 使用场景及目标:①解决滚动轴承故障数据小样本、类别不均衡、工况多变导致的传统方法泛化能力差的问题;②构建轻量化、高精度、可部署于边缘设备的智能诊断模型;③实现端到端的自动化诊断流程,提升工业运维智能化水平;④通过可视化评估指标辅助人工复核与模型优化。; 阅读建议:此资源以实际项目代码为核心,强调工程实现与算法优化的结合,建议读者结合文档中的代码示例搭建环境运行,重点理解数据预处理逻辑、模型迁移策略与评估机制的设计思路,并尝试在自有数据上进行迁移与调优实践。
本资源提供重庆市2026年水系(线+面)空间分布数据,系统整理重庆市范围内河流、沟渠、水道、湖泊、水库等水系要素,包含可编辑MXD工程文件、标准Shapefile矢量文件以及标准成图TIF文件,可用于水文地理、水资源管理、生态环境及自然灾害等相关研究。 原始数据来源是Open Street Map(OSM),水系类型包括江、河、溪、水域、水库、河岸、河堤等等,可在shp文件属性表中自行查阅。 数据包含水系线和水系面两类矢量数据。线状水系用于表达河流、沟渠及其他线性水体的空间位置和形态;面状水系用于表达湖泊、水库及其他具有一定面积的水体分布,可较直观地反映重庆市河湖水系的空间格局。 标准Shapefile文件支持空间查询、属性编辑、长度与面积统计、河网密度分析、缓冲区分析及专题制图,可与行政区划、DEM、土地利用、人口、降水及灾害数据进行空间叠加。资源配套提供可编辑MXD工程文件,完成水系图层组织、符号配置、标注及地图版式设置,便于用户直接在ArcGIS中编辑和制图。 该数据可广泛应用于重庆市水资源管理、河湖空间格局研究、洪涝灾害分析、生态环境评价、流域研究及国土空间规划等领域,可为区域水系特征分析及相关GIS空间研究提供基础数据支撑。 同时提供标准成图TIF文件,可直接用于科研论文、项目报告、专题地图及教学展示。整体数据具有线面数据配套、空间分布直观、格式完整、GIS兼容性好等特点,可满足重庆市水系空间分析、专题制图及科研应用需求。
源码下载地址: https://pan.quark.cn/s/7c5ebe7e1146 专门用于处理和播放监控系统生成的MP4视频文件的软件工具即为监控专用MP4文件播放器。此类播放器通常配备特殊功能,能够解析和播放那些因编码格式特殊或包含特定行业标准而在常规多媒体播放器上无法打开的MP4文件。作为常见类型,H264文件播放器得到了广泛应用,因为H264(也称为AVC,即Advanced Video Coding)是当前监控录像最常用的视频编码格式之一,其高效的压缩比率能在维持良好画质的条件下减小文件体积。 监控视频的编码方法通常与一般娱乐视频存在差异,可能包含更复杂的元数据或专有的加密技术,旨在保障数据的安全性和隐私性。因此,标准MP4播放器可能因无法识别这些特征而无法正常播放。针对这些特点进行优化的监控专用MP4文件播放器,则能够确保正确解码和播放相关文件。 描述中提及的"各种录像文件"可能涵盖以下几种情形: 1. 非标准分辨率:监控摄像头或许会采用非典型的分辨率,如320x240、720x480等,这些在标准播放器中可能无法得到支持。 2. 特殊帧率:监控录像或许会采用非常规的帧率,例如1fps、5fps等,这与一般视频的24fps、30fps有所不同。 3. 非线性时间轴:部分监控系统可能会进行时间戳重置,导致视频时间线呈现非线性,普通播放器可能无法正确处理。 4. 加密保护:为防止未授权的访问,监控录像可能被加密,需要特定的播放器才能进行解密。 5. 多通道音频:部分监控设备或许会包含多个音频通道,例如同时记录多路音频,普通播放器可能无法同时播放。 在提供的压缩包文件中,"下载说明.htm"或许包含了软件的安装指南、使用教程或注意事项,对于用户而言至关...

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧