■■■SQL选择按日记录 订单表 请帮忙■■■

fanqingfeng 2005-02-28 09:31:43
订单表ORDER:
ID ProductPay(商品金额)CarryPay(运费) Type Time
-----------------------------
1 200 20 发货 2005-1-1 11:52:23
2 200 50 发货 2005-1-1 11:56:23
3 150 12 发货 2005-1-2 13:14:56
4 620 45 收货 2005-1-1 12:25:02
5 120 10 收货 2005-1-3 22:12:30
。。。


想计算每天发货和收获的金额:
Time Out(发货金额)OutCarry(发货运费)In(收货金额)InCarry(收获运费)
----------------------------------------
2005-1-1 400 70 620 45
2005-1-2 150 12 0 0
2005-1-3 0 0 120 10
。。。。


谢谢 急用
...全文
80 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fanqingfeng 2005-02-28
  • 打赏
  • 举报
回复
非常感谢!
jinjazz 2005-02-28
  • 打赏
  • 举报
回复
Create Table 表(ID varchar(10),ProductPay integer ,CarryPay integer,Type varchar(10),[time] datetime)
--插入数据
insert into 表
select '1','200','20','发货','2005-1-1 11:52:23' union
select '2','200','50','发货','2005-1-1 11:56:23' union
select '3','150','12','发货','2005-1-2 13:14:56' union
select '4','620','45','收货','2005-1-1 12:25:02' union
select '5','120','10','收货','2005-1-3 22:12:30'
select * from 表
--测试语句

select convert(char(10),time,120),
sum(case when type='发货' then productpay else 0 end)[发货总金额],
sum(case when type='发货' then CarryPay else 0 end)[发货总运费],
sum(case when type='发货' then 1 else 0 end)[发货次数],
sum(case when type='收货' then productpay else 0 end)[收货总金额],
sum(case when type='收货' then CarryPay else 0 end)[收货总运费],
sum(case when type='收货' then 1 else 0 end)[收货次数]
from 表
group by convert(char(10),time,120)
--删除测试环境
Drop Table 表
fanqingfeng 2005-02-28
  • 打赏
  • 举报
回复
你好 谢谢你的解答 请问能否进一步统计到 每天的发货次数 和收货次数?

即2005-1-1 的发货次数是2 收货次数是1
fanqingfeng 2005-02-28
  • 打赏
  • 举报
回复
谢谢
jinjazz 2005-02-28
  • 打赏
  • 举报
回复
create view test as
select convert(char(10),time,120) [time],
sum(case when type='发货' then productpay else 0 end)[发货总金额],
sum(case when type='发货' then CarryPay else 0 end)[发货总运费],
sum(case when type='收货' then productpay else 0 end)[收货总金额],
sum(case when type='收货' then CarryPay else 0 end)[收货总运费]
from 表
group by convert(char(10),time,120)
fanqingfeng 2005-02-28
  • 打赏
  • 举报
回复
结果正确
请问 能否生成视图表呢?
或者自动生成另外一个基本表保存
jinjazz 2005-02-28
  • 打赏
  • 举报
回复
--建立测试环境
Create Table 表(ID varchar(10),ProductPay integer ,CarryPay integer,Type varchar(10),[time] datetime)
--插入数据
insert into 表
select '1','200','20','发货','2005-1-1 11:52:23' union
select '2','200','50','发货','2005-1-1 11:56:23' union
select '3','150','12','发货','2005-1-2 13:14:56' union
select '4','620','45','收货','2005-1-1 12:25:02' union
select '5','120','10','收货','2005-1-3 22:12:30'
select * from 表
--测试语句

select convert(char(10),time,120),
sum(case when type='发货' then productpay else 0 end)[发货总金额],
sum(case when type='发货' then CarryPay else 0 end)[发货总运费],
sum(case when type='收货' then productpay else 0 end)[收货总金额],
sum(case when type='收货' then CarryPay else 0 end)[收货总运费]
from 表
group by convert(char(10),time,120)
--删除测试环境
Drop Table 表
子陌红尘 2005-02-28
  • 打赏
  • 举报
回复
select
Time = convert(char(10),Time,120),
out = sum(case when type = '发货' then ProductPay else 0 end),
OutCarry = sum(case when type = '发货' then CarryPay else 0 end),
in = sum(case when type = '收货' then ProductPay else 0 end),
InCarry = sum(case when type = '收货' then CarryPay else 0 end)
from
ORDER
group by
convert(char(10),Time,120)

34,590

社区成员

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

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