sql select问题

wchengyu 2007-02-13 09:18:54
要求:表a中有字段 物品名称,价格,销售数量,日期
sql语句,实现结果显示 第一周销售数量,第二周销售数量,或第一个月销售数量,第二个月销售数量
可以不显示其他内容。
...全文
353 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
rookie_one 2007-02-14
  • 打赏
  • 举报
回复
select 物品名称,sum(销售数量) as 销售数量,DATEPART (week,日期) 第几周
from a
group by 物品名称,DATEPART(week,日期)

--DATEPART(week,日期)返回本日期属于本年度的第几周,强烈建议lz看看下边这个
http://blog.joycode.com/ghj/archive/2003/11/11/6319.aspx
taxpayer 2007-02-14
  • 打赏
  • 举报
回复
select 物品名称,sum(销售数量) as 销售数量,datename(ww,日期) from a group by 物品名称,datename(ww,日期)
wchengyu 2007-02-13
  • 打赏
  • 举报
回复
谢谢楼上的,每月差不多可以实现了,那每周呢,str(week(日期))好像不行。
happy_fly_bird 2007-02-13
  • 打赏
  • 举报
回复
我想思想是一样的,你看了应该会明白
balance 营业额,yyt是几号窗口,person是员工代号
happy_fly_bird 2007-02-13
  • 打赏
  • 举报
回复
我有一个每月的营业额和总额,希望能给你帮助,是好久以前写的.

create table table1(tid int primary key , ttime datetime,balance int,yyt int,person int)

insert into table1 values(1,'2002-5-25',145774,1,1001)
insert into table1 values(2,'2003-4-29',14588,2,1001)
insert into table1 values(3,'2005-5-12',125664,3,1003)
insert into table1 values(4,'2002-1-22',645555,5,1002)

insert into table1 values(5,'2005-2-22',64457,1,1004)
insert into table1 values(6,'2005-8-22',64457,1,1004)
insert into table1 values(7,'2005-4-22',64457,1,1004)
insert into table1 values(8,'2005-12-22',64457,1,1004)
insert into table1 values(9,'2005-6-22',64457,2,1004)
insert into table1 values(10,'2005-9-22',64457,2,1004)
insert into table1 values(11,'2005-11-22',64457,4,1004)

go
select tid, year(ttime)as years,month(ttime) as months,balance,yyt,person into table3 from table1
go
select *from table3
go
select t3.*, (t3.months1+t3.months2+t3.months3+t3.months4+t3.months5) as sumprice,
(t3.months1+t3.months2+t3.months3+t3.months4+t3.months5)/5 as avgprice
from(select years ,
sum( case t.months when 1 then balance else 0 end) as months1,

sum(case t.months when 2 then balance else 0 end) as months2,
sum (case t. months when 3 then balance else 0 end )as months3,
sum (case t.months when 4 then balance else 0 end) as months4,
sum (case t.months when 5 then balance else 0 end) as months5
from table3 as t
group by t.years) as t3
wanttoknow2001 2007-02-13
  • 打赏
  • 举报
回复
select sum(价格*销售数量) as 销售数量,str(year(日期))+str(month(日期)) as 日期 from cost group by 类型,str(year(日期))+str(month(日期))

这是按月统计 不知道是不是你要的
chuifengde 2007-02-13
  • 打赏
  • 举报
回复
不懂你第一周是怎么定义的

34,587

社区成员

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

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