求一SQL语句(Sybase中)。。关于sum(结果集合的)。谢过各位了。

老土豆T 2003-10-29 09:27:41
select sum(字段一)as t1,sum(字段二)as t2 into 临时表 form 正表 group by 字段0。


但后来sql语句需要改动 sum(字段二) 需要一个限定条件。
所以语句的意思应该是
select sum(字段一) as t1, sum(select 字段二 where 条件一) as t2 into 临时表 from 正表 group by 字段0


上面的语句是不对,请教各位前辈,这类情况改如何写sql?
...全文
138 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
老土豆T 2003-10-29
  • 打赏
  • 举报
回复
lynx1111,谢谢你。。 ,能不能通过多条t-sql 语句。完成么?

因为考虑到数据库的移植,存储过程也是在考虑之外的(而且这里逻辑相对不是复杂)

所以我想通过 3条或者再多一点的t-sql 完成,,谢谢大家。
老土豆T 2003-10-29
  • 打赏
  • 举报
回复
用case啊///// 啊,那效率,我可能要汇总近10万条记录呢。
lynx1111 2003-10-29
  • 打赏
  • 举报
回复
select sum(字段一),sum(case when 字段3=1 then 字段二 end)as t2 into 临时表 from 正表 group by 字段0


sql server中没问题,祝你好运!
mixtrue 2003-10-29
  • 打赏
  • 举报
回复
对于结果insert to a table ,to realize it is very simple .

First : you create a stored procedure, in stored procedure ,create a temporary table to hold your temporary data .

Second: select the temporary data form the temporary table to insert it into your table which you want to insert.

Third: to realize your business.
老土豆T 2003-10-29
  • 打赏
  • 举报
回复
T0: lynx111
所以语句的意思应该是
select sum(字段一) as t1, sum(select 字段二 where 字段3=1) as t2 into 临时表 from 正表 group by 字段0
mixtrue 2003-10-29
  • 打赏
  • 举报
回复
To totodo(dagger in my hand)

对于field2像你所说的只有使用stored procedure 来实现,因为这么复杂的逻辑不是一个sql语句所能过实现的。你可以在stored procedure 建立temporary table 来保存你field2 的数据然后来实现,你的复杂的逻辑。我想不能可能在一条sql中实现只是sum filed2的部分数据。除非你的数据有规律可以遵循。
老土豆T 2003-10-29
  • 打赏
  • 举报
回复
to inprise_lyj

谢谢。我也是想非开写的。。。但是结果集必须放在一张表里。。。不知道您有没有好的方法,多谢指教。
lynx1111 2003-10-29
  • 打赏
  • 举报
回复
把 where 条件一 贴贴看
inprise_lyj 2003-10-29
  • 打赏
  • 举报
回复
那就只能分开写了呀!
老土豆T 2003-10-29
  • 打赏
  • 举报
回复
to mixtrue 我也知道这样不符合 关系形 数据库。


我的意思是, sum(字段一) 的时候假没有条件限制,假设全部100条。。但是sum(字段二)的时候有一些条件限制,比如只50条,,而结果是按照一个字段归类的。
mixtrue 2003-10-29
  • 打赏
  • 举报
回复
搂住你的sql不正确,你既然求field1 ,field2分组的话,这样field1,field2 必须在group by 中出现否则告诉你不正确。

搂住你可以这样啊,这样是正确的sql 语句

select sum(字段一)as t1,sum(字段二)as t2 into 临时表
form 正表 where 条件一(字段二的条件)
group by 字段0。

for example:

select sum(field1),sum(field2) into temparory_table
from table where field2 =condition1 group by field1
huangry 2003-10-29
  • 打赏
  • 举报
回复
帮你顶
老土豆T 2003-10-29
  • 打赏
  • 举报
回复
lynx1111 :) 非常感谢。。。
lynx1111 2003-10-29
  • 打赏
  • 举报
回复
sql server 中测试:
select sum(字段1)as aa,sum(case when 字段3=1 then 字段2 end)as bb from 正表 group by 字段0
select sum(字段1)from 正表
select sum(字段2)from 正表 where 字段3=1
select (select sum(字段1)from 正表)as aa,(select sum(字段2)from 正表 where 字段3=1)as bb
查询1:查询成本(相对于批处理):24.58%
查询2:查询成本(相对于批处理):18.85%
查询3:查询成本(相对于批处理):18.85%
查询4:查询成本(相对于批处理):37.71%


估计用多条语句完成效率会更低!

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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