请教这个SQL语句如何写?

cplusplus2007 2008-03-26 09:36:23
我有一个表,结构为

create table tblSalse(
id int not null,
time datetime not null,
salse int not null,
succ_ind tinyint not null
)

id表示某个用户
time字段表示这个用户发生salse的时间
salse表示订单销售额
succ_ind表示salse成功与否,1为成功,0为未成功

现在要计算某个用户在一个时间段内的订单总销售额与成功率,如果某个用户在这个时间段内没有订单发生,显示为0或者null,所以需要left join,能在一条SQL语句中实现吗?我是这样做的,觉得不好
约定starttime表示时间段的起始时间,endtime为时间段的结束时间

select t1.id,t1.sums,t2.succCount*100.0/t1.totalCount as succ_rate
from
(select id,sum(salse) as sums,count(succ_ind) as totalCount from tblSalse
where time between starttime and endtime) as t1
left join
(select id,count(succ_ind) as succCount from tblSalse where time between starttime and endtime) as t2
on t1.id=t2.id

我听人说left join很消耗资源,如何去掉left join而满足我的要求啊?
...全文
111 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
cplusplus2007 2008-03-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 qqhmitzk 的回复:]
在数据量不大情况下,影响不会很大
[/Quote]
嘿嘿,就是因为数据量特别大,几百万条了吧,运行起来,看那磁盘转得都心疼,CPU利用率一下就上去了
qqhmitzk 2008-03-26
  • 打赏
  • 举报
回复
在数据量不大情况下,影响不会很大
cplusplus2007 2008-03-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yyyyzzzz_2002 的回复:]
贴主的sql能运行么?
1.有集合函数,那么应该分组(group by )
2.派生表(伪视图)不正确,少了 一个括号,别名
3.在做除法时(t2.succCount*100.0/t1.totalCount ,应该考虑为0时出现异常
4.按照你的做法,应该这样写:
select t1.id,t1.sums,t2.succCount*100.0/isnull(t1.totalCount,1) as succ_rate
from
(select id,sum(salse) as sums,count(succ_ind) as totalCount from tblSalse
where time between starttime …
[/Quote]
楼上说得是,我忘记了,只是想举个列子说明一下问题,left join无法避免吗?
yyyyzzzz_2002 2008-03-26
  • 打赏
  • 举报
回复
贴主的sql能运行么?
1.有集合函数,那么应该分组(group by )
2.派生表(伪视图)不正确,少了 一个括号,别名
3.在做除法时(t2.succCount*100.0/t1.totalCount ,应该考虑为0时出现异常
4.按照你的做法,应该这样写:
select t1.id,t1.sums,t2.succCount*100.0/isnull(t1.totalCount,1) as succ_rate
from
(select id,sum(salse) as sums,count(succ_ind) as totalCount from tblSalse
where time between starttime and endtime group by id) as t1
left join
(select id,count(succ_ind) as succCount from tblSalse where succ_ind=1 and time between starttime and endtime group by id) as t2
on t1.id=t2.id )a

wzy_love_sly 2008-03-26
  • 打赏
  • 举报
回复
create  table tb(id int,name varchar(10))
insert into tb select 1,'a'
insert into tb select 2,'b'
insert into tb select 3,'c'
insert into tb select 4,'d'

select sum(case when name='a' then 1 when name='b' then 2 when name='c' then 2 end) as '和'
from tb


5

判断下
cplusplus2007 2008-03-26
  • 打赏
  • 举报
回复
我一个坚决办法是用sum代替count对succ_ind字段求和,因为成功的都为1,失败的都为0,刚好可以满足要求,但是有一个比较通用的问题是,在其他地方类似于succ_ind字段的定义可能不止1和0,有可能会有2、3、4代表不同含义,如果这样的话,SQL该如何写啊?
cplusplus2007 2008-03-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wzy_love_sly 的回复:]
我听人说left join很消耗资源
-----------------------
谁说的?
[/Quote]

一个老外写的一本书,关于SQL Server的,名字叫存储引擎,
cplusplus2007 2008-03-26
  • 打赏
  • 举报
回复
对不起漏了一个条件,应该是如下:

select t1.id,t1.sums,t2.succCount*100.0/t1.totalCount as succ_rate
from
(select id,sum(salse) as sums,count(succ_ind) as totalCount from tblSalse
where time between starttime and endtime) as t1
left join
(select id,count(succ_ind) as succCount from tblSalse where succ_ind=1 and time between starttime and endtime) as t2
on t1.id=t2.id

wzy_love_sly 2008-03-26
  • 打赏
  • 举报
回复
time 键非聚集索引
wzy_love_sly 2008-03-26
  • 打赏
  • 举报
回复
我听人说left join很消耗资源
-----------------------
谁说的?

34,587

社区成员

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

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