我想做,如下的统计,日累计,月累计,年累计

xygxyg 2005-01-21 11:34:02
Item Color Quantity 日期
-------------------- -------------------- --------------------------
Table Blue 124 2004-01-01
Table Red 223 205-01-01
Chair Blue 101
Chair Red
基础表如上:我想做到如下的效果 :
日累计 月累计 年累计
table blue quantity合计 table blue quantity合计 table blue quantity合计
如何做到这种查询,请大家给予帮助.必须在一个界面上显示所有的累计.
...全文
99 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2005-01-21
  • 打赏
  • 举报
回复
select a.*
,b.月累计,b.月
,c.年累计,c.年
from(
select Item,Color
,日累计=sum(Quantity)
,日=convert(char(10),日期,120)
from 表
group by Item,Color,convert(char(10),日期,120)
)a,(
select Item,Color
,月累计=sum(Quantity)
,月=convert(char(7),日期,120)
from 表
group by Item,Color,convert(char(7),日期,120)
)b,(
select Item,Color
,年累计=sum(Quantity)
,年=convert(char(10),日期,120)
from 表
group by Item,Color,convert(char(4),日期,120)
)c
where a.Item=b.Item and a.Color=b.Color
and a.Item=c.Item and a.Color=c.Color
and a.日 like b.月+'-__'
and b.月 like c.年+'-__' --写错别名
zjcxc 元老 2005-01-21
  • 打赏
  • 举报
回复
select a.*
,b.月累计,b.月
,c.年累计,c.年
from(
select Item,Color
,日累计=sum(Quantity)
,日=convert(char(10),日期,120)
from 表
group by Item,Color,convert(char(10),日期,120)
)a,(
select Item,Color
,月累计=sum(Quantity)
,月=convert(char(7),日期,120)
from 表
group by Item,Color,convert(char(7),日期,120)
)b,(
select Item,Color
,年累计=sum(Quantity)
,年=convert(char(10),日期,120)
from 表
group by Item,Color,convert(char(4),日期,120)
)c
where a.Item=b.Item and a.Color=b.Color
and a.Item=c.Item and a.Color=c.Color
and a.日 like b.月+'-__'
and b.月 like a.年+'-__'

34,575

社区成员

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

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