高分求解sql,也可以使用proc或view只要能解决就行!有点难度,请高手指点!

showmetoyou 2004-12-23 05:24:22
生成数据表(moblieuser)的脚本如下:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[moblieuser]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[moblieuser]
GO

CREATE TABLE [dbo].[moblieuser] (
[id] [float] NULL ,
[moblie] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
[password] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
[ordercode] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
[paytime] [smalldatetime] NULL ,
[status] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
[result] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
数据如下:(精简)
id moblie password ordercode paytime status result
1990.0 13500057554 699704 MOVIE 20041124 NULL 000000
1993.0 13500057554 699704 MOVIE 20041124 1 null
1995.0 13500057554 699704 MOVIE 20041124 1 000000
2051.0 13500337201 741611 MUSIC 20041124 NULL null
2067.0 13500337201 741611 MUSIC 20041124 NULL 000000
2030.0 13500337201 741611 MUSIC 20041124 1 000000
5735.0 13500337201 191406 MUSIC 20041205 NULL 000000
7268.0 13455760459 754181 MOVIE 20041210 NULL 000000
7284.0 13455760459 NULL MOVIE 20041210 1 null
7284.0 13455760459 NULL MUSIC 20041210 1 000000


条件:ordercode字段表示栏目代码, MOVIE为电影频道,订阅收费10元,music为音乐频道,订阅收费5元
status字段为状态标志,只有两个状态,null表示订阅,1表示退订
result字段为处理标志,只有两个状态,000000表示处理成功,null表示处理失败
举例:用户13500057554在20041124日订阅(status为null(is not 1))MOVIE成功(result为000000),20041200对应的收入加10元,
如果不成功,不加钱,如果用户退订MOVICE并且成功,收入减10元,退订不成功,收入不减!
目标:统计生成moblieuser表中三天时间内每天收入表(income)
paytime money
20041124 0 注释0=(10-0-10+0+5-5)
20041205 5
20041210 5 注释5=(10-0-5)

请各位大虾看看测试,测试,贴出解决方案!分数不够还可以加!
如果有不清楚的地方,直接加我msn:showmeself@hotmail.com或QQ:39197106!也欢迎加我交流!









...全文
145 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
showmetoyou 2004-12-24
  • 打赏
  • 举报
回复
欢迎交流!
showmetoyou 2004-12-23
  • 打赏
  • 举报
回复
select datename(yy,paytime)+right(('0'+datename(mm,paytime)),2)+right(('0'+datename(dd,paytime)),2) as 日期,
count(distinct moblie) as 用户数目,
sum((case ordercode when 'MSNR001' then 12 when '10057001' then 15 when 'LWZ' then 5 end)
* (case when status is null then 1 else -1 end)
* (case when result is null then 0 else 1 end)) as 总金额
from moblieuser
group by datename(yy,paytime)+right(('0'+datename(mm,paytime)),2)+right(('0'+datename(dd,paytime)),2)
order by datename(yy,paytime)+right(('0'+datename(mm,paytime)),2)+right(('0'+datename(dd,paytime)),2) DESC

这就是我项目中的初步sql,已成功!我明天再研究一下!中午揭帖!
showmetoyou 2004-12-23
  • 打赏
  • 举报
回复
谢谢!各位大虾!我测试一下!明天中午揭帖!等着加分吧!
vinsonshen 2004-12-23
  • 打赏
  • 举报
回复
这样试试吧:

select convert(varchar(10),paytime,120) as paytime,
income=sum(case when ordercode='MOVIE' and status is null and result='000000' then 10
when ordercode='MUSIC' and status is null and result='000000' then 5
when ordercode='MOVIE' and status =1 and result='000000' then -10
when ordercode='MUSIC' and status =1 and result='000000' then -5
else 0 end
)
from moblieuser
group by convert(varchar(10),paytime,120)
playyuer 2004-12-23
  • 打赏
  • 举报
回复
select paytime,
(select sum(b.val) from moblieuser where status is null and result = '0' and paytime = a.paytime)
-
(select sum(b.val) from moblieuser where status = '1' and result = '0' and paytime = a.paytime)

from moblieuser a
,(select 'MOVIE' as ordercode,10 as val
union all
select 'MUSIC',5) b
where a.ordercode = b.ordercode
group by paytime
order by paytime
子陌红尘 2004-12-23
  • 打赏
  • 举报
回复
select
paytime,
sum((case ordercode when 'MOVIE' then 10 when 'MUSIC' then 5 end) * (case when status is null then 1 else -1 end) * (case when result is null then 0 else 1 end)) as money
from
moblieuser
group by
paytime
order by
paytime
lsxaa 2004-12-23
  • 打赏
  • 举报
回复
select a.paytime,
sum(case when a.result is null then 0
else (case when a.status is null then b.val
when a.status=1 then -b.val
end
)
end) as money
from t a,(select 'MOVIE' as ordercode,10 as val union all
select 'MUSIC' as ordercode,5 as val ) b --改一点
where a.ordercode=b.ordercode
group by a.paytime
order by a.paytime
lsxaa 2004-12-23
  • 打赏
  • 举报
回复
我没测试,你自己测试吧
lsxaa 2004-12-23
  • 打赏
  • 举报
回复
select a.paytime,
sum(case when a.result is null then 0
else (case when a.status is null then b.val
when a.status=1 then -b.val
end
)
end) as money
from t a,(select 'MOVIE' as ordercode,10 as val union all
'MUSIC' as ordercode,5 as val ) b
where a.ordercode=b.ordercode
group by a.paytime
order by a.paytime

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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