sqlserver 按季度查询

huoyutu 2008-08-20 02:12:54
我现在用的sqlserver2005的版本,我如何查询第一季度到第三季度的数据,并且要按季度统计数据
我最后查询出来的结果格式如下

名字 价格 第一季度 第二季度 第三季度
...全文
606 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
huoyutu 2008-08-20
  • 打赏
  • 举报
回复
select xx.ped_mcname,xx.ped_breedid,xx.ped_breedname,sum(xx.ped_auditing_price) price,
case datepart(qq,xx.ped_coltime)
when 1 then '第一季度'
when 2 then '第二季度'
when 3 then '第三季度' end
from pc_priceexp_data xx group by xx.ped_mcname,xx.ped_breedid,xx.ped_breedname,
datepart(qq,xx.ped_coltime)

但是这样把所有年份的都查出来了
我只要2008年的第一季度到第三季度的
和2007年的第一季度到第三季度的数据

要进行同比
-狙击手- 2008-08-20
  • 打赏
  • 举报
回复
select 名字,价格,case datepart(qq,datecol) when 1 then '第一季度' when 2 then '第二季度' when 3 then '第三季度' end
from ta
huoyutu 2008-08-20
  • 打赏
  • 举报
回复
补充一下
上面的是经过行转列之后的格式

行转列就不需要了
只要查询出如下格式就行

名称1 价格 第一季度
名称1 价格 第二季度
名称1 价格 第三季度
名称2 价格 第一季度
名称2 价格 第二季度
名称2 价格 第三季度
-狙击手- 2008-08-20
  • 打赏
  • 举报
回复
select 名字,价格,
第一季度 =sum(case when datepart(qq,datecol)= 1 then ... else... end),
第二季度 =sum(case when datepart(qq,datecol)= 2 then ... else... end),
第三季度 =sum(case when datepart(qq,datecol)= 3 then ... else... end)
from ta
group by 名字,价格
liangCK 2008-08-20
  • 打赏
  • 举报
回复 1
select 名字,sum(价格),
sum(case when month(时间) between 1 and 3 then 数量 else 0 end) 第一季度,
sum(case when month(时间) between 4 and 6 then 数量 else 0 end) 第二季度
..
from tb
group by 名字
guoli0813 2008-08-20
  • 打赏
  • 举报
回复
兄弟,请你把你的例子贴出来

34,590

社区成员

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

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