SQL简单查询问题!

zhjian6 2004-06-17 05:55:12
我有一个表
tableA
userid uyear umonth uday



四个字段都是int,用户来记录用户登录用的!

select userid,count(userid) as count2 from temped where umonth=2 group by userid order by userid

这条语句,可以输入2月份,各用户登录的次数!

现在,想2,3,4,5,6 月份一起输入 该如何写?
得到的结果希望是

userid count2(二月) count3(三月) count4(四月) count5(五月) count6(六月)
...全文
178 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2004-06-18
  • 打赏
  • 举报
回复
--建议用连接,效率高一些:

select a.userid,
b.name,
sum(case umonth when 2 then 1 else 0 end) count2,
sum(case umonth when 3 then 1 else 0 end) count3,
sum(case umonth when 4 then 1 else 0 end) count4,
sum(case umonth when 5 then 1 else 0 end) count5,
sum(case umonth when 6 then 1 else 0 end) count6
from temped a join 用户信息表 b on a.userid=id
group by userid,姓名
order by userid
zhjian6 2004-06-18
  • 打赏
  • 举报
回复
谢谢,都是高手!
pbsql 2004-06-18
  • 打赏
  • 举报
回复
select userid,
(select 姓名 from 用户信息表 where ID=temped.userid) name,
sum(case umonth when 2 then 1 else 0 end) count2,
sum(case umonth when 3 then 1 else 0 end) count3,
sum(case umonth when 4 then 1 else 0 end) count4,
sum(case umonth when 5 then 1 else 0 end) count5,
sum(case umonth when 6 then 1 else 0 end) count6
from temped
group by userid order by userid
zhjian6 2004-06-18
  • 打赏
  • 举报
回复
谢谢各位,我用了
select userid,
sum(case umonth when 2 then 1 else 0 end) count2,
sum(case umonth when 3 then 1 else 0 end) count3,
sum(case umonth when 4 then 1 else 0 end) count4,
sum(case umonth when 5 then 1 else 0 end) count5,
sum(case umonth when 6 then 1 else 0 end) count6
from temped
group by userid order by userid
是可以的,但是想加点东西就出错
我另外有个表,是用户信息表,有ID,对应的姓名。
我现在想把输出的ID变成姓名,其它不变改怎么写?
zjcxc 元老 2004-06-17
  • 打赏
  • 举报
回复
--只有6个月,直接写就行啦,即使有12个月,也可以直接写嘛,反正比较固定

select userid
,[2月份]=sum(case umonth when 2 then 1 else 0 end)
,[3月份]=sum(case umonth when 3 then 1 else 0 end)
,[4月份]=sum(case umonth when 4 then 1 else 0 end)
,[5月份]=sum(case umonth when 5 then 1 else 0 end)
,[6月份]=sum(case umonth when 6 then 1 else 0 end)
from tableA
where uyear=2003 --统计那一年
group by userid
order by userid
zanglinfeng 2004-06-17
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql='select userid'
select @sql = @sql + ',sum(case umonth when ''' + a.umonth + '''then 1 else 0 end) as ' + a.umonth + ' '
from (select distinct umonth from [tableA]) a

set @sql =@sql + 'from [tableA] group by userid'
exec (@sql)


不错,赞同!
frankwong 2004-06-17
  • 打赏
  • 举报
回复
如果你的月份数是确定的话,用“风云”的方法可以,如果月份数不确定,即是列的数量不确定,建议使用一下方法:

declare @sql varchar(8000)
set @sql='select userid'
select @sql = @sql + ',sum(case umonth when ''' + a.umonth + '''then 1 else 0 end) as ' + a.umonth + ' '
from (select distinct umonth from [tableA]) a

set @sql =@sql + 'from [tableA] group by userid'
exec (@sql)
pbsql 2004-06-17
  • 打赏
  • 举报
回复
select userid,
sum(case umonth when 2 then 1 else 0 end) count2,
sum(case umonth when 3 then 1 else 0 end) count3,
sum(case umonth when 4 then 1 else 0 end) count4,
sum(case umonth when 5 then 1 else 0 end) count5,
sum(case umonth when 6 then 1 else 0 end) count6
from temped t
group by userid order by userid
内容概要:本文提出了一种基于改进扩散模型的高海拔地区新能源高波动出力场景生成方法,并提供了完整的Python代码实现。该方法针对高海拔地区风能、光伏等新能源出力波动剧烈、不确定性高的特点,通过优化扩散模型的结构与训练策略,有效捕捉历史数据的概率分布特征与时序相关性,从而生成高质量、多样化的出力场景。文中详细阐述了模型的数学推导、网络架构设计、损失函数优化及采样算法改进,并通过实验证明其在拟合精度、场景多样性与稳定性方面优于传统生成模型,为电力系统在高比例新能源接入下的规划、调度与风险评估提供了可靠的场景输入支持。; 适合人群:具备一定Python编程能力和机器学习基础,从事新能源发电预测、电力系统分析、智能优化、场景生成等方向研究的科研人员、高校研究生及工程技术人员。; 使用场景及目标:①用于高海拔地区风电、光伏出力的不确定性建模与多场景生成;②支撑含高渗透率新能源的电力系统随机优化调度、鲁棒决策与风险评估;③为相关学术研究、论文复现与算法改进提供可运行的技术方案与代码基础; 阅读建议:建议读者结合所提供的完整资源(代码、数据集、说明文档)进行实践操作,重点关注扩散模型的前向加噪与反向去噪过程的设计细节,以及如何将其适配于新能源时序数据的生成任务,通过参数调优与对比实验深入理解模型的生成机制与性能边界。
内容概要:本文围绕基于静态约束法的配电网电动汽车接入容量评估展开研究,提出了一种在新型电力系统背景下评估主动配电网对电动汽车承载能力的方法。研究通过构建数学模型,结合潮流计算与关键约束条件(如电压越限、线路过载等),量化分析配电网可承受的最大电动汽车充电负荷容量,旨在识别规模化电动汽车接入带来的潜在运行风险,并为电网规划与运行提供科学依据。文中配套提供了完整的Matlab代码实现,便于仿真验证与结果复现。此外,该研究与分布式光伏承载力评估、电动汽车可调能力分析等方向形成技术联动,展现了多主题协同的研究体系。; 适合人群:具备电力系统分析基础理论知识及Matlab编程能力的高校研究生、科研机构研究人员,以及从事新能源并网、智能配电网规划与运行等相关领域的工程技术人员。; 使用场景及目标:①用于学术研究中的模型复现与论文撰写支撑;②评估实际配电网中电动汽车大规模接入的可行性与安全边界,指导充电基础设施布局;③作为高校教学案例,帮助学生深入理解电网承载力评估的核心原理、建模方法与仿真技术; 阅读建议:建议结合文中提及的相关研究方向(如二阶锥规划、多面体聚合方法等)进行对比学习,充分利用所提供的Matlab代码与网盘资料开展仿真实验,重点关注约束条件的设定逻辑与潮流计算模块的实现细节,以深化对评估模型机理与工程应用价值的理解。

34,876

社区成员

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

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