有个sql查询语句请教下。

lqsmn613 2012-11-22 08:38:06
情况是这样的,有个表有四个属性,分为名称,年份,月份,数量 name,year,month,num
我现在是要查询一年中12个月的数量明细。名称是固定的10个值,需要查询出的结果如下:
name,year,一月份num,二月份num,三月份num.......十二月份num

请问这个该咋整。
...全文
297 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lqsmn613 2012-11-22
  • 打赏
  • 举报
回复
引用 8 楼 TravyLee 的回复:
引用 7 楼 lqsmn613 的回复: 引用 2 楼 TravyLee 的回复:SQL code? 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 if object_id('test')……
多谢多谢!!终于成功了!
习惯性蹭分 2012-11-22
  • 打赏
  • 举报
回复

group by name,year
--改为 group by left(name,5),year试一下
  • 打赏
  • 举报
回复
引用 7 楼 lqsmn613 的回复:
引用 2 楼 TravyLee 的回复:SQL code? 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 if object_id('test') is not n……
你可以先把那个字段的数据处理一下 不要贴图片
lqsmn613 2012-11-22
  • 打赏
  • 举报
回复
引用 2 楼 TravyLee 的回复:
SQL code?



123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263

if object_id('test') is not nulldrop table test go create tabl……


多谢,你的方法很管用。
另外,我还有个问题请教下,实际情况具体如表内所示,这是最原始的那张表内的实际数据,这都是查询的同一年同一月的数据,我现在需要在之前所说的功能之外,还需要将6和7两行的数据合并为一个数据显示为一行即可。
这种情况的话需要怎么处理呢?再次感谢!
  • 打赏
  • 举报
回复
引用 5 楼 sb727182111 的回复:
引用 2 楼 TravyLee 的回复:SQL code? 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 if object_id('test') is not n……
多见几次你就知道了 没别的办法
sb727182111 2012-11-22
  • 打赏
  • 举报
回复
引用 2 楼 TravyLee 的回复:
SQL code? 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 if object_id('test') is not nulldrop table test go create tabl……
为啥我第一反应是用循环写呢- - 太失败了, 没学过计算机,现在要用,硬伤啊
-Tracy-McGrady- 2012-11-22
  • 打赏
  • 举报
回复

select name,year,
max(case month when 1 then num esle 0 end)一月份num,
max(case month when 2 then num esle 0 end)二月份num,
max(case month when 3 then num esle 0 end)二月份num,
.....
from tbName
group by name,year

--行列转化
sb727182111 2012-11-22
  • 打赏
  • 举报
回复
这个啊 坐等大神,反正我写出来基本是惨不忍睹了,行转换为啥不用EXEL呢
  • 打赏
  • 举报
回复
if object_id('test') is not null
drop table test
go
create table test
(
	name varchar(10),
	year int,
	month int,
	num int
)
go
insert test
select 'test01',2012,1,20 union all
select 'test01',2012,2,20 union all
select 'test01',2012,2,20 union all
select 'test01',2012,3,20 union all
select 'test01',2012,4,20 union all
select 'test01',2012,4,20 union all
select 'test01',2012,4,20 union all
select 'test01',2012,5,20 union all
select 'test01',2012,5,20 union all
select 'test01',2012,6,20 union all
select 'test01',2012,7,20 union all
select 'test01',2012,7,20 union all
select 'test01',2012,7,20 union all
select 'test01',2012,7,20 union all
select 'test01',2012,7,20 union all
select 'test01',2012,8,20 union all
select 'test01',2012,9,20 union all
select 'test01',2012,10,20 union all
select 'test01',2012,11,20 union all
select 'test01',2012,12,20 
go

select
	name,
	year,
	sum(case when month=1 then num else 0 end) as [1月份num],
	sum(case when month=2 then num else 0 end) as [2月份num],
	sum(case when month=3 then num else 0 end) as [3月份num],
	sum(case when month=4 then num else 0 end) as [4月份num],
	sum(case when month=5 then num else 0 end) as [5月份num],
	sum(case when month=6 then num else 0 end) as [6月份num],
	sum(case when month=7 then num else 0 end) as [7月份num],
	sum(case when month=8 then num else 0 end) as [8月份num],
	sum(case when month=9 then num else 0 end) as [9月份num],
	sum(case when month=10 then num else 0 end) as [10月份num],
	sum(case when month=11 then num else 0 end) as [11月份num],
	sum(case when month=12 then num else 0 end) as [12月份num]
from
	test
group by
	name,year

/*
name       year        1月份num      2月份num      3月份num      4月份num      5月份num      6月份num      7月份num      8月份num      9月份num      10月份num     11月份num     12月份num
---------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
test01     2012        20          40          20          60          40          20          100         20          20          20          20          20

(1 行受影响)


*/
lqsmn613 2012-11-22
  • 打赏
  • 举报
回复
大清早的没人吗?????自顶一下。
内容概要:本文围绕基于Basisformer模型的时间序列锂离子电池SOC(State of Charge,荷电状态)预测展开研究,利用PyTorch框架实现深度学习模型的构建与训练。通过将历史充放电数据作为输入,Basisformer能够有效捕捉电池状态的动态变化特征,提升SOC预测精度。文中详细介绍了模型结构设计、数据预处理流程、训练策略及实验结果分析,并与传统方法进行对比,验证了该方法在复杂工况下的优越性与鲁棒性。该研究不仅展示了Basisformer在时序建模中的潜力,也为电池管理系统提供了高精度的状态估计解决方案。; 适合人群:具备一定Python编程基础和深度学习理论知识,熟悉PyTorch框架,从事电池管理系统、新能源汽车或智能预测方向研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于电动汽车、储能系统等领域的电池SOC高精度实时估算;②为电池健康管理(BMS)提供可靠的状态输入;③推动深度学习在时间序列预测中的实际落地,提升现有预测模型的泛化能力与稳定性; 阅读建议:建议读者结合标题为【锂电池SOC估计】【PyTorch】基于Basisformer时间序列锂离子电池SOC预测研究(python代码实现)的资源,重点研读所提供的Python代码,深入理解数据处理方式与模型网络结构的设计思路,尝试调整超参数以观察对预测性能的影响,从而全面掌握Basisformer在时序建模中的优势、适用边界及工程化实现路径。

34,876

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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